How to include end date in pandas date_range method?

前端 未结 7 1719
[愿得一人]
[愿得一人] 2021-01-02 02:42

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

7条回答
  •  悲&欢浪女
    2021-01-02 03:22

    I had a similar problem when using datetime objects in dataframe. I would set the boundaries through .min() and .max() functions and then fill in missing dates using the pd.date_range function. Unfortunately the returned list/df was missing the maximum value.

    I found two work arounds for this:

    1) Add "closed = None" parameter in the pd.date_range function. This worked in the example below; however, it didn't work for me when working only with dataframes (no idea why).

    2) If option #1 doesn't work then you can add one extra unit (in this case a day) using the datetime.timedelta() function. In the case below it over indexed by a day but it can work for you if the date_range function isn't giving you the full range.

    import pandas as pd
    import datetime as dt 
    
    #List of dates as strings
    time_series = ['2020-01-01', '2020-01-03', '2020-01-5', '2020-01-6', '2020-01-7']
    
    #Creates dataframe with time data that is converted to datetime object 
    raw_data_df = pd.DataFrame(pd.to_datetime(time_series), columns = ['Raw_Time_Series'])
    
    #Creates an indexed_time list that includes missing dates and the full time range
    
    #Option No. 1 is to use the closed = None parameter choice. 
    indexed_time = pd.date_range(start = raw_data_df.Raw_Time_Series.min(),end = raw_data_df.Raw_Time_Series.max(),freq='D',closed= None)
    print('indexed_time option #! = ', indexed_time)
    
    #Option No. 2 if the function allows you to extend the time by one unit (in this case day) 
    #by using the datetime.timedelta function to get what you need. 
    indexed_time = pd.date_range(start = raw_data_df.Raw_Time_Series.min(),end = raw_data_df.Raw_Time_Series.max()+dt.timedelta(days=1),freq='D')
    print('indexed_time option #2 = ', indexed_time)
    
    #In this case you over index by an extra day because the date_range function works properly
    #However, if the "closed = none" parameters doesn't extend through the full range then this is a good work around 
    

提交回复
热议问题