Want the last day of each month for a data frame in pandas

匿名 (未验证) 提交于 2019-12-03 01:36:02

问题:

I have a data frame in pandas where the index are business days. I want to create a new data frame using only the last day of each month, along with the corresponding data in the various columns. I have tried a few different ways with little success and the error message I keep getting is: AttributeError: 'DataFrame' object has no attribute 'date'.

The index in my data frame is labeled 'Date'. Other than verifying that, I don't know where to go. Also, the dates in this column include hours, minutes, and seconds...not sure if that matters.

Below is an example of what the data frame looks like:

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

I'd like for the result to show

11/30/2015 00:00:00    2  9  1

Some of the code I have tried is as follows: Got the same error with both.

prices = prices.resample('M', 'first') prices = prices.index + pd.offsets.MonthEnd(0)

回答1:

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,

If instead the dates are in a column, not the index, you would do df['Date'].dt.days_in_month

Edit:

Above is if you had wanted the last day of the month by itself. Instead, it sounds like you want? prices.index = prices.index + pd.offsets.MonthEnd(0)



回答2:

my first contribution to STACKOVERFLOW YESSS

the below will work even if the last day of the month is NOT in your original time series.... worked for me

there may be extra coding that i used in my thing, but the spirit is below:

for i in range(df.size):   if i==0:     #get the first date whatever that is:     lastdayofmonthseries = pd.DataFrame(data=[df.ix[0][0]], index=[df.ix[0].name],columns=[df.columns[0]])    else:       if i< df.size-1:         # print SpotCloses.ix[i],SpotCloses.ix[i+1]          if df.ix[i].name.month!=df.ix[i+1].name.month:           #this will find the last day of each month in time series           TempDF=pd.DataFrame(data=[df.ix[i][0]],index=[df.ix[i].name],columns=[df.columns[0]])           lastdayofmonthseries=lastdayofmonthseries.append(TempDF)       else:           #check if its the last date and save it whatever that is           if i==df.size-1:               TempDF = pd.DataFrame(data=[df.ix[i][0]], index=[df.ix[i].name],columns=[df.columns[0]])               lastdayofmonthseries = lastdayofmonthseries.append(TempDF)


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!