def hourly_resample(df, bse=0, minutes=60):
"""
Args:
df:
pandas dataframe containing time series needing resampling
bse (int):
base time to set; optional; default is zero (on the hour);
minutes (int):
sampling recurrence interval in minutes; optional; default is 60 (hourly samples)
Returns:
A Pandas DataFrame that has been resampled to every hour, at the minute defined by the base (bse)
Description:
see http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.resample.html for more info
This function uses pandas powerful time-series manipulation to upsample to every minute, then downsample to every hour,
on the hour.
This function will need adjustment if you do not want it to return hourly samples, or iusgsGisf you are sampling more frequently than
once per minute.
see http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases
"""
if int(str(pd.__version__).split('.')[0]) == 0 and int(str(pd.__version__).split('.')[1]) < 18: # pandas versioning
df = df.resample('1Min')
else:
# you can make this smaller to accomodate for a higher sampling frequency
df = df.resample('1Min').first()
# http://pandas.pydata.org/pandas-docs/dev/generated/pandas.Series.interpolate.html
df = df.interpolate(method='time', limit=90)
if int(str(pd.__version__).split('.')[0]) == 0 and int(str(pd.__version__).split('.')[1]) < 18: # pandas versioning
df = df.resample(str(minutes) + 'Min', closed='left', label='left', base=bse)
else:
# modify '60Min' to change the resulting frequency
df = df.resample(str(minutes) + 'Min', closed='left', label='left', base=bse).first()
return df
评论列表
文章目录