Reputation: 167
I'm trying to convert a column in my Pandas DataFrame from USD to EUR. Each entry has a different date so needs a different conversion rate. The problem is that the DataFrame contains over 200 million rows so it should be as efficient as possible. Does anyone have an idea how to do this efficiently? I tried this, but it seems to be extremely slow:
from easymoney.money import EasyPeasy
ep = EasyPeasy()
df['money_eur'] = df.apply(lambda x: ep.currency_converter(x['money_usd'], 'USD', 'EUR', date=x['date'].strftime('%d/%m/%Y')), axis=1)
Upvotes: 0
Views: 604
Reputation: 167
Found a solution! Precompute a database with all exchange rates for each day. Use a left merge for the df and the exchange rates df. Now the USD column can be easily used.
df = df.merge(exchangeRatesCurrencies, how='left', left_on='date_days', right_on='date_exchange')
df['money_eur'] = df['money_usd'] / df['USD']
Upvotes: 1