简体   繁体   中英

pandas groupby dates within a quarter

I have two companies with different year-ends (1/31 and 12/31) and I want to get the average for metrics that occur in their respective quarters. For this example, I've created a DataFrame with the 8 quarter-end dates for 2016-2017 for both companies:

comp1 = pd.date_range('1/31/2016', periods=8, freq='3M')
comp2 = pd.date_range('1/31/2016', periods=8, freq='Q')
quarters = pd.DataFrame([1] * 8 + [2] * 8, index=comp1.append(comp2), columns=['company'])

Here's made up data, where I have two values (A and B) that are measured at random dates within each month for the years 2016 and 2017:

values = np.transpose([np.arange(1, 25), np.arange(1, 25) *  11])
dates = ['2016-01-14', '2016-02-03', '2016-03-15', '2016-04-04', 
         '2016-05-30', '2016-06-11', '2016-07-18', '2016-08-08', 
         '2016-09-09', '2016-10-10', '2016-11-01', '2016-12-24', 
         '2017-01-30', '2017-02-19', '2017-03-13', '2017-04-24', 
         '2017-05-31', '2017-06-02', '2017-07-28', '2017-08-23', 
         '2017-09-04', '2017-10-30', '2017-11-11', '2017-12-06']
df = pd.DataFrame(values, index=pd.DatetimeIndex(dates), columns=['A', 'B'])

The data looks like this:

             A    B
2016-01-14   1   11
2016-02-03   2   22
2016-03-15   3   33
2016-04-04   4   44
2016-05-30   5   55
2016-06-11   6   66
2016-07-18   7   77
2016-08-08   8   88
2016-09-09   9   99
2016-10-10  10  110
2016-11-01  11  121
2016-12-24  12  132
2017-01-30  13  143
2017-02-19  14  154
2017-03-13  15  165
2017-04-24  16  176
2017-05-31  17  187
2017-06-02  18  198
2017-07-28  19  209
2017-08-23  20  220
2017-09-04  21  231
2017-10-30  22  242
2017-11-11  23  253
2017-12-06  24  264

Here's the result I want, grouping by the quarters and averaging the values within each quarter:

            company   A    B
2016-01-31        1   1   11
2016-04-30        1   3   33
2016-07-31        1   6   66
2016-10-31        1   9   99
2017-01-31        1  12  132
2017-04-30        1  15  165
2017-07-31        1  18  198
2017-10-31        1  21  231
2016-03-31        2   2   22
2016-06-30        2   5   55
2016-09-30        2   8   88
2016-12-31        2  11  121
2017-03-31        2  14  154
2017-06-30        2  17  187
2017-09-30        2  20  220
2017-12-31        2  23  253

You can resample your datetime index by Quarter period and calculate the mean for that period.

df.resample('Q-JAN', convention='end').agg('mean')

And you can also do a groupby operation on company:

df.groupby('company').resample('Q-JAN', convention='end').agg('mean')

Lets assume your DataFrame has column 'date_of_order'. The easiest way would be:

df['date_of_order'] = pd.to_datetime(df['date_of_order']) # if you haven't converted it already

df.groupby(df['date_of_order'].dt.to_period('Q'))['column to aggregate'].agg(...)

@iDrwish had responded with:

df.resample('Q', convention='end').agg('mean')

This works for the December year-end company, and a simple change ( Q to Q-JAN ) gets results for the January year-end company:

df.resample('Q-JAN', convention='end').agg('mean')

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