gpt4 book ai didi

python - 在 pandas 中重新采样后进行舍入

转载 作者:行者123 更新时间:2023-12-02 02:14:25 25 4
gpt4 key购买 nike

我有一个包含货币对买价/卖价的文件(片段):

RateDateTime    RateBid RateAsk
2020-01-22 00:00:00.5945505 0.849190 0.849450
2020-01-22 00:00:00.5945526 0.849080 0.849520
2020-01-22 00:00:00.5945537 0.849040 0.849600
2020-01-22 00:00:00.5945552 0.849290 0.849610
2020-01-22 00:00:00.5946302 0.849300 0.849610
2020-01-22 00:00:02.5946370 0.849400 0.849610
2020-01-22 00:00:04.5946408 0.849400 0.849540
2020-01-22 00:00:04.5946432 0.849590 0.849730
2020-01-22 00:00:04.5946435 0.849160 0.849730
2020-01-22 00:00:04.5946438 0.849570 0.849940
2020-01-22 00:00:22.5946828 0.849560 0.849940
2020-01-22 00:00:37.5947048 0.849570 0.849940

我编写了这个脚本来提取日期时间、买价和卖价,并找到 30 秒间隔的平均价和第一个买价和卖价:

from datetime import datetime as dt
import pandas as pd
import re

date = []
bid = []
ask = []
time_format = "%Y-%m-%d %H:%M:%S"

with open("2020-01-22_pro_EURGBP.txt", "r") as prices:
next(prices)
for line in prices:
date_string = re.findall("\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}", line)[0]
date_parsed = dt.strptime(date_string, time_format)
date.append(date_parsed)
split = line.split()
bid.append(split[2])
ask.append(split[3])
df = pd.DataFrame({"Bid": bid, "Ask": ask}, dtype="float64", index=date)


mean = df.resample("30S").mean()
first = df.resample("30S").first()
final = pd.merge(mean, first, left_index=True, right_index=True)
final = final.rename(
columns={
"Bid_x": "Average Bid",
"Ask_x": "Average Ask",
"Bid_y": "First Bid",
"Ask_y": "First Ask",
}
)
final = final.rename_axis("Datetime")

print(final)

具有以下输出片段:

                      Average Bid  Average Ask  First Bid  First Ask
Datetime
2020-01-22 00:00:00 0.849325 0.849662 0.84919 0.84945
2020-01-22 00:00:30 0.849570 0.849937 0.84957 0.84994
2020-01-22 00:01:00 0.849501 0.849819 0.84956 0.84994

但是,例如,对于 00:00:00 到 00:00:29.9999999 之间的时间,我希望它向上舍入到 00:00:30 窗口,00:00:30 到 00:00:59.9999999 去到 00:01:00 窗口等等。我不知道如何解决这个问题。

最佳答案

关于python - 在 pandas 中重新采样后进行舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60391027/

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