Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 391
Chris Avatar asked Nov 20 '25 11:11

Chris


1 Answers

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
like image 163
sacuL Avatar answered Nov 22 '25 00:11

sacuL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!