How to include end date in pandas date_range metho

2019-02-16 16:44发布

From pd.date_range('2016-01', '2016-05', freq='M', ).strftime('%Y-%m'), the last month is 2016-04, but I was expecting it to be 2016-05. It seems to me this function is behaving like the range method, where the end parameter is not included in the returning array.

Is there a way to get the end month included in the returning array, without processing the string for the end month?

5条回答
Fickle 薄情
2楼-- · 2019-02-16 17:11

I dont think so. You need to add the (n+1) boundary

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

The start and end dates are strictly inclusive. So it will not generate any dates outside of those dates if specified. http://pandas.pydata.org/pandas-docs/stable/timeseries.html

Either way, you have to manually add some information. I believe adding just one more month is not a lot of work.

查看更多
▲ chillily
3楼-- · 2019-02-16 17:16

Include the day when specifying the 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')
查看更多
小情绪 Triste *
4楼-- · 2019-02-16 17:20

A way to do it without messing with figuring out month ends yourself.

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')
查看更多
甜甜的少女心
5楼-- · 2019-02-16 17:29

You can use .union to add the next logical value after initializing the 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 might not be the right thing to use, but it satisfies your question:

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

In either case, the resulting output is as expected:

['2016-01' '2016-02' '2016-03' '2016-04' '2016-05']
查看更多
相关推荐>>
6楼-- · 2019-02-16 17:30

For the later crowd. You can also try to use the Month-Start 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')
查看更多
登录 后发表回答