想要在每个月的最后一天获得pandas中的数据框

Mil*_*use 1 python dataframe pandas

我在pandas中有一个数据框,其中索引是工作日.我想仅使用每个月的最后一天创建一个新的数据框,以及各列中的相应数据.我尝试了几种不同的方法但收效甚微,我不断得到的错误信息是:AttributeError:'DataFrame'对象没有属性'date'.

我的数据框中的索引标记为"日期".除了验证,我不知道去哪里.此外,此列中的日期还包括小时,分钟和秒......不确定是否重要.

下面是数据框的示例:

Date                   A    B    C
11/27/2015 00:00:00    5    2    4
11/30/2015 00:00:00    2    9    1
12/1/2015  00:00:00    6    1    8
12/2/2015  00:00:00    4    7    0
Run Code Online (Sandbox Code Playgroud)

我希望结果显示出来

11/30/2015 00:00:00    2  9  1
Run Code Online (Sandbox Code Playgroud)

我尝试过的一些代码如下:两者都有相同的错误.

prices = prices.resample('M', 'first')
prices = prices.index + pd.offsets.MonthEnd(0)
Run Code Online (Sandbox Code Playgroud)

chr*_*isb 6

In [1]: df = pd.DataFrame({'a':range(1000)}, index=pd.date_range('2014-1-1', periods=1000))

In [2]: df.index.days_in_month
Out[2]: 
array([31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
       31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 28, 28, 28,
Run Code Online (Sandbox Code Playgroud)

如果相反,日期在列中,而不是索引,您可以这样做 df['Date'].dt.days_in_month

编辑:

以上就是你本来想要一个月的最后一天.相反,它听起来像你想要的? prices.index = prices.index + pd.offsets.MonthEnd(0)