简体   繁体   中英

Slicing window on pandas dataframe

I have a pandas dataframe with time-series data in 1-min intervals. Is there a pythonic way to slice my data for every 15 min like this?

a=pd.DataFrame(index=pd.date_range('2017-01-01 00:04','2017-01-01 01:04',freq='1T'))
a['data']=np.arange(61)

for i in range(0,len(a),15):
    print a[i:i+15]

Is there any built in function for this in pandas?

IIUC, use groups and pd.Grouper with freq=15min

for _, g in a.groupby(pd.Grouper(freq='15min')):
    print(g)

Can also do

groups = a.groupby(pd.Grouper(freq='15min'))
list(groups)

Outputs

                     data
2017-01-01 00:04:00     0
2017-01-01 00:05:00     1
2017-01-01 00:06:00     2
2017-01-01 00:07:00     3
2017-01-01 00:08:00     4
2017-01-01 00:09:00     5
2017-01-01 00:10:00     6
2017-01-01 00:11:00     7
2017-01-01 00:12:00     8
2017-01-01 00:13:00     9
2017-01-01 00:14:00    10
                     data
2017-01-01 00:15:00    11
2017-01-01 00:16:00    12
2017-01-01 00:17:00    13
2017-01-01 00:18:00    14
2017-01-01 00:19:00    15
2017-01-01 00:20:00    16
2017-01-01 00:21:00    17
2017-01-01 00:22:00    18
2017-01-01 00:23:00    19
2017-01-01 00:24:00    20
2017-01-01 00:25:00    21
2017-01-01 00:26:00    22
2017-01-01 00:27:00    23
2017-01-01 00:28:00    24
2017-01-01 00:29:00    25
                     data
2017-01-01 00:30:00    26
2017-01-01 00:31:00    27
2017-01-01 00:32:00    28
2017-01-01 00:33:00    29
2017-01-01 00:34:00    30
2017-01-01 00:35:00    31
2017-01-01 00:36:00    32
2017-01-01 00:37:00    33
2017-01-01 00:38:00    34
2017-01-01 00:39:00    35
2017-01-01 00:40:00    36
2017-01-01 00:41:00    37
2017-01-01 00:42:00    38
2017-01-01 00:43:00    39
2017-01-01 00:44:00    40

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