简体   繁体   中英

pandas Grouper by month aggregation returns previous month as datetime index

I am using the following code to group by ohlc (open, high, low, close) prices into a monthly aggregate.

df = df.groupby(pd.Grouper(freq='M',level=0,label='left')).agg(ohlc_dict)

This code is working perfectly to aggregate for minutes, days and weeks. But when I aggregate for months it is returning me as the datetime value of the last day of the previous month (instead of the first day of the actual month). For example:

datetime                                                                 
2004-05-31 20:59:00

is being aggregated to:

datetime                                                                 
2004-04-30

If I use label='right' it will return me 2005-05-31 , the desired value for this statement is 2005-05-01 . Why this is happening and what is the best way to achieve the desired statement?

Thank you

As @MaxU offered, use freq='MS' instead of 'M' . You can see all the possible frequencies here:

http://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM