1

I have a series,

pd.Series( [6.22, 6.23, 6.23, 6.24, 6.24, 6.25, np.nan, np.nan, np.nan, np.nan], index = pd.DatetimeIndex(['2023-08-01 10:31:40.110000', '2023-08-01 10:31:43.110000',
                   '2023-08-01 10:31:46.111000', '2023-08-01 10:31:49.111000',
                   '2023-08-01 10:31:52.111000', '2023-08-01 10:31:55.117000',
                   '2023-08-01 10:31:58.112000', '2023-08-01 10:32:01.112000',
                   '2023-08-01 10:32:04.117000', '2023-08-01 10:34:07.095000'],
                  dtype='datetime64[ns]', name='exchange_time', freq=None))

referring to this python pandas dataframe resample.last how to make sure data comes from the same row

but,

ts.resample('6S', closed='left', label='right').apply(lambda x: x.iloc[-1])

gives error, because my time index has a big gap, how to solve this? thanks.

Note: 2023-08-01 10:32:00 should be resampled as NaN as I always want the previous value no matter it is null or not. so .last() won't work.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
tesla1060
  • 2,621
  • 6
  • 31
  • 43

1 Answers1

0

Try ohlc(), like this:

ts.resample('6S', closed='left', label='right').ohlc()
result.index = result.index + to_offset('-6s')

where I shifted the resulting DatetimeIndex by a the required amount (-6s).

The resulted series now will be:

                     open  high   low  close
exchange_time                               
2023-08-01 10:31:36  6.22  6.22  6.22   6.22
2023-08-01 10:31:42  6.23  6.23  6.23   6.23
2023-08-01 10:31:48  6.24  6.24  6.24   6.24
2023-08-01 10:31:54  6.25  6.25  6.25   6.25
2023-08-01 10:32:00   NaN   NaN   NaN    NaN
2023-08-01 10:32:06   NaN   NaN   NaN    NaN
2023-08-01 10:32:12   NaN   NaN   NaN    NaN
2023-08-01 10:32:18   NaN   NaN   NaN    NaN
2023-08-01 10:32:24   NaN   NaN   NaN    NaN
2023-08-01 10:32:30   NaN   NaN   NaN    NaN
2023-08-01 10:32:36   NaN   NaN   NaN    NaN
2023-08-01 10:32:42   NaN   NaN   NaN    NaN
2023-08-01 10:32:48   NaN   NaN   NaN    NaN
2023-08-01 10:32:54   NaN   NaN   NaN    NaN
2023-08-01 10:33:00   NaN   NaN   NaN    NaN
2023-08-01 10:33:06   NaN   NaN   NaN    NaN
2023-08-01 10:33:12   NaN   NaN   NaN    NaN
2023-08-01 10:33:18   NaN   NaN   NaN    NaN
2023-08-01 10:33:24   NaN   NaN   NaN    NaN
2023-08-01 10:33:30   NaN   NaN   NaN    NaN
2023-08-01 10:33:36   NaN   NaN   NaN    NaN
2023-08-01 10:33:42   NaN   NaN   NaN    NaN
2023-08-01 10:33:48   NaN   NaN   NaN    NaN
2023-08-01 10:33:54   NaN   NaN   NaN    NaN
2023-08-01 10:34:00   NaN   NaN   NaN    NaN
2023-08-01 10:34:06   NaN   NaN   NaN    NaN

If you want an one-liner solution that produces the same result, do this:

ts.resample('6S', closed='left', label='right', loffset='-6s').ohlc()

where I used loffset parameter for shifting. However, this parameter will raise a deprecation warning: FutureWarning: 'loffset' in .resample() and in Grouper() is deprecated.

gsamaras
  • 71,951
  • 46
  • 188
  • 305