从 Pandas 中的 DatetimeIndex 制作月份和年份列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31710454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:41:58  来源:igfitidea点击:

Making a list of months and years from DatetimeIndex in Pandas

pythonlistpandasdataframestrftime

提问by jenryb

I have a dataframe of information. I set the index to be the received date and time. Now I want a list

我有一个信息数据框。我将索引设置为接收的日期和时间。现在我想要一个清单

I set the df index doing this:

我设置 df 索引这样做:

df.index = pd.to_datetime(df.index, format='%m/%d/%Y %H:%M')

which gives me this:

这给了我这个:

print df.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-07-28 09:42:08, ..., 2015-07-28 09:06:12]
Length: 15177, Freq: None, Timezone: None

I want a list of the month and years in order to use them to plot, like so: ["Jan 2015", "Feb 2015", "Mar 2015", "Apr 2015", "May 2015", "June 2015", "Jul 2015", "Aug 2014", "Sep 2014", "Oct 2014", "Nov 2014", "Dec 2014"]

我想要一个月份和年份的列表,以便使用它们进行绘图,如下所示: ["Jan 2015", "Feb 2015", "Mar 2015", "Apr 2015", "May 2015", "June 2015", "Jul 2015", "Aug 2014", "Sep 2014", "Oct 2014", "Nov 2014", "Dec 2014"]

How can I do this? I've looked into something like this:

我怎样才能做到这一点?我研究过这样的事情:

df = [datetime.datetime.strftime(n,'%b-%Y') for n in pd.DataFrame(df).resample('M').index] 

But this gives me the error DataError: No numeric types to aggregate.

但这给了我错误DataError: No numeric types to aggregate

回答by EdChum

Original answer

原答案

The following should work: convert your datetimeindex to a series, so you can call applyand use strftimeto return an array of strings:

以下应该有效:将您的 datetimeindex 转换为series,以便您可以调用apply和使用strftime来返回一个字符串数组:

In [27]:
import datetime as dt
import pandas as pd
df = pd.DataFrame(index=pd.date_range(start = dt.datetime(2014,1,1), end = dt.datetime.now(), freq='M'))
df.index.to_series().apply(lambda x: dt.datetime.strftime(x, '%b %Y'))

Out[27]:
2014-01-31    Jan 2014
2014-02-28    Feb 2014
2014-03-31    Mar 2014
2014-04-30    Apr 2014
2014-05-31    May 2014
2014-06-30    Jun 2014
2014-07-31    Jul 2014
2014-08-31    Aug 2014
2014-09-30    Sep 2014
2014-10-31    Oct 2014
2014-11-30    Nov 2014
2014-12-31    Dec 2014
2015-01-31    Jan 2015
2015-02-28    Feb 2015
2015-03-31    Mar 2015
2015-04-30    Apr 2015
2015-05-31    May 2015
2015-06-30    Jun 2015
Freq: M, dtype: object

If you want a list then just call tolist():

如果你想要一个列表,那么只需调用tolist()

df.index.to_series().apply(lambda x: dt.datetime.strftime(x, '%b %Y')).tolist()


Updated answer

更新答案

Actually, looking at this question 2 years later, I realise the above is completely unnecessary. You can just do:

其实,两年后看这个问题,我意识到以上是完全没有必要的。你可以这样做:

In [10]:
df.index.strftime('%Y-%b')

Out[10]:
array(['2014-Jan', '2014-Feb', '2014-Mar', '2014-Apr', '2014-May',
       '2014-Jun', '2014-Jul', '2014-Aug', '2014-Sep', '2014-Oct',
       '2014-Nov', '2014-Dec', '2015-Jan', '2015-Feb', '2015-Mar',
       '2015-Apr', '2015-May', '2015-Jun', '2015-Jul', '2015-Aug',
       '2015-Sep', '2015-Oct', '2015-Nov', '2015-Dec', '2016-Jan',
       '2016-Feb', '2016-Mar', '2016-Apr', '2016-May', '2016-Jun',
       '2016-Jul', '2016-Aug', '2016-Sep', '2016-Oct', '2016-Nov',
       '2016-Dec', '2017-Jan', '2017-Feb', '2017-Mar', '2017-Apr',
       '2017-May', '2017-Jun', '2017-Jul'], 
      dtype='<U8')

datetimeindexsupport .dtaccessors directly without converting to a Series

datetimeindex.dt直接支持访问器,无需转换为Series