2

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.