gpt4 book ai didi

python - Pandas 重采样/TimeGrouper 中的错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:42 27 4
gpt4 key购买 nike

这是一个可重现的例子:

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]

现在让我们创建DataFrame:

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。现在让我们使用 pandas 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,而在 03:54 应为 1965.00

添加行列完美地显示了问题:

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 转换之前按行列对每个组进行排序。

感谢您的关注!

最佳答案

以下代码可能适合您。

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

关于python - Pandas 重采样/TimeGrouper 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33022480/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com