I have the following MultiIndex
created with df.groupby(...).resample()
. It is stock market-like OHLC data grouped by an asset and then having OHLC candle time-series for this asset.
high low close ... avg_trade buys sells
pair timestamp ...
AAVE-ETH 2020-01-01 80.0 80.0 80.0 ... 1280.0 1 0
2020-01-02 96.0 96.0 96.0 ... 1120.0 1 0
ETH-USDC 2020-01-02 1600.0 1600.0 1600.0 ... 5000.0 1 0
2020-01-05 1620.0 1400.0 1400.0 ... 1125.0 1 1
The df.index
content is:
MultiIndex([('AAVE-ETH', '2020-01-01'),
('AAVE-ETH', '2020-01-02'),
('ETH-USDC', '2020-01-02'),
('ETH-USDC', '2020-01-05')],
names=['pair', 'timestamp'])
I would like to do a DataFrame.truncate()
like operation by the second index (timestamp) so that I discard all entries beyond a certain timestamp.
However the naive df.truncate(timestamp)
will give an error:
TypeError: Level type mismatch: 2020-01-04 00:00:00
Can a grouped data frame be truncated by its second index (time series) somehow?