Finance

Finance-specific data cleaning functions.

janitor.finance.convert_currency(df: pandas.core.frame.DataFrame, column_name: str = None, from_currency: str = None, to_currency: str = None, historical_date: datetime.date = None, make_new_column: bool = False) → pandas.core.frame.DataFrame[source]

Converts a column from one currency to another, with an option to convert based on historical exchange values.

This method mutates the original DataFrame.

Parameters:
  • df – A pandas dataframe.
  • column_name – Name of the new column. Should be a string, in order for the column name to be compatible with the Feather binary format (this is a useful thing to have).
  • from_currency – The base currency to convert from. May be any of: currency_set = {“AUD”, “BGN”, “BRL”, “CAD”, “CHF”, “CNY”, “CZK”, “DKK”, “EUR”, “GBP”, “HKD”, “HRK”, “HUF”, “IDR”, “ILS”, “INR”, “ISK”, “JPY”, “KRW”, “MXN”, “MYR”, “NOK”, “NZD”, “PHP”, “PLN”, “RON”, “RUB”, “SEK”, “SGD”, “THB”, “TRY”, “USD”, “ZAR”}
  • to_currency – The target currency to convert to. May be any of: currency_set = {“AUD”, “BGN”, “BRL”, “CAD”, “CHF”, “CNY”, “CZK”, “DKK”, “EUR”, “GBP”, “HKD”, “HRK”, “HUF”, “IDR”, “ILS”, “INR”, “ISK”, “JPY”, “KRW”, “MXN”, “MYR”, “NOK”, “NZD”, “PHP”, “PLN”, “RON”, “RUB”, “SEK”, “SGD”, “THB”, “TRY”, “USD”, “ZAR”}
  • historical_date – If supplied, get exchange rate on a certain date. If not supplied, get the latest exchange rate. The exchange rates go back to Jan. 4, 1999.
  • make_new_column – Generates new column for converted currency if True, otherwise, converts currency in place.
Setup:
import pandas as pd
import janitor
from datetime import date

data_dict = {
    "a": [1.23452345, 2.456234, 3.2346125] * 3,
    "Bell__Chart": [1/3, 2/7, 3/2] * 3,
    "decorated-elephant": [1/234, 2/13, 3/167] * 3,
    "animals": ["rabbit", "leopard", "lion"] * 3,
    "cities": ["Cambridge", "Shanghai", "Basel"] * 3,
}

example_dataframe = pd.DataFrame(data_dict)
Example:Converting a column from one currency to another using rates

from 01/01/2018:

example_dataframe.convert_currency('a', from_currency='USD',
to_currency='EUR', historical_date=date(2018,1,1))
Output:
            a  Bell__Chart  decorated-elephant  animals     cities
0  1.029370     0.333333            0.004274   rabbit  Cambridge
1  2.048056     0.285714            0.153846  leopard   Shanghai
2  2.697084     1.500000            0.017964     lion      Basel
3  1.029370     0.333333            0.004274   rabbit  Cambridge
4  2.048056     0.285714            0.153846  leopard   Shanghai
5  2.697084     1.500000            0.017964     lion      Basel
6  1.029370     0.333333            0.004274   rabbit  Cambridge
7  2.048056     0.285714            0.153846  leopard   Shanghai
8  2.697084     1.500000            0.017964     lion      Basel
janitor.finance.inflate_currency(df: pandas.core.frame.DataFrame, column_name: str = None, country: str = None, currency_year: int = None, to_year: int = None, make_new_column: bool = False) → pandas.core.frame.DataFrame[source]

Inflates a column of monetary values from one year to another, based on the currency’s country.

The provided country can be any economy name or code from the World Bank list of economies: https://databank.worldbank.org/data/download/site-content/CLASS.xls.

This method mutates the original DataFrame.

Functional usage example:

import pandas as pd
import janitor.finance

df = pd.DataFrame(...)

df = janitor.finance.inflate_currency(
    df=df,
    column_name='profit',
    country='USA',
    currency_year=2015,
    to_year=2018,
    make_new_column=True
)

Method chaining usage example:

import pandas as pd
import janitor.finance

df = pd.DataFrame(...)
df = df.inflate_currency(
    column_name='profit',
    country='USA',
    currency_year=2015,
    to_year=2018,
    make_new_column=True
)
Parameters:
  • df – A pandas dataframe.
  • column_name – Name of the column containing monetary values to inflate.
  • country – The country associated with the currency being inflated. May be any economy or code from the World Bank list of economies: https://databank.worldbank.org/data/download/site-content/CLASS.xls.
  • currency_year – The currency year to inflate from. The year should be 1960 or later.
  • to_year – The currency year to inflate to. The year should be 1960 or later.
  • make_new_column – Generates new column for inflated currency if True, otherwise, inflates currency in place.