2015-10-08 129 views
4

這裏是一個重複的例子:的Bug大熊貓重新取樣/ TimeGrouper

from pandas import DataFrame, Timestamp, TimeGrouper 

i = [Timestamp('2015-10-07 03:50:01.543999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:02.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:02.180000+0000', tz='UTC'), Timestamp('2015-10-07 03:50:04.380000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:14.744000+0000', tz='UTC'), Timestamp('2015-10-07 03:50:17.380000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:19.860000+0000', tz='UTC'), Timestamp('2015-10-07 03:50:19.996000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:32.823999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:37.867999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:41.956000+0000', tz='UTC'), Timestamp('2015-10-07 03:50:41.956000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:46.584000+0000', tz='UTC'), Timestamp('2015-10-07 03:50:46.584000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:46.584000+0000', tz='UTC'), Timestamp('2015-10-07 03:50:46.584000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:46.584000+0000', tz='UTC'), Timestamp('2015-10-07 03:50:46.584000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:46.584000+0000', tz='UTC'), Timestamp('2015-10-07 03:50:46.828000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:46.828000+0000', tz='UTC'), Timestamp('2015-10-07 03:50:49.047999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:53.668000+0000', tz='UTC'), Timestamp('2015-10-07 03:50:53.668000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:55.675999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:55.675999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:55.675999+0000', tz='UTC'), Timestamp('2015-10-07 03:50:55.675999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:50:55.464000+0000', tz='UTC'), Timestamp('2015-10-07 03:50:57.123999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:02.127999+0000', tz='UTC'), Timestamp('2015-10-07 03:51:02.327999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:07.484000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:07.484000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.504000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.520000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.119999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.119999+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.119999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.119999+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.119999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.119999+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.119999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:08.119999+0000', tz='UTC'), Timestamp('2015-10-07 03:51:08.119999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:15.547999+0000', tz='UTC'), Timestamp('2015-10-07 03:51:15.547999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:15.547999+0000', tz='UTC'), Timestamp('2015-10-07 03:51:16.996000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:16.996000+0000', tz='UTC'), Timestamp('2015-10-07 03:51:23.888000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:24.671999+0000', tz='UTC'), Timestamp('2015-10-07 03:51:26.719999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:51:26.719999+0000', tz='UTC'), Timestamp('2015-10-07 03:51:29.924000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:00.372000+0000', tz='UTC'), Timestamp('2015-10-07 03:52:00.372000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:00.372000+0000', tz='UTC'), Timestamp('2015-10-07 03:52:00.372000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:02.900000+0000', tz='UTC'), Timestamp('2015-10-07 03:52:02.900000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:02.900000+0000', tz='UTC'), Timestamp('2015-10-07 03:52:02.900000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:05.883999+0000', tz='UTC'), Timestamp('2015-10-07 03:52:05.883999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:05.883999+0000', tz='UTC'), Timestamp('2015-10-07 03:52:05.883999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:05.883999+0000', tz='UTC'), Timestamp('2015-10-07 03:52:05.888000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:05.888000+0000', tz='UTC'), Timestamp('2015-10-07 03:52:29.119999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:31.319999+0000', tz='UTC'), Timestamp('2015-10-07 03:52:31.319999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:31.319999+0000', tz='UTC'), Timestamp('2015-10-07 03:52:31.319999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:31.319999+0000', tz='UTC'), Timestamp('2015-10-07 03:52:31.319999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:31.319999+0000', tz='UTC'), Timestamp('2015-10-07 03:52:31.319999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:31.319999+0000', tz='UTC'), Timestamp('2015-10-07 03:52:33.676000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:33.987999+0000', tz='UTC'), Timestamp('2015-10-07 03:52:33.987999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:33.248000+0000', tz='UTC'), Timestamp('2015-10-07 03:52:43.288000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:43.288000+0000', tz='UTC'), Timestamp('2015-10-07 03:52:43.288000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:45.068000+0000', tz='UTC'), Timestamp('2015-10-07 03:52:45.068000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:45.068000+0000', tz='UTC'), Timestamp('2015-10-07 03:52:45.068000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:48.259999+0000', tz='UTC'), Timestamp('2015-10-07 03:52:48.259999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:57.196000+0000', tz='UTC'), Timestamp('2015-10-07 03:52:57.196000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:57.196000+0000', tz='UTC'), Timestamp('2015-10-07 03:52:57.196000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:52:57.196000+0000', tz='UTC'), Timestamp('2015-10-07 03:52:59.743999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:00.244000+0000', tz='UTC'), Timestamp('2015-10-07 03:53:00.248000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:00.356000+0000', tz='UTC'), Timestamp('2015-10-07 03:53:00.380000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:03.012000+0000', tz='UTC'), Timestamp('2015-10-07 03:53:14.055999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:18.447999+0000', tz='UTC'), Timestamp('2015-10-07 03:53:18.447999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:18.472000+0000', tz='UTC'), Timestamp('2015-10-07 03:53:18.472000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:27.259999+0000', tz='UTC'), Timestamp('2015-10-07 03:53:30.831999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:30.831999+0000', tz='UTC'), Timestamp('2015-10-07 03:53:30.831999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:30.831999+0000', tz='UTC'), Timestamp('2015-10-07 03:53:30.831999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:30.831999+0000', tz='UTC'), Timestamp('2015-10-07 03:53:30.840000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:30.840000+0000', tz='UTC'), Timestamp('2015-10-07 03:53:31.631999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:41.776000+0000', tz='UTC'), Timestamp('2015-10-07 03:53:44.119999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:52.319999+0000', tz='UTC'), Timestamp('2015-10-07 03:53:52.319999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:54.239999+0000', tz='UTC'), Timestamp('2015-10-07 03:53:54.243999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:54.243999+0000', tz='UTC'), Timestamp('2015-10-07 03:53:54.243999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:53:54.311999+0000', tz='UTC'), Timestamp('2015-10-07 03:54:02.648000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:02.648000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:02.648000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:02.648000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:02.648000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:02.648000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:02.648000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:02.648000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:02.648000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:02.648000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:04.268000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:04.268000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:04.268000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:05.980000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:08.959999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:08.959999+0000', tz='UTC'), Timestamp('2015-10-07 03:54:09.144000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:09.223999+0000', tz='UTC'), Timestamp('2015-10-07 03:54:09.223999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:09.223999+0000', tz='UTC'), Timestamp('2015-10-07 03:54:10.828000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:10.828000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:10.828000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:10.828000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:10.828000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:10.828000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:10.828000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:10.828000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:12.751999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:15.480000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:15.480000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:15.480000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:15.484000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:15.484000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:15.484000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:15.484000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:15.484000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:15.484000+0000', tz='UTC'), Timestamp('2015-10-07 03:54:16.963999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:16.963999+0000', tz='UTC'), Timestamp('2015-10-07 03:54:17.460000+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:34.519999+0000', tz='UTC'), Timestamp('2015-10-07 03:54:34.519999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:35.319999+0000', tz='UTC'), Timestamp('2015-10-07 03:54:35.319999+0000', tz='UTC'), 
Timestamp('2015-10-07 03:54:35.319999+0000', tz='UTC')] 

p = [1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.5, 
1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 
1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 
1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 
1965.5, 1965.75, 1965.75, 1965.75, 1965.75, 1965.5, 1965.5, 1965.75, 1965.75, 1965.75, 
1965.75, 1965.75, 1965.75, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 
1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 
1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 
1965.5, 1965.5, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 
1965.25, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.75, 1965.75, 1965.5, 
1965.75, 1965.75, 1965.75, 1965.75, 1965.75, 1965.75, 1965.75, 1965.75, 1965.75, 1965.75, 
1965.75, 1965.75, 1965.75, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 
1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 
1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.5, 1965.5, 1965.5, 1965.5, 
1965.5, 1965.75, 1965.5, 1965.75, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 1965.5, 
1965.5, 1965.5, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 
1965.5, 1965.25, 1965.5, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.5, 
1965.0, 1965.0, 1965.0, 1965.0, 1965.0, 1965.0, 1964.75, 1964.75, 1964.75, 1964.75, 
1964.75, 1964.75, 1964.75, 1964.75, 1964.5, 1964.5, 1964.5, 1964.75, 1964.75, 1964.75, 
1965.0, 1965.0, 1965.0, 1965.0, 1965.0, 1965.0, 1965.0, 1965.0, 1965.0, 1965.25, 1965.25, 
1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.25, 1965.0, 1965.0, 1965.25, 
1965.25, 1965.25, 1965.25, 1965.25, 1965.25] 

現在讓我們創建數據框:

df = DataFrame(data=p, index=i, columns=['price']) 

看起來什麼數據,如針對分鐘03:54

df[df.index >= Timestamp('2015-10-07 03:54:00+00:00')].head(12) 

            price 
2015-10-07 03:54:02.648000+00:00 1965.00 
2015-10-07 03:54:02.648000+00:00 1965.00 
2015-10-07 03:54:02.648000+00:00 1965.00 
2015-10-07 03:54:02.648000+00:00 1965.00 
2015-10-07 03:54:02.648000+00:00 1965.00 
2015-10-07 03:54:02.648000+00:00 1965.00 
2015-10-07 03:54:02.648000+00:00 1964.75 
2015-10-07 03:54:02.648000+00:00 1964.75 
2015-10-07 03:54:02.648000+00:00 1964.75 
2015-10-07 03:54:02.648000+00:00 1964.75 
2015-10-07 03:54:04.268000+00:00 1964.75 
2015-10-07 03:54:04.268000+00:00 1964.75 

第一個價格是1965.00。現在,讓我們用大熊貓resample方法創建1分鐘吧:

df.resample(rule='1Min', how='ohlc', closed='left', label='left') 

          price       
           open  high  low close 
2015-10-07 03:50:00+00:00 1965.25 1965.75 1965.25 1965.50 
2015-10-07 03:51:00+00:00 1965.75 1965.75 1965.25 1965.50 
2015-10-07 03:52:00+00:00 1965.75 1965.75 1965.25 1965.75 
2015-10-07 03:53:00+00:00 1965.50 1965.75 1965.25 1965.50 
2015-10-07 03:54:00+00:00 1964.75 1965.25 1964.50 1965.25 

開市價爲1964.75當它應該是1965.0003:54

添加行列顯示問題完美:

df['row'] = range(1, df.shape[0] + 1) 
grouped = df.groupby(TimeGrouper(freq='1Min', closed='left', label='left')) 
grouped.get_group(Timestamp('2015-10-07 03:54:00+00:00')).head(12) 
            price row 
2015-10-07 03:54:02.648000+00:00 1964.75 189 
2015-10-07 03:54:02.648000+00:00 1964.75 188 
2015-10-07 03:54:02.648000+00:00 1964.75 187 
2015-10-07 03:54:02.648000+00:00 1964.75 186 
2015-10-07 03:54:02.648000+00:00 1965.00 185 
2015-10-07 03:54:02.648000+00:00 1965.00 181 
2015-10-07 03:54:02.648000+00:00 1965.00 183 
2015-10-07 03:54:02.648000+00:00 1965.00 182 
2015-10-07 03:54:02.648000+00:00 1965.00 180 
2015-10-07 03:54:02.648000+00:00 1965.00 184 
2015-10-07 03:54:04.268000+00:00 1964.75 190 
2015-10-07 03:54:04.268000+00:00 1964.75 191 

時的時間戳是相同的類TimeGrouper改變行的順序。

臨時解決方案是在應用ohlc轉換之前按行列對每個組進行排序。

感謝您的關注!

+0

大熊貓的什麼版本?爲什麼不使用OHLC方法... –

+0

熊貓版本16.2 與OHLC相同的問題: x ['price']。resample('1Min',closed ='left',label ='left',how ='ohlc ') 2015-10-07 03:54:00 + 00:00 1964.75 1965.50 1964.50 1965.25 – agiap

+0

我不相信所有的熊貓都必須穩定,所以我猜想resample會做一些不穩定的排序?如果是這樣,我想你會需要做一些類似於你所做的事情,並在一些獨特的列上手動排序。 – JohnE

回答

0

以下代碼可能適用於您。

df = DataFrame(data=p, index=i, columns=['price']) 
df = df.reset_index().drop_duplicates(subset='index').set_index('index') 

df.resample(rule='1Min', how='ohlc', closed='left', label='left') 

它表明O/P爲

      price 
          open high low  close 
index    
2015-10-07 03:50:00+00:00 1965.25 1965.75 1965.25 1965.50 
2015-10-07 03:51:00+00:00 1965.75 1965.75 1965.25 1965.50 
2015-10-07 03:52:00+00:00 1965.75 1965.75 1965.25 1965.75 
2015-10-07 03:53:00+00:00 1965.50 1965.75 1965.25 1965.50 
2015-10-07 03:54:00+00:00 1965.00 1965.25 1964.50 1965.25