Extract Year, Month and Day from datetime64[ns, UTC], Python

7.5k Views Asked by At

I have this column in a df:

    > df["time"]
0         2007-02-01 22:00:00+00:00
1         2007-02-01 22:00:00+00:00
2         2007-02-01 22:00:00+00:00
3         2007-02-01 22:00:00+00:00
4         2007-02-01 22:00:00+00:00

I want to create three new columns with day, month, and year but I can't figure out a way to extract each one of these of the time column.

1

There are 1 best solutions below

0
On BEST ANSWER

In order to not modify your existing time column, create a separate datetime series using pd.to_datetime and then use the dt accessor:

# obtain datetime series:
datetimes = pd.to_datetime(df['time'])

# assign your new columns
df['day'] = datetimes.dt.day
df['month'] = datetimes.dt.month
df['year'] = datetimes.dt.year

>>> df
                        time  day  month  year
0  2007-02-01 22:00:00+00:00    1      2  2007
1  2007-02-01 22:00:00+00:00    1      2  2007
2  2007-02-01 22:00:00+00:00    1      2  2007
3  2007-02-01 22:00:00+00:00    1      2  2007
4  2007-02-01 22:00:00+00:00    1      2  2007

An alternative would be to use str.split('-') on the datetime.dt.date series:

datetimes = pd.to_datetime(df['time'])

df[['year','month','day']] = datetimes.dt.date.astype(str).str.split('-',expand=True)

>>> df
                        time  year month day
0  2007-02-01 22:00:00+00:00  2007    02  01
1  2007-02-01 22:00:00+00:00  2007    02  01
2  2007-02-01 22:00:00+00:00  2007    02  01
3  2007-02-01 22:00:00+00:00  2007    02  01
4  2007-02-01 22:00:00+00:00  2007    02  01