@@ -305,22 +305,22 @@ def PDresample_by_hour(df, function_dict, function_stats):
305305 :param df: pandas dataframe with ancillary variables and coords removed but with TIME as index
306306 :return: pandas dataframe
307307 """
308- ## back the index 30min
309- df .index = df .index - pd .Timedelta (30 , units = 'm' )
310308
311309 varnames = df .columns
312- df_data = pd .DataFrame ()
310+ df_data = pd .DataFrame (index = pd . DatetimeIndex ([]) )
313311 for variable in varnames :
314312 ds_var = df [variable ]
315- ds_var_mean = ds_var .resample ('1H' ).apply (function_dict [variable ]).astype (np .float32 )
313+ ds_var_resample = ds_var .resample ('1H' , base = 0.5 ) # shift by half hour to centre bin on the hour
314+ ds_var_mean = ds_var_resample .apply (function_dict [variable ]).astype (np .float32 )
316315 df_data = pd .concat ([df_data , ds_var_mean ], axis = 1 , sort = False )
317316 for stat_method in function_stats :
318- ds_var_stat = ds_var . resample ( '1H' ) .apply (stat_method ).astype (np .float32 )
317+ ds_var_stat = ds_var_resample .apply (stat_method ).astype (np .float32 )
319318 ds_var_stat = ds_var_stat .rename ("_" .join ([variable , stat_method ]))
320319 df_data = pd .concat ([df_data , ds_var_stat ], axis = 1 , sort = False )
321320
322- ##forward the index 30min
323- df_data .index = df_data .index + pd .Timedelta (30 , units = 'm' )
321+ ##forward the index 30min so the timestamps are on the hour
322+ df_data .index += pd .to_timedelta ('30min' )
323+
324324 return df_data
325325
326326
0 commit comments