python pandas time series year extraction
No need to apply a function for each row there is a new datetime accessor you can call to access the year property:
In [35]:df1['year'] = df1['timestamp'].dt.yeardf1Out[35]: timestamp year0 2005-08-31 16:39:40 20051 2005-12-28 16:00:34 20052 2005-10-21 17:52:10 20053 2014-01-28 12:23:15 20144 2014-01-28 12:23:15 20145 2011-02-04 18:32:34 20116 2011-02-04 18:32:34 20117 2011-02-04 18:32:34 2011
If your timestamps are str then you can convert to datetime64 using pd.to_dateime
:
df['timestamp'] = pd.to_datetime(df['timestamp'])
You can access the months and other attributes using dt
like the above.
For version prior to 0.15.0
you can perform the following:
df1['year'] = df1['timestamp'].apply(lambda x: x.year)