从 Pandas 日期时间列中分别提取月份和年份 [英] Extracting just Month and Year separately from Pandas Datetime column
问题描述
我有一个数据框 df,包含以下列:
I have a Dataframe, df, with the following column:
df['ArrivalDate'] =
...
936 2012-12-31
938 2012-12-29
965 2012-12-31
966 2012-12-31
967 2012-12-31
968 2012-12-31
969 2012-12-31
970 2012-12-29
971 2012-12-31
972 2012-12-29
973 2012-12-29
...
列的元素是pandas.tslib.Timestamp.
The elements of the column are pandas.tslib.Timestamp.
我只想包括年和月.我以为会有简单的方法来做到这一点,但我想不通.
I want to just include the year and month. I thought there would be simple way to do it, but I can't figure it out.
这是我尝试过的:
df['ArrivalDate'].resample('M', how = 'mean')
我收到以下错误:
Only valid with DatetimeIndex or PeriodIndex
然后我尝试了:
df['ArrivalDate'].apply(lambda(x):x[:-2])
我收到以下错误:
'Timestamp' object has no attribute '__getitem__'
有什么建议吗?
我有点想通了.
df.index = df['ArrivalDate']
然后,我可以使用索引对另一列重新采样.
Then, I can resample another column using the index.
但我仍然想要一种重新配置整个列的方法.有什么想法吗?
But I'd still like a method for reconfiguring the entire column. Any ideas?
推荐答案
如果您希望新列分别显示年和月,您可以这样做:
If you want new columns showing year and month separately you can do this:
df['year'] = pd.DatetimeIndex(df['ArrivalDate']).year
df['month'] = pd.DatetimeIndex(df['ArrivalDate']).month
或...
df['year'] = df['ArrivalDate'].dt.year
df['month'] = df['ArrivalDate'].dt.month
然后您可以将它们组合起来或照原样使用它们.
Then you can combine them or work with them just as they are.
这篇关于从 Pandas 日期时间列中分别提取月份和年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!