xxxxxxxxxx
# Set start and end dates
start = '2016-1-1'
end = '2016-2-29'
# Create time series of consting of monthly values
monthly_index = pd.date_range(start=start, end=end, freq='M')
monthly_series = pd.Series(data=[1,2], index=monthly_index)
# Create index to resample into
weekly_index = pd.date_range(start=start, end=end, freq='W')
# resample monthly time series using weekly_index
print(monthly_series.reindex(weekly_index))
print(monthly_series.reindex(weekly_index, method='bfill'))
print(monthly_series.reindex(weekly_index, method='ffill'))
xxxxxxxxxx
# Up-sampling = Adding more frequency (Increased no of rows)
# example : converting weekly data to daily data
# We need some method to fill in the values for more rows
upsampled_df = weekly_df.resample('D').ffill() # alternatives: .bfill(), .interpolate(), etc
# Down-sampling = Reducing frequency (Decreased no of rows)
# example : converting weekly data to monthly data
# We need some method to summarize / aggregate the values for reduced rows
downsampled_df = weekly_df.resample('M').mean() # alternatives: .median(), .std(), .first() etc