如何在 pandas date_range方法中包括结束日期? [英] How to include end date in pandas date_range method?

查看:81
本文介绍了如何在 pandas date_range方法中包括结束日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pd.date_range('2016-01', '2016-05', freq='M', ).strftime('%Y-%m')中,上个月是2016-04,但是我希望它是2016-05.在我看来,此函数的行为类似于range方法,其中end参数不包含在返回数组中.

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?

推荐答案

一种无需弄清楚月份就可以做到的方法会自己结束.

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')

这篇关于如何在 pandas date_range方法中包括结束日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆