How to include end date in pandas date_range method?

From pd.date_range('2016-01', '2016-05', freq='M', ).strftime('%Y-%m') , last month 2016-04 , but I expected it to be 2016-05 . It seems to me that this function behaves like a range method, where the final parameter is not included in the returned array.

Is there a way to get the final month included in the returned array without processing the string at the end of the month?

+5
source share
5 answers

The way to do this, without falling into the calculation of the month, ends by itself.

 pd.date_range(*(pd.to_datetime(['2016-01', '2016-05']) + pd.offsets.MonthEnd()), freq='M') DatetimeIndex(['2016-01-31', '2016-02-29', '2016-03-31', '2016-04-30', '2016-05-31'], dtype='datetime64[ns]', freq='M') 
+2
source

You can use .union to add the following boolean after initializing date_range . It should work as written for any frequency:

 d = pd.date_range('2016-01', '2016-05', freq='M') d = d.union([d[-1] + 1]).strftime('%Y-%m') 

Alternatively, you can use period_range instead of date_range . Depending on what you intend to do, this may not be right, but it satisfies your question:

 pd.period_range('2016-01', '2016-05', freq='M').strftime('%Y-%m') 

In any case, the resulting output will be as expected:

 ['2016-01' '2016-02' '2016-03' '2016-04' '2016-05'] 
+4
source

For the later crowd. You can also try using the "Start of the month" frequency.

 >>> pd.date_range('2016-01', '2016-05', freq='MS', format = "%Y-%m" ) DatetimeIndex(['2016-01-01', '2016-02-01', '2016-03-01', '2016-04-01', '2016-05-01'], dtype='datetime64[ns]', freq='MS') 
+2
source

I do not think so. You need to add a border (n + 1)

  pd.date_range('2016-01', '2016-06', freq='M' ).strftime('%Y-%m') 

Start and end dates are strictly included. So this will not generate any dates beyond these dates, if indicated. http://pandas.pydata.org/pandas-docs/stable/timeseries.html

In any case, you need to manually add some information. I think adding another month is not a lot of work.

0
source

Include day by specifying dates in date_range call

 pd.date_range('2016-01-31', '2016-05-31', freq='M', ).strftime('%Y-%m') array(['2016-01', '2016-02', '2016-03', '2016-04', '2016-05'], dtype='|S7') 
0
source

All Articles