gpt4 book ai didi

python - 如何将 5 分钟的 DataFrame 过滤为 15 分钟的 DataFrame?

转载 作者:行者123 更新时间:2023-11-30 22:18:51 25 4
gpt4 key购买 nike

我的 csv 文件中有比特币价格。我的文件每 5 分钟更新一次,并将时间戳设置为 epoc。我已将此数据加载到 pandas DataFrame 中。

我想做的是将这些数据从 5 分钟转换为 15、30、60 等分钟。我需要做的是从起始行添加 x 秒并将文件的其余部分读取到 DataFrame。

需要明确的是,我需要获取时间戳的准确数据。例如:

1006.54999992,1483304400,1006.54999997,1004.00000002,1005.84692623,2686.70823136
1002.69522396,1483305300,1003.50000156,1002.03739724,1003.50000156,1066.56568909
1004.9,1483306200,1004.9,1003.50000155,1003.50000155,4978.96836354

这里是示例数据:

Close,Timestamp,High,Low,Open,Volume
1006.54999992,1483304400,1006.54999997,1004.00000002,1005.84692623,2686.70823136
1005.40527258,1483304700,1006.54999994,1004.00000001,1006.50831019,11553.13463685
1003.50000156,1483305000,1006.54999994,1002.42767301,1005.40527258,24319.95180383
1002.69522396,1483305300,1003.50000156,1002.03739724,1003.50000156,1066.56568909
1001.97782306,1483305600,1002.69522396,1001.97782306,1002.69522396,2074.17726448
1003.50000155,1483305900,1003.50000155,1001.84692611,1001.84692612,3281.67078015
1004.9,1483306200,1004.9,1003.50000155,1003.50000155,4978.96836354
1006.49999618,1483306500,1006.5499955,1003.50000164,1003.50000347,6070.86273057
1006.99999998,1483306800,1007.0,1004.30668523,1004.30668523,723.41389783
1007.98333891,1483307100,1008.151,1006.54999328,1006.99999999,1357.21576969
1008.23099997,1483307400,1008.54999326,1007.0,1007.0,459.99976456
1005.99999956,1483307700,1008.231,1004.33924087,1007.391,6139.66580632
1007.18578657,1483308000,1007.4,1004.79999999,1005.99999939,11867.90775651
1003.9999994,1483308300,1007.18578594,1001.84692611,1007.18578594,27285.53584028
1001.00000001,1483308600,1003.99999997,1000.2,1003.9999991,11068.8150516
1005.99669899,1483308900,1007.40360648,1001.84692611,1001.84692611,13223.84822808
1004.99999988,1483309200,1005.99669893,1003.00000001,1003.14143239,3069.76051701
1004.00000001,1483309500,1005.99669899,1004.00000001,1004.00000001,616.35942426
1004.99999989,1483309800,1005.99669893,1002.55436881,1003.80404142,1519.48804831
1005.0,1483310100,1006.14142953,1003.05841976,1003.05841976,8158.1735214
1004.99999997,1483310400,1005.0,1004.9999999,1005.0,3497.33824251
1004.99999999,1483310700,1005.0,1002.55399997,1004.99999991,7791.517061
1004.99999969,1483311000,1006.99669898,1004.99999968,1004.99999999,8604.25057064
1005.99999949,1483311300,1007.39313634,1004.99999999,1007.39313634,162.26831131
1004.44444427,1483311600,1005.99999991,1001.84362417,1004.99999999,3803.79028496
1004.99999992,1483311900,1005.99999985,1003.85858574,1003.85858574,69939.19414843
1001.00000001,1483312200,1004.99999993,1001.0,1004.99999992,96461.36606918

最佳答案

您需要resamplefirst ,但对于完全相同的值,需要通过 read_csv 中的 dtype=str 将所有值转换为 string :

df = pd.read_csv(file, dtype=str)

df['Timestamp'] = pd.to_datetime(df['Timestamp'], unit='s')

df = (df.set_index('Timestamp')
.resample('15T').first()
.reset_index()
.reindex(columns=df.columns))
df['Timestamp'] = df['Timestamp'].astype(np.int64) // 10**9
print (df)
Close Timestamp High Low Open \
0 1006.54999992 1483304400 1006.54999997 1004.00000002 1005.84692623
1 1002.69522396 1483305300 1003.50000156 1002.03739724 1003.50000156
2 1004.9 1483306200 1004.9 1003.50000155 1003.50000155
3 1007.98333891 1483307100 1008.151 1006.54999328 1006.99999999
4 1007.18578657 1483308000 1007.4 1004.79999999 1005.99999939
5 1005.99669899 1483308900 1007.40360648 1001.84692611 1001.84692611
6 1004.99999989 1483309800 1005.99669893 1002.55436881 1003.80404142
7 1004.99999999 1483310700 1005.0 1002.55399997 1004.99999991
8 1004.44444427 1483311600 1005.99999991 1001.84362417 1004.99999999

Volume
0 2686.70823136
1 1066.56568909
2 4978.96836354
3 1357.21576969
4 11867.90775651
5 13223.84822808
6 1519.48804831
7 7791.517061
8 3803.79028496

关于python - 如何将 5 分钟的 DataFrame 过滤为 15 分钟的 DataFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49255609/

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