1

I am trying to group a Pandas DataFrame that is indexed by date by the business day of month, approx 22/month.

I would like to return a result that contains 22 rows with mean of some value in `DataFrame.

I can by day of month but cant seem to figure out how to by business day.

Is there a function that will return the business day of month of a date?

if someone could provide a simple example that would be most appreciated.

Stefan
  • 41,759
  • 13
  • 76
  • 81
Mike U
  • 2,901
  • 10
  • 32
  • 44
  • Possible duplicate of [Using Python to count the number of business days in a month?](http://stackoverflow.com/questions/18233122/using-python-to-count-the-number-of-business-days-in-a-month) – PyNEwbie Apr 29 '16 at 01:16
  • How do you want to handle any data for a weekend? Ignore it, roll it forward to Monday, or roll it back to the preceding Friday? – Alexander Apr 29 '16 at 01:51
  • Alexander the dataframe only contains data from business days, there is no weekend data – Mike U Apr 29 '16 at 07:12

2 Answers2

2

Assuming your dates are in the index (if not use 'set_index):

df.groupby(pd.TimeGrouper('B')) 

See time series functionality.

Stefan
  • 41,759
  • 13
  • 76
  • 81
  • df.groupby('B') does not work it just throws an error – Mike U Apr 29 '16 at 07:11
  • Thanks for answer Stefan. What I was wanting to achieve was somthing like the following df.groupby(pd.TimeGrouper('B'))['Amount'].mean() to return 22 rows with the mean value for each business day of the month – Mike U Apr 29 '16 at 07:37
  • You are welcome. Hope this answered your question about grouping by business day. – Stefan Apr 29 '16 at 08:02
1

I think what the question is asking is to groupby business day of month - the other answer just seems to resample the data to the nearest business day (at least for me). This code returns a groupby object with 22 rows

from datetime import date
import pandas as pd
import numpy as np

d = pd.Series(np.random.randn(1000), index=pd.bdate_range(start='01 Jan 2018', periods=1000))
def to_bday_of_month(dt):
    month_start = date(dt.year, dt.month, 1)
    return np.busday_count(month_start, dt)

day_of_month = [to_bday_of_month(dt) for dt in d.index.date]
d.groupby(day_of_month).mean()
Antonbass
  • 319
  • 2
  • 5