Converting currencies
Another quite common preprocessing step you might encounter while working on financial tasks is converting currencies. Imagine you have a portfolio of multiple assets, priced in different currencies and you would like to arrive at a total portfolio’s worth. The simplest example might be American and European stocks.
In this recipe, we show how to easily convert stock prices from USD to EUR. However, the very same steps can be used to convert any pair of currencies.
How to do it…
Execute the following steps to convert stock prices from USD to EUR:
- Import the libraries:
import pandas as pd import yfinance as yf from forex_python.converter import CurrencyRates
- Download Apple’s OHLC prices from January 2020:
df = yf.download("AAPL", start="2020-01-01", end="2020-01-31", progress=False) df = df.drop(columns=["...