import imfp
# Get parameters and their valid codes
= imfp.imf_parameters("PCPS")
params
# Fetch annual coal price index data
= imfp.imf_dataset(
df ="PCPS",
database_id=["A"], # Annual frequency
freq=["PCOAL"], # Coal prices
commodity=["IX"], # Index
unit_measure=2000,
start_year=2015
end_year )
Datasets
Making a Request
To retrieve data from an IMF database, you’ll need the database ID and any relevant filter parameters. Here’s a basic example using the Primary Commodity Price System (PCPS) database:
This example creates two objects we’ll use in the following sections:
params
: A dictionary of parameters and their valid codesdf
: The retrieved data frame containing our requested data
Decoding Returned Data
When you retrieve data using imfp.imf_dataset
, the returned data frame contains columns that correspond to the parameters you specified in your request. However, these columns use input codes (short identifiers) rather than human-readable descriptions. To make your data more interpretable, you can replace these codes with their corresponding text descriptions using the parameter information from imfp.imf_parameters
, so that codes like “A” (Annual) or “W00” (World) become self-explanatory labels.
For example, suppose we want to decode the freq
(frequency), ref_area
(geographical area), and unit_measure
(unit) columns in our dataset. We’ll merge the parameter descriptions into our data frame:
# Decode frequency codes (e.g., "A" → "Annual")
= df.merge(
df # Select code-description pairs
'freq'][['input_code', 'description']],
params[# Match codes in the data frame
='freq',
left_on# ...to codes in the parameter data
='input_code',
right_on# Keep all data rows
='left'
how=['freq', 'input_code']
).drop(columns={"description": "freq"})
).rename(columns
# Decode geographic area codes (e.g., "W00" → "World")
= df.merge(
df 'ref_area'][['input_code', 'description']],
params[='ref_area',
left_on='input_code',
right_on='left'
how=['ref_area', 'input_code']
).drop(columns={"description":"ref_area"})
).rename(columns
# Decode unit codes (e.g., "IX" → "Index")
= df.merge(
df 'unit_measure'][['input_code', 'description']],
params[='unit_measure',
left_on='input_code',
right_on='left'
how=['unit_measure', 'input_code']
).drop(columns={"description":"unit_measure"}) ).rename(columns
After decoding, the data frame is much more human-interpretable. This transformation makes the data more accessible for analysis and presentation, while maintaining all the original information.
Understanding the Data Frame
Also note that the returned data frame has additional mysterious-looking codes as values in some columns.
Codes in the time_format
column are ISO 8601 duration codes. In this case, “P1Y” means “periods of 1 year.” The unit_mult
column represents the number of zeroes you should add to the value column. For instance, if value is in millions, then the unit multiplier will be 6. If in billions, then the unit multiplier will be 9.