Skip to content Skip to sidebar Skip to footer

Sampling With The Most Recent Value

Consider the following Series: created_at 2014-01-27 21:50:05.040961 80000.00 2014-03-12 18:46:45.517968 79900.00 2014-09-05 20:54:17.991260 63605.31 2014-11-04 01:16:08.2

Solution 1:

If you first define the set of pre-defined days (days in my example below), you can reindex with that and specify the filling method ('ffill' will propagate last valid observation forward, so this means take most recent for a time series):

In [19]:sOut[19]:time2014-01-27 21:50:05.040961    80000.002014-03-12 18:46:45.517968    79900.002014-09-05 20:54:17.991260    63605.312014-11-04 01:16:08.286631    64405.312014-11-04 01:17:26.398272    63605.312014-11-04 01:24:38.225306    64405.312014-11-13 19:32:14.273478    65205.31Name:my_series,dtype:float64In [20]:days=pd.date_range('2014-12-01','2014-12-07')In [21]:s.reindex(days,method='ffill')Out[21]:2014-12-01    65205.312014-12-02    65205.312014-12-03    65205.312014-12-04    65205.312014-12-05    65205.312014-12-06    65205.312014-12-07    65205.31Freq:D,Name:my_series,dtype:float64

In this case (the example dates you gave), this gives alle the same values, as for all dates the most recent observation in the original series is the same.

If you don't want to give a specific set, but just all dates from the start to end of the original Series, you can use resample do reach the same:

In [23]:s.resample('D',how='last',fill_method='ffill')Out[23]:time2014-01-27    800002014-01-28    800002014-01-29    800002014-01-30    80000...2014-11-10    64405.312014-11-11    64405.312014-11-12    64405.312014-11-13    65205.31Freq:D,Name:my_series,Length:291

Post a Comment for "Sampling With The Most Recent Value"