简体   繁体   中英

Slicing Pandas Dataframe by minutes

I have a dataframe like this:

2014-01-17 15:03:55.073616,description,53.5,61.8
2014-01-17 15:03:55.789405,description,54.0,62.4
2014-01-17 15:03:56.604489,description,54.2,62.5
2014-01-17 15:03:57.345481,description,54.2,62.5
2014-01-17 15:03:58.072992,description,54.3,62.6
2014-01-17 15:03:58.805325,description,54.6,62.9
2014-01-17 15:03:59.585869,description,57.3,65.4
2014-01-17 15:04:00.292370,description,57.3,65.4
2014-01-17 15:04:01.030217,description,57.1,65.2
2014-01-17 15:04:01.836544,description,57.1,65.2
2014-01-17 15:04:02.559560,description,56.7,64.9
2014-01-17 15:04:03.259607,description,56.7,64.9
2014-01-17 15:04:03.968458,description,56.2,64.4
2014-01-17 15:04:04.695971,description,56.3,64.5
2014-01-17 15:04:05.447393,description,56.3,64.5
...

I would like to slice it by minutes , for example a slice between the third minute and the fifth, looking at the doc it seems that I would have to use searchsorted , but I don't want to be providing the entire date everytime, since my dataframe only contains hourly data, I would just like to slice using an int for start/end minutes .

thanks in advance

Assuming the datetime is the index, you can access the minute:

# df1 = pd.read_csv('foo.csv', sep=',', header=None, parse_dates=[0], index_col=0)

In [11]: df1.index.minute
Out[11]: array([3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4], dtype=int32)

And grab those between 3rd and 5th minute:

In [12]: df1.iloc[(3 <= df1.index.minute) & (df1.index.minute < 5)]
Out[12]: 
                                      1     2     3
0                                                  
2014-01-17 15:03:55.073616  description  53.5  61.8
2014-01-17 15:03:55.789405  description  54.0  62.4
2014-01-17 15:03:56.604489  description  54.2  62.5
2014-01-17 15:03:57.345481  description  54.2  62.5
2014-01-17 15:03:58.072992  description  54.3  62.6
2014-01-17 15:03:58.805325  description  54.6  62.9
2014-01-17 15:03:59.585869  description  57.3  65.4
2014-01-17 15:04:00.292370  description  57.3  65.4
2014-01-17 15:04:01.030217  description  57.1  65.2
2014-01-17 15:04:01.836544  description  57.1  65.2
2014-01-17 15:04:02.559560  description  56.7  64.9
2014-01-17 15:04:03.259607  description  56.7  64.9
2014-01-17 15:04:03.968458  description  56.2  64.4
2014-01-17 15:04:04.695971  description  56.3  64.5
2014-01-17 15:04:05.447393  description  56.3  64.5

[15 rows x 3 columns]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM