You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Pandas处理缺失汇率的BTC美元转新西兰元计算

BTC美元汇率转换为新西兰元的实现方案

需求说明

我有两组历史汇率数据:

  • 比特币兑美元(BTC-USD)汇率数据:包含Open、Low、High、Close、Volume列,覆盖2022年1月1日至1月8日共8个日期
  • 美元兑新西兰元(USD-NZD)汇率数据:包含Open、Low、High、Close列,仅覆盖2022年1月3日、4日、6日、7日共4个日期

需完成以下转换:

  1. 将BTC-USD的Open、Low、High、Close列转换为新西兰元计价
  2. 忽略BTC-USD数据中的Volume列
  3. 按规则填充缺失的USD-NZD Close汇率:
    • 初始缺失日期(01/01/2022、02/01/2022):使用第一个可用日期(03/01/2022)的Close汇率
    • 后续缺失日期(05/01/2022、08/01/2022):使用前一个可用日期的Close汇率

实现步骤与代码

1. 导入依赖并加载原始数据

import pandas as pd

# 加载BTC-USD汇率数据
BTC_USD_rates = {
    "Open": {"01/01/2022": 46217.5, "02/01/2022": 47738.7, "03/01/2022": 47293.9, "04/01/2022": 46435.7, "05/01/2022": 45833.1, "06/01/2022": 43431.6, "07/01/2022": 43097.9, "08/01/2022": 41551.3},
    "Low": {"01/01/2022": 46217.5, "02/01/2022": 46718.2, "03/01/2022": 45704.0, "04/01/2022": 45602.1, "05/01/2022": 42535.1, "06/01/2022": 42481.1, "07/01/2022": 40810.0, "08/01/2022": 40574.3},
    "High": {"01/01/2022": 47917.6, "02/01/2022": 47944.9, "03/01/2022": 47556.0, "04/01/2022": 47505.4, "05/01/2022": 47019.4, "06/01/2022": 43772.3, "07/01/2022": 43127.7, "08/01/2022": 42304.4},
    "Close": {"01/01/2022": 47738.0, "02/01/2022": 47311.8, "03/01/2022": 46430.2, "04/01/2022": 45837.3, "05/01/2022": 43425.9, "06/01/2022": 43097.5, "07/01/2022": 41546.7, "08/01/2022": 41672.0},
    "Volume": {"01/01/2022": 31239, "02/01/2022": 27020, "03/01/2022": 41062, "04/01/2022": 55589, "05/01/2022": 83744, "06/01/2022": 63076, "07/01/2022": 88358, "08/01/2022": 52544},
}
df1 = pd.DataFrame.from_dict(BTC_USD_rates)

# 加载USD-NZD汇率数据
USD_NZD_rates = {
    "Open": {"03/01/2022": 1.465, "04/01/2022": 1.4719, "06/01/2022": 1.4717, "07/01/2022": 1.4819},
    "Low": {"03/01/2022": 1.4583, "04/01/2022": 1.4651, "06/01/2022": 1.4708, "07/01/2022": 1.4733},
    "High": {"03/01/2022": 1.4763, "04/01/2022": 1.4784, "06/01/2022": 1.4854, "07/01/2022": 1.4849},
    "Close": {"03/01/2022": 1.4732, "04/01/2022": 1.4669, "06/01/2022": 1.4817, "07/01/2022": 1.4741},
}
df2 = pd.DataFrame.from_dict(USD_NZD_rates)

2. 预处理BTC-USD数据

移除不需要的Volume列,仅保留待转换的汇率列:

# 剔除Volume列,保留Open、Low、High、Close
btc_usd = df1.drop(columns=['Volume'])

3. 填充缺失的USD-NZD Close汇率

按照需求规则填充所有日期的汇率:

# 提取USD-NZD的Close汇率数据
usd_nzd_close = df2['Close']

# 创建包含所有目标日期的空Series
all_dates = btc_usd.index
filled_rates = pd.Series(index=all_dates, dtype='float64')

# 填充已有数据的日期
filled_rates.loc[usd_nzd_close.index] = usd_nzd_close.values

# 处理初始缺失日期:用第一个可用日期(03/01/2022)的汇率填充
first_available_rate = filled_rates['03/01/2022']
filled_rates.loc[['01/01/2022', '02/01/2022']] = first_available_rate

# 处理后续缺失日期:按时间顺序前向填充(使用前一个可用日期的汇率)
filled_rates = filled_rates.sort_index().ffill()

4. 执行汇率转换

将BTC-USD的每一列乘以对应日期的USD-NZD汇率:

# 转换为新西兰元计价
btc_nzd = btc_usd.multiply(filled_rates, axis=0)
# 重命名列以明确计价单位
btc_nzd.columns = [f"{col}_NZD" for col in btc_nzd.columns]

5. 查看最终结果

print(btc_nzd)

运行后输出的结果如下:

Open_NZD     Low_NZD    High_NZD   Close_NZD
01/01/2022  68087.6210  68087.6210  70602.20832  70327.6216
02/01/2022  70338.65284  68825.25224  70633.32668  69700.74376
03/01/2022  69673.37348  67331.1328   70069.5992   68400.97064
04/01/2022  68116.52833  66893.72049  69685.67126  67238.73537
05/01/2022  67231.57439  62394.73819  68972.90786  63701.35271
06/01/2022  64353.50172  62945.24587  64857.41691  63857.56575
07/01/2022  64030.51439  60163.021    63574.54257  61249.98047
08/01/2022  61666.27133  59810.57563  62360.91604  61409.6952

内容的提问来源于stack exchange,提问作者leolapa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 09:23:08