我能够转换刻度数据,也可以使用熊猫的resample
功能对其重新采样。请参见下面的代码。
是否还有任何标准的numpy / pandas / ....功能可以让我回想起high
和low
值出现的时刻?
我希望将这些日期时间作为结果数据框中的两个额外列。
import numpy as np
import pandas as pd
np.random.seed(1)
data = np.random.rand(500)
myRange = pd.date_range('2018-04-09', periods=500, freq='50s')
df = pd.DataFrame(data,myRange)
df.columns = ['price']
dfOHLC = df.price.resample('1h').ohlc()
dfOHLC_resampled = df.resample('2h').agg({'open': 'first',
'high': 'max',
'low': 'min',
'close': 'last',
})
print(dfOHLC)
# open high low close
# 2018-04-09 00:00:00 0.417022 0.988861 0.000114 0.137475
# 2018-04-09 01:00:00 0.139276 0.997323 0.002870 0.121343
# 2018-04-09 02:00:00 0.044552 0.988616 0.012556 0.505662
# 2018-04-09 03:00:00 0.021525 0.976759 0.000402 0.802161
# 2018-04-09 04:00:00 0.572489 0.990472 0.022330 0.990472
# 2018-04-09 05:00:00 0.300248 0.993913 0.018333 0.450087
# 2018-04-09 06:00:00 0.478073 0.989955 0.003018 0.227900
print(dfOHLC_resampled)
# open high low close
# price price price price
# 2018-04-09 00:00:00 0.417022 0.997323 0.000114 0.121343
# 2018-04-09 02:00:00 0.044552 0.988616 0.000402 0.802161
# 2018-04-09 04:00:00 0.572489 0.993913 0.018333 0.450087
# 2018-04-09 06:00:00 0.478073 0.989955 0.003018 0.227900
答案 0 :(得分:1)
在{0.2} +版本中,可以使用Series.idxmax
和
Series.idxmin
:
dfOHLC_resampled = dfOHLC.resample('2h').agg({'open': 'first',
'high': ['max', 'idxmax'],
'low': ['min', 'idxmin'],
'close': 'last',
})
print(dfOHLC_resampled)
open high low \
first max idxmax min
2018-04-09 00:00:00 0.417022 0.997323 2018-04-09 01:00:00 0.000114
2018-04-09 02:00:00 0.044552 0.988616 2018-04-09 02:00:00 0.000402
2018-04-09 04:00:00 0.572489 0.993913 2018-04-09 05:00:00 0.018333
2018-04-09 06:00:00 0.478073 0.989955 2018-04-09 06:00:00 0.003018
close
idxmin last
2018-04-09 00:00:00 2018-04-09 00:00:00 0.121343
2018-04-09 02:00:00 2018-04-09 03:00:00 0.802161
2018-04-09 04:00:00 2018-04-09 05:00:00 0.450087
2018-04-09 06:00:00 2018-04-09 06:00:00 0.227900