convert_currency
convert_currency(amount, from_, to='USD', rates=None)Convert amounts between currencies.
Converts monetary amounts from one currency to another using a table of exchange rates. By default it uses the bundled currency_rates snapshot (units per US dollar); pass your own rates for up-to-date or custom figures. Because rates share the USD base, any pair is converted as a cross-rate through USD automatically: a conversion is amount / rate(from) * rate(to). amount, from_ and to are vectorised and recycled, so you can convert a whole column with per-row source currencies at once. Codes are upper-cased and resolved through a small alias map (“RMB” to “CNY”, “EURO” to “EUR”, …), and any unknown code yields missing with a warning rather than a silent wrong number.
The source-currency argument is from_ because from is a Python keyword; R names it from.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| amount | (number, list or Series) | Amounts (text like “$1,200” is salvaged with ensure_numeric()). |
required |
| from_ | str or list of str | Source currency code(s), e.g. “EUR” (case-insensitive). | required |
| to | str or list of str | Target currency code(s). Defaults to “USD”. | 'USD' |
| rates | dict or pandas.DataFrame | None (default) for the bundled snapshot, a dict of units per USD (e.g. {"EUR": 0.92, "GBP": 0.79}), or a data frame with currency and per_usd columns. |
None |
Returns
| Name | Type | Description |
|---|---|---|
| pandas.Series | Converted amounts; unknown currency codes yield missing with a warning. |
See Also
add_currency, list_currencies, currency_rates
Examples
>>> convert_currency(100, "EUR", "USD")
>>> convert_currency([100, 50], from_=["EUR", "GBP"], to="USD")
>>> convert_currency(100, "EUR", "CNY")
>>> convert_currency(100, "EUR", "RMB")