Reputation: 75
I have the following dataframe:
df1:
Revenue Earnings Date
Year
2017 43206832000 4608790000 2017-01-01
2018 43462740000 8928258000 2018-01-01
2019 44268171000 5001014000 2019-01-01
2020 43126472000 4770527000 2020-01-01
I am using an api to get the excahnge currency, the api is CurrencyConverter, the link is: https://pypi.org/project/CurrencyConverter/
I am trying to add a column to my dataframe to show me the exchange rate of that date, I used the method:
c.convert(100, 'EUR', 'USD', date=date(2013, 3, 21))
My code is:
c = CurrencyConverter()
earnings['exchange_rate'] = c.convert(1, 'BRL', 'USD', earnings['Date'])
print(earnings)
I get an answer that says:
TypeError: 'Series' objects are mutable, thus they cannot be hashed
I would like to get the following:
Revenue Earnings Date exchange_rate
Year
2017 43206832000 4608790000 2017-01-01 0.305
2018 43462740000 8928258000 2018-01-01 0.305
2019 44268171000 5001014000 2019-01-01 0.295
2020 43126472000 4770527000 2020-01-01 0.249
Upvotes: 2
Views: 240
Reputation: 195448
Try:
from currency_converter import CurrencyConverter
# if "Date" column isn't already converted:
df["Date"] = pd.to_datetime(df["Date"])
c = CurrencyConverter(fallback_on_missing_rate=True) # without fallback_on_missing_rate=True I get `BRL has no rate for 2017-01-01` error.
df["exchange_rate"] = df["Date"].apply(lambda x: c.convert(1, "BRL", "USD", x))
print(df)
Prints:
Revenue Earnings Date exchange_rate
Year
2017 43206832000 4608790000 2017-01-01 0.306034
2018 43462740000 8928258000 2018-01-01 0.304523
2019 44268171000 5001014000 2019-01-01 0.258538
2020 43126472000 4770527000 2020-01-01 0.249114
Upvotes: 1