0

With pandas 1.40 it was introduced the argument inclusive in the class pd.date_range(). Reading the documentation:

inclusive{“both”, “neither”, “left”, “right”}, default “both” Include boundaries; Whether to set each bound as closed or open

I wanted to use it to create a df with monthly frequency starting on the first day of 01/2020 and finishing on the first day of the month of 12/2022. I wrote this code:

df = pd.date_range(start='01/01/2020', end='31/12/2022', freq = "M", inclusive = "left")

Which is not giving my expected output.

DatetimeIndex(['2020-01-31', '2020-02-29', '2020-03-31', '2020-04-30',
           '2020-05-31', '2020-06-30', '2020-07-31', '2020-08-31',
           '2020-09-30', '2020-10-31', '2020-11-30', '2020-12-31',
           '2021-01-31', '2021-02-28', '2021-03-31', '2021-04-30',
           '2021-05-31', '2021-06-30', '2021-07-31', '2021-08-31',
           '2021-09-30', '2021-10-31', '2021-11-30', '2021-12-31',
           '2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30',
           '2022-05-31', '2022-06-30', '2022-07-31', '2022-08-31',
           '2022-09-30', '2022-10-31', '2022-11-30'],
          dtype='datetime64[ns]', freq='M')

Expected Output

DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01',
           '2020-05-01', '2020-06-01', '2020-07-01', '2020-08-01',
           '2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01',
           '2021-01-01', '2021-02-01', '2021-03-01', '2021-04-01',
           '2021-05-01', '2021-06-01', '2021-07-01', '2021-08-01',
           '2021-09-01', '2021-10-01', '2021-11-01', '2021-12-01',
           '2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01',
           '2022-05-01', '2022-06-01', '2022-07-01', '2022-08-01',
           '2022-09-01', '2022-10-01', '2022-11-01', '2022-12-01' ],
          dtype='datetime64[ns]', freq='M')
  1. How do I get my expected output with pd.date_range()?
  2. What I misunderstood on how to use correctly the argument inclusive inside pd.date_range()?
Andrea Ciufo
  • 359
  • 1
  • 3
  • 19
  • 1
    I will not be able to explain the whole answer but frequency "MS"(month start frequency) is what you need to specify in freq. Check [Offset aliases](https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases) for more detail. – imxitiz Jan 19 '23 at 17:30
  • 1
    Does this answer your question? [Pandas date\_range to generate monthly data at beginning of the month](https://stackoverflow.com/questions/34915828/pandas-date-range-to-generate-monthly-data-at-beginning-of-the-month) – imxitiz Jan 19 '23 at 17:31
  • @imxitiz yes thank you! I searched before but it was not enough – Andrea Ciufo Jan 19 '23 at 20:04
  • 1
    @imxitiz while i fixed one issue i would also understand better how inclusive works and if is just because i didn't defined `MS` as is something introduced recently with pandas 1.4.0 but probably is a not specific question? – Andrea Ciufo Jan 19 '23 at 20:38

2 Answers2

1

Following on from @imxitiz comment, try

pd.date_range(start='01/01/2020', end='31/12/2022', freq = "MS", inclusive = "left")

Output:

DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01',
               '2020-05-01', '2020-06-01', '2020-07-01', '2020-08-01',
               '2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01',
               '2021-01-01', '2021-02-01', '2021-03-01', '2021-04-01',
               '2021-05-01', '2021-06-01', '2021-07-01', '2021-08-01',
               '2021-09-01', '2021-10-01', '2021-11-01', '2021-12-01',
               '2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01',
               '2022-05-01', '2022-06-01', '2022-07-01', '2022-08-01',
               '2022-09-01', '2022-10-01', '2022-11-01', '2022-12-01'],
              dtype='datetime64[ns]', freq='MS')
imxitiz
  • 3,920
  • 3
  • 9
  • 33
s_pike
  • 1,710
  • 1
  • 10
  • 22
0

I have pandas version 1.1.5 Inclusive is not recognized and generates an exception like this :

*builtins.TypeError: _generate_range() got an unexpected keyword argument 'inclusive'*

What you want has nothing to do with inclusive but to frequency type :

  • M for month end
  • MS for month beginning (S is for Start)

All types defined here : https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases

Proposed command line

df = pd.date_range(start='01/01/2020', end='31/12/2022', freq = "MS")

Result

DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01',
               '2020-05-01', '2020-06-01', '2020-07-01', '2020-08-01',
               '2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01',
               '2021-01-01', '2021-02-01', '2021-03-01', '2021-04-01',
               '2021-05-01', '2021-06-01', '2021-07-01', '2021-08-01',
               '2021-09-01', '2021-10-01', '2021-11-01', '2021-12-01',
               '2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01',
               '2022-05-01', '2022-06-01', '2022-07-01', '2022-08-01',
               '2022-09-01', '2022-10-01', '2022-11-01', '2022-12-01'],
              dtype='datetime64[ns]', freq='MS')
Laurent B.
  • 1,653
  • 1
  • 7
  • 16