Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample every nth minute in a minute based datetime column in python

How to select every 5th minute row in a dataframe? If 5th minute is missing then 4th or 3rd would do..

I DO NOT WANT MEAN OR ANY AGGREGATE

I have tried:

df.groupby(pd.TimeGrouper('5Min'))['AUDUSD'].mean()

df.resample('5min', how=np.var).head()

both are not producing desired results..

My Input:

                        DATETIME            AUDUSD
DATETIME        
2019-06-07 00:01:00     2019.06.07 00:01    0.69740
2019-06-07 00:02:00     2019.06.07 00:02    0.69742
2019-06-07 00:03:00     2019.06.07 00:03    0.69742
2019-06-07 00:04:00     2019.06.07 00:04    0.69742
2019-06-07 00:05:00     2019.06.07 00:05    0.69739
2019-06-07 00:06:00     2019.06.07 00:06    0.69740
2019-06-07 00:07:00     2019.06.07 00:07    0.69739
2019-06-07 00:08:00     2019.06.07 00:08    0.69740
2019-06-07 00:11:00     2019.06.07 00:11    0.69741
2019-06-07 00:12:00     2019.06.07 00:12    0.69741
2019-06-07 00:13:00     2019.06.07 00:13    0.69740
2019-06-07 00:14:00     2019.06.07 00:14    0.69740
2019-06-07 00:15:00     2019.06.07 00:15    0.69754
2019-06-07 00:16:00     2019.06.07 00:16    0.69749
2019-06-07 00:17:00     2019.06.07 00:17    0.69752
2019-06-07 00:18:00     2019.06.07 00:18    0.69753
2019-06-07 00:19:00     2019.06.07 00:19    0.69758
2019-06-07 00:20:00     2019.06.07 00:20    0.69763
2019-06-07 00:21:00     2019.06.07 00:21    0.69764
2019-06-07 00:23:00     2019.06.07 00:23    0.69765
2019-06-07 00:28:00     2019.06.07 00:28    0.69763

Desired Output:

                        DATETIME            AUDUSD
DATETIME        
2019-06-07 00:05:00     2019.06.07 00:05    0.69739
2019-06-07 00:10:00     2019.06.07 00:08    0.69740
2019-06-07 00:15:00     2019.06.07 00:15    0.69754
2019-06-07 00:20:00     2019.06.07 00:20    0.69763
2019-06-07 00:25:00     2019.06.07 00:23    0.69765
2019-06-07 00:30:00     2019.06.07 00:28    0.69763
like image 359
VjSwamy Avatar asked Jun 16 '26 01:06

VjSwamy


1 Answers

This works for me, except i used first as i don't know what method your using:

df.set_index(pd.DatetimeIndex(df['DATETIME']))  

df.set_index(pd.DatetimeIndex(df['DATETIME'])).resample("5T").agg('first')                                                                                                          

Out[2649]: 
                             DATETIME   AUDUSD
DATETIME                                      
2019-06-07 00:00:00  2019.06.07 00:01  0.69740
2019-06-07 00:05:00  2019.06.07 00:05  0.69739
2019-06-07 00:10:00  2019.06.07 00:11  0.69741
2019-06-07 00:15:00  2019.06.07 00:15  0.69754
2019-06-07 00:20:00  2019.06.07 00:20  0.69763
2019-06-07 00:25:00  2019.06.07 00:28  0.69763

like image 86
oppressionslayer Avatar answered Jun 18 '26 16:06

oppressionslayer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!