Pandas resample up to certain date - filling missing timeseries

Viewed 147

I am trying to resample my time series to gain a consistent dataframe shape across many iterations.

Sometime when I pull my data, there is no result, so I am trying to resample my dataframe to include a fill for every time this has happened, however I want to force the resample to run up to a certain date.

My current efforts include

df.set_index(df.date, inplace = True)
resampled = df.resample('D').sum()

But I am unsure on how to force the resampler to continue to the latest date

I have also tried :

df.index = pd.period_range(min(older_df.date), max(older_df.date))

but then there is a length mismatch.

1 Answers

Chain the resample with reindex:

min_date = df.index.min()
max_date = '2020-01-01'   # change your date here

daterange = pd.date_range(min_date, max_date)

df.resample('D').sum().reindex(daterange)
Related