假设我有以下内容:
start = dt.datetime(2015,07,01)
end = dt.datetime(2016,06,30)
rng = pd.date_range(start, end,freq='M')
print rng
DatetimeIndex(['2015-07-31', '2015-08-31', '2015-09-30', '2015-10-31',
'2015-11-30', '2015-12-31', '2016-01-31', '2016-02-29',
'2016-03-31', '2016-04-30', '2016-05-31', '2016-06-30'],
dtype='datetime64[ns]', freq='M')
我希望:
rng[0] + MonthBegin()
给出“2015-07-01”的输出。相反,它显示“2015-08-01”
我错过了什么?
最佳答案
请参阅Anchored Offset Semantics文档部分。
锚定偏移将捕捉到下一个 anchor 。如果您的日期在一个月内,MonthBegin()
将捕捉到下个月的月初。
如果您想要当月的开始,您可以减去 MonthBegin()
:
rng[0] - MonthBegin()
或者您可以添加MonthBegin(-1)
:
rng[0] + MonthBegin(-1)
关于python - 不理解 pandas 中 MonthEnd() 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38211814/