Pandas Time Series Resampling and Interpolation: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Series =Overview= For time series, use <code>resample()</code>. The object must have a datetime-like index (<code>DatetimeIndex</c...")
 
Line 2: Line 2:
* [[Pandas_Series#Interpolation|Series]]
* [[Pandas_Series#Interpolation|Series]]
=Overview=
=Overview=
For time series, use <code>resample()</code>. The object must have a datetime-like index (<code>DatetimeIndex</code>, <code>PeriodIndex</code>, <code>TimedeltaIndex</code>) or the caller must pass the label of a date time-like series/index.
 
This applies to time series.
 
To interpolate, resample at the desired frequency with <code>resample()</code>, and then call <code>interpolate()</code>. <font color=darkkhaki>Instead of interpolation, the new elements can be forward filled with <code>pad()</code> or back filled with <code>bfill()</code>, or filled with mean() values, but in that case we get NaNs.</code>
 
The object must have a datetime-like index (<code>DatetimeIndex</code>, <code>PeriodIndex</code>, <code>TimedeltaIndex</code>) or the caller must pass the label of a date time-like series/index.


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


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

Revision as of 02:27, 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.

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