Search code examples
pythonpandasdate-range

Pandas: monthly date range with number of days


Suppose I have a start and end dates like so:

start_d = datetime.date(2017, 7, 20)
end_d = datetime.date(2017, 9, 10)

I wish to obtain a Pandas DataFrame that looks like this:

Month    NumDays
2017-07  12
2017-08  31
2017-09  10

It shows the number of days in each month that is contained in my range.

So far I can generate the monthly series with pd.date_range(start_d, end_d, freq='MS').


Solution

  • You can use date_range by default day frequency first, then create Series and resample with size. Last convert to month period by to_period:

    import datetime as dt    
    
    start_d = dt.date(2017, 7, 20)
    end_d = dt.date(2017, 9, 10)
    
    s = pd.Series(index=pd.date_range(start_d, end_d), dtype='float64')
    
    df = s.resample('MS').size().rename_axis('Month').reset_index(name='NumDays')
    df['Month'] = df['Month'].dt.to_period('m')
    print (df)
        Month  NumDays
    0 2017-07       12
    1 2017-08       31
    2 2017-09       10
    

    Thank you Zero for simplifying solution:

    df = s.resample('MS').size().to_period('m').rename_axis('Month').reset_index(name='NumDays')