Pandas Time Series Resampling and Interpolation: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 10: Line 10:


<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
s.resample("2H").interpolate()
s2 = s.resample("1D").interpolate()
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
s.resample("1D").interpolate()
s2 = s.resample("2H").interpolate()
</syntaxhighlight>
 
To start resampling at a timestamp in the past:
 
<syntaxhighlight lang='py'>
s2 = s.resample("D", origin="2023-10-01").interpolate()
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 02:43, 21 October 2023

Internal

Overview

This applies to time series.

To interpolate, resample at the desired frequency with resample(), and then call interpolate(). Instead of interpolation, the new elements can be forward filled with pad() or back filled with bfill(), or filled with mean() values, but in that case we get NaNs.

The object must have a datetime-like index (DatetimeIndex, PeriodIndex, TimedeltaIndex) or the caller must pass the label of a date time-like series/index.

s2 = s.resample("1D").interpolate()
s2 = s.resample("2H").interpolate()

To start resampling at a timestamp in the past:

s2 = s.resample("D", origin="2023-10-01").interpolate()