简体   繁体   中英

pd.date_range is returning start value at the end of the month then moving forward with monthly dates?

val.index[-1] = '2019-08-01'

pd.date_range(start = val.index[-1], periods = 6,freq='M').tolist()

[Timestamp('2019-08-31 00:00:00', freq='M'),
 Timestamp('2019-09-30 00:00:00', freq='M'),
 Timestamp('2019-10-31 00:00:00', freq='M'),
 Timestamp('2019-11-30 00:00:00', freq='M'),
 Timestamp('2019-12-31 00:00:00', freq='M'),
 Timestamp('2020-01-31 00:00:00', freq='M')]

I am trying to create a list from an index for a forecast however the list is populating with the last day of the month and then moving forward with monthly dates. My goal is to obtain a list like this:

[Timestamp('2019-09-01 00:00:00', freq='M'),
 Timestamp('2019-10-01 00:00:00', freq='M'),
 Timestamp('2019-11-01 00:00:00', freq='M'),
 Timestamp('2019-12-01 00:00:00', freq='M'),
 Timestamp('2020-01-01 00:00:00', freq='M'),
 Timestamp('2020-02-01 00:00:00', freq='M')]

If you know the length of your dataframe, you could do this:

this_date = pd.date_range(start = '2019-08-31',periods = 6, freq = "M")
A = [1,2,3,4,5,6]
df = pd.DataFrame({'A':A}, index = this_date)

df.reset_index(drop = True, inplace = True)
df.set_index(pd.date_range(start = '2019-08-31',periods = 6, freq = "MS"))

is this applicable to your question?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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