0

I have a pandas dataframe that I have reindexed to use hourly values. It is a list of weather observations that was a mix of 3-hourly, hourly, and more-than-once-per-hour data. Now I am trying to add some columns to calculate and plot the values I need, but I have been getting the odd error I put as my question. In this code, "winter_months" is a list of tuples of date-time-groups and temperatures.

    # compute 24-hour rolling mean for winter months
    dfw = pd.DataFrame(winter_months, columns = ('date_time','temp'))
    dfw['date_time'] = pd.to_datetime(dfw['date_time'])
    dfw = dfw.set_index('date_time', drop=True).resample('H').mean()
    dfw = dfw.sort_index()
    dfw
    

This returns a lovely dataframe that is just what I wanted:
beginning of the output dataframe with hourly values from 1948 to 2021. The index is now hourly, which is terrific.

    dfw.index
    DatetimeIndex(['1948-02-15 06:00:00', '1948-02-15 07:00:00',
           '1948-02-15 08:00:00', '1948-02-15 09:00:00',
           '1948-02-15 10:00:00', '1948-02-15 11:00:00',
           '1948-02-15 12:00:00', '1948-02-15 13:00:00',
           '1948-02-15 14:00:00', '1948-02-15 15:00:00',
           ...
           '2021-03-31 14:00:00', '2021-03-31 15:00:00',
           '2021-03-31 16:00:00', '2021-03-31 17:00:00',
           '2021-03-31 18:00:00', '2021-03-31 19:00:00',
           '2021-03-31 20:00:00', '2021-03-31 21:00:00',
           '2021-03-31 22:00:00', '2021-03-31 23:00:00'],
          dtype='datetime64[ns]', name='date_time', length=641010, freq='H')

The problem comes up when I try to create a new date index so that the low temperature data can be grouped within a continuous period of Oct-Mar.

    dfw['pretend_winter'] = dfw.index + pd.DateOffset(months=9)


  

ValueError: Inferred frequency None from passed values does not conform to passed frequency H

I have looked at ValueError: Inferred frequency None from passed values does not conform to passed frequency MS and Pandas not able to infer time series frequency, despite proper formatting? and tried the solutions posted there with no luck.

What am I missing?

JudyD
  • 1
  • 1
  • If I don't resample to an hourly frequency, I am able to use the data offset to create a new "winter" year group. The reason I want hourly observations is that I am calculating a rolling mean over 24 hours, and am using a minimum of 23 periods to exclude means over missing data. The 3-hourly observations only have 8 periods in 24 hours, and I am using the resampling to fill those years' data. – JudyD Jul 27 '21 at 18:46
  • I changed the 9-month date offset to 270 days, and got rid of this error. – JudyD Jul 29 '21 at 19:20

0 Answers0