简体   繁体   English

python pandas DatetimeIndex如何获取月份的第一天?

[英]python pandas DatetimeIndex how to get first day of month?

begin = '2016-08-01'
end = '2018-07-01'
time_unit = 'm'
unit = 'month'

time_dict = {}
time_series = pd.DatetimeIndex(start=begin, end=end, freq=time_unit).tolist()

all month generated DatetimeIndex start end day of month. 所有月份生成的DatetimeIndex开始于月份的结束日期。 for example 例如

2016-08-31 00:00:00

how can I get 我怎样才能得到

2016-08-01 00:00:00

Change m to MS for month start frequency : m更改为MS以表示月份开始频率

begin = '2016-08-01'
end = '2018-07-01'
time_unit = 'MS'
unit = 'month'

time_dict = {}
time_series = pd.DatetimeIndex(start=begin, end=end, freq=time_unit).tolist()
print (time_series)
[Timestamp('2016-08-01 00:00:00', freq='MS'), Timestamp('2016-09-01 00:00:00', freq='MS'), Timestamp('2016-10-01 00:00:00', freq='MS'), Timestamp('2016-11-01 00:00:00', freq='MS'), Timestamp('2016-12-01 00:00:00', freq='MS'), Timestamp('2017-01-01 00:00:00', freq='MS'), Timestamp('2017-02-01 00:00:00', freq='MS'), Timestamp('2017-03-01 00:00:00', freq='MS'), Timestamp('2017-04-01 00:00:00', freq='MS'), Timestamp('2017-05-01 00:00:00', freq='MS'), Timestamp('2017-06-01 00:00:00', freq='MS'), Timestamp('2017-07-01 00:00:00', freq='MS'), Timestamp('2017-08-01 00:00:00', freq='MS'), Timestamp('2017-09-01 00:00:00', freq='MS'), Timestamp('2017-10-01 00:00:00', freq='MS'), Timestamp('2017-11-01 00:00:00', freq='MS'), Timestamp('2017-12-01 00:00:00', freq='MS'), Timestamp('2018-01-01 00:00:00', freq='MS'), Timestamp('2018-02-01 00:00:00', freq='MS'), Timestamp('2018-03-01 00:00:00', freq='MS'), Timestamp('2018-04-01 00:00:00', freq='MS'), Timestamp('2018-05-01 00:00:00', freq='MS'), Timestamp('2018-06-01 00:00:00', freq='MS'), Timestamp('2018-07-01 00:00:00', freq='MS')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM