为了不修改现有time
列,请使用pd.to_datetime
然后使用dt
访问器创建一个单独的日期时间序列:
# 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
另一种方法是str.split('-')
在datetime.dt.date
系列上使用:
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