简体   繁体   中英

Pandas Timestamp(year=2011, month=8, day=1) returns last day of month

Wanted to create a time range from August 1, 2011 dynamically to the last month of the existing data. Don't know why I'm returning time series with the last day of the month instead of the first.

Any suggestions? Please ignore my petty comments to my coworkers.

# Our Formatting System is garbage - So Have to code it to non garbage
    start_date = pd.Timestamp(2011, 8, 1,)
    months = pd.date_range(start_date, periods=len(timeseries[0]), freq='M')
    print(months)

在此处输入图片说明

By using the freq M , you are telling it to use month's end. See this link for a description of datetime offsets in pandas, but in short M is

month end frequency

Use 'MS' (month start) instead:

>>> start_date = pd.Timestamp(2011, 8, 1,)
>>> months = pd.date_range(start_date, periods=10, freq='MS')
>>> print(months)
DatetimeIndex(['2011-08-01', '2011-09-01', '2011-10-01', '2011-11-01',
               '2011-12-01', '2012-01-01', '2012-02-01', '2012-03-01',
               '2012-04-01', '2012-05-01'],
              dtype='datetime64[ns]', freq='MS')

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