python - 如何在获取历史汇率的同时加快货币兑换速度

标签 python pandas dataframe google-colaboratory

我需要一些帮助来思考这个问题:

我有一个包含 61K 条服务记录的数据集。每项服务都会在特定日期续订,每项服务也会产生费用,并且费用金额以 10 种不同货币之一计费。

我需要对每条服务记录执行的操作是将每项服务成本转换为服务续订日期的加元货币。

当我在包含 6 个服务的小样本数据集中执行此操作时,需要 3 秒,但这意味着如果我在 61k 记录数据集上执行此操作,可能需要超过 8 小时,这太长了(我认为我可以在 Excel 或 Google Sheets 中执行此操作要快得多,但我不想这样做)

是否有更好的方法或方法可以在 google colab 中使用 pandas/python 来执行此操作,这样就不需要那么长时间?

提前谢谢

# setup
import pandas as pd
!pip install forex-python
from forex_python.converter import CurrencyRates

#sample dataset/df

dummy_data = {
        'siteid': ['11', '12', '13', '41', '42','51'],
        'userid': [0,0,0,0,0,0],
        'domain': ['A',  'B',  'C',  'E',  'F', 'G'],
        'currency':['MXN',  'CAD',  'USD',  'USD',  'AUD', 'HKD'],
        'servicecost': [2.5,  3.3,  1.3,  2.5,  2.5, 2.3],
        'date': ['2022-02-04',  '2022-03-05',  '2022-01-03',  '2021-04-06',  '2022-12-05', '2022-11-01']
        }
df = pd.DataFrame(dummy_data, columns = ['siteid', 'userid', 'domain','currency','servicecost','date'])

#ensure date is in the proper datatype
df['date'] = pd.to_datetime(df['date'],errors='coerce')

#go through df, get the data to do the conversion and populate a new series
def convertServiceCostToCAD(currency,servicecost,date):
  return CurrencyRates().convert(currency, 'CAD', servicecost, date)
df['excrate']=list(map(convertServiceCostToCAD, df['currency'], df['servicecost'], df['date']))

最佳答案

所以,如果我理解正确的话,这个包的作用就是提供两种货币之间的每日固定汇率(因此一个方向是另一个方向的反方向)。

导致事情如此缓慢的原因很明显是对包方法的调用。对我来说,每次通话大约 4 秒。

您总是有兴趣了解货币 x 与 CAD 之间的汇率。

该包有一个方法.get_rates(),它似乎提供与.convert()方法使用的相同信息,但针对一种货币和所有其他货币。

所以你可以做的是:

  1. 收集 DataFrame 中的所有唯一日期
  2. 针对每个日期调用 .get_rates() 并保存结果
  3. 使用结果加上您的金额来计算所需的列

例如如下:

import pandas as pd
from forex_python.converter import CurrencyRates
from tqdm import tqdm  # use 'pip install tqdm' before


df = pd.DataFrame({
    'siteid': ['11', '12', '13', '41', '42', '51'],
    'userid': [0, 0, 0, 0, 0, 0],
    'domain': ['A', 'B', 'C', 'E', 'F', 'G'],
    'currency': ['MXN', 'CAD', 'USD', 'USD', 'AUD', 'HKD'],
    'servicecost': [2.5, 3.3, 1.3, 2.5, 2.5, 2.3],
    'date': ['2022-02-04', '2022-03-05', '2022-01-03', '2021-04-06', '2022-12-05', '2022-11-01']
})

# get rates for all unique dates, added tqdm progress bar to see progress
rates_dict = {date: CurrencyRates().get_rates('CAD', date_obj=pd.to_datetime(date, errors='coerce'))
              for date in tqdm(df['date'].unique())}

# now use these rates to set cost to 1/(CAD to currency_x rate), except when currency is CAD and when servicecost is 0, in those cases just use servicecost
df['excrate'] = df.apply(lambda row: 1.0/rates_dict[row['date']][row['currency']]*row['servicecost'] if row['currency']!='CAD' and row['servicecost'] != 0 else row['servicecost'], axis=1)

print(df)
>   siteid  userid domain currency  servicecost        date   excrate
  0     11       0      A      MXN          2.5  2022-02-04  0.154553
  1     12       0      B      CAD          3.3  2022-03-05  3.300000
  2     13       0      C      USD          1.3  2022-01-03  1.670334
  3     41       0      E      USD          2.5  2021-04-06  3.140874
  4     42       0      F      AUD          2.5  2022-12-05  2.219252
  5     51       0      G      HKD          2.3  2022-11-01  0.380628

这会大大加快速度多少取决于您的数据中有多少个不同的日期。但既然你说原始 DataFrame 有 60k 行,我假设有大量日期多次出现。运行此代码大约需要大约 4 秒 * DataFrame 中唯一日期的数量。

关于python - 如何在获取历史汇率的同时加快货币兑换速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72915550/

相关文章:

python - 在 python 数据框中查找正则表达式

python - __mro__ 与其他双下划线名称有何不同?

python - Pandas - 使用另一个数据框过滤数据框

python - 计算 Pandas 的累积复合返回

python - 如何访问多级 pandas 数据框中的特定值

Python - Pandas - 将特定函数应用于给定级别 - 多索引数据帧

python - Pandas parallel groupBy 消耗大量内存

python - 如何仅通过比较 pandas 中结果不匹配的列来创建数据框?

python - 在 python 中使用正则表达式提取日期

python - Pandas DatetimeIndex 将日期转换为 1970 年