- Is there a way to offset the pd.resample by 1 period (1Min in my case)?
- I have both the minute data and tick data for a particular stock symbol
- If I
pd.resample('1Min').agg({..})
I get the accurate conversion of open, high, low, close and volume from the tick data However, the DatetimeIndex is off by 1Min. I imagine this is caused by what pandas considered the start-end resample period
ohlcv_df = data.resample('1Min')['last'].agg( {'open_p': 'first', 'high_p': 'max', 'low_p': 'min', 'close_p': 'last'}) ohlcv_df['tot_vlm'] = data.resample('1Min')['tot_vlm'].agg({'tol_vlm': 'last'}) ohlcv_df['prd_vlm'] = data.resample('1Min')['last_sz'].sum() ohlcv_df['num_trds'] = 0 ohlcv_df['symbol'] = self.symbol ohlcv_df['date'] = ohlcv_df.index ohlcv_df['date'] = ohlcv_df.date.apply(lambda x: x.date())
Asked
Active
Viewed 20 times
0

Karun
- 561
- 7
- 23
-
Welcome to SO! Please take a moment to read about how to post pandas questions: http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – YOLO Jan 21 '20 at 16:52
-
1Without sample data it's difficult to fully understand what's going on here. My guess is that you're looking for something like `df.resample('1Min', label='right')...`? You don't seem to want an offset as much as you need to select the proper label for each bin. – ALollz Jan 21 '20 at 16:54
-
@ALollz that did the trick! Tickdata is rather large so didn't embed the dataframe data. If you provide that as the answer I can mark it answered. Thx – Karun Jan 21 '20 at 16:58
-
@Karun That's fine, but often you can create sample data that illustrates the problem with a few lines. Something like `df = pd.DataFrame(data=np.random.randint(0,100,100), index=pd.date_range('2010-01-01', freq='5s', periods=100))` would be enough to illustrate the resampling gets labeled with the start, though you'd want the end. – ALollz Jan 21 '20 at 17:01