Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert datetime string to new columns of Day, Month, Year in pandas data frame

I have a data frame with 3 columns: time (which is in the format 'YYYY-MM-DDTHH:MM:SSZ'), device_id, and rain, but I need the first column, time, to become three columns of day, month, and year with values from the timestamp.

So the original data frame looks something like this:

     time                  device_id                              rain
     2016-12-27T00:00:00Z  9b839362-b06d-4217-96f5-f261c1ada8d6   NaN
     2016-12-28T00:00:00Z  9b839362-b06d-4217-96f5-f261c1ada8d6   0.2
     2016-12-29T00:00:00Z  9b839362-b06d-4217-96f5-f261c1ada8d6   NaN
     2016-12-30T00:00:00Z  9b839362-b06d-4217-96f5-f261c1ada8d6   NaN
     2016-12-31T00:00:00Z  9b839362-b06d-4217-96f5-f261c1ada8d6   NaN

But I'm trying to get the data frame to look like this:

     day  month  year  device_id                              rain
     27   12     2016  9b839362-b06d-4217-96f5-f261c1ada8d6   NaN
     28   12     2016  9b839362-b06d-4217-96f5-f261c1ada8d6   0.2
     29   12     2016  9b839362-b06d-4217-96f5-f261c1ada8d6   NaN
     30   12     2016  9b839362-b06d-4217-96f5-f261c1ada8d6   NaN
     31   12     2016  9b839362-b06d-4217-96f5-f261c1ada8d6   NaN

I don't care about the hour/seconds/minutes but need these values from the original time stamp, and I don't even know where to start. Please help!

Here's some reproducible code to get started:

>> import pandas as pd 
>> df = pd.DataFrame([['2016-12-27T00:00:00Z', '9b839362-b06d-4217-96f5-f261c1ada8d6', 'NaN']], columns=['time', 'device_id', 'rain'])
>> print df
2016-12-27T00:00:00Z  9b849362-b06d-4217-96f5-f261c1ada8d6  NaN
like image 480
JAG2024 Avatar asked Mar 10 '23 06:03

JAG2024


1 Answers

The cleanest way is to use builtin pandas datetime functions.

First, convert the column to datetime:

df["time"] = pd.to_datetime(df["time"])

Then, extract your information:

df["day"] = df['time'].map(lambda x: x.day)
df["month"] = df['time'].map(lambda x: x.month)
df["year"] = df['time'].map(lambda x: x.year)
like image 53
nlassaux Avatar answered Apr 29 '23 13:04

nlassaux