Python for the Energy Industry
Another use of the CargoTimeSeries endpoint is accessing data on imports and exports. That is the focus of this lesson.
Note: in the video we use loading_state and unloading_state to filter exports and imports. It is more accurate to use loading_end and unloading_start timestamp filters, as reflected in this notebook.
We start with the usual config:
# initial imports
import pandas as pd
import numpy as np
from datetime import datetime
from dateutil.relativedelta import relativedelta
import vortexasdk as v
# The cargo unit for the time series (barrels)
TS_UNIT = 'b'
# The granularity of the time series
TS_FREQ = 'day'
# datetimes to access last 7 weeks of data
now = datetime.utcnow()
seven_weeks_ago = now - relativedelta(weeks=7)
How would we get the last 7 weeks' imports to China?
# Find China ID
#china = [g.id for g in v.Geographies().search('china').to_list() if 'country' in g.layer]
china = v.Geographies().search('China',exact_term_match=True)[0]['id']
search_result = v.CargoTimeSeries().search(
timeseries_frequency=TS_FREQ,
timeseries_unit=TS_UNIT,
filter_destinations=china,
filter_time_min=seven_weeks_ago,
filter_time_max=now,
filter_activity="unloading_start",
)
imports_df = search_result.to_df()
imports_df.head()
key | value | count | |
---|---|---|---|
0 | 2020-12-21 00:00:00+00:00 | 617142 | 12 |
1 | 2020-12-22 00:00:00+00:00 | 10558469 | 105 |
2 | 2020-12-23 00:00:00+00:00 | 15172418 | 116 |
3 | 2020-12-24 00:00:00+00:00 | 14449385 | 107 |
4 | 2020-12-25 00:00:00+00:00 | 18163683 | 109 |
How about the last 7 weeks' exports from Saudi Arabia?
# Find Saudia Arabia ID
saudi = v.Geographies().search('Saudi Arabia',exact_term_match=True)[0]['id']
search_result = v.CargoTimeSeries().search(
timeseries_frequency=TS_FREQ,
timeseries_unit=TS_UNIT,
filter_origins=saudi,
filter_time_min=seven_weeks_ago,
filter_time_max=now,
filter_activity="loading_end",
)
exports_df = search_result.to_df()
exports_df.head()
key | value | count | |
---|---|---|---|
0 | 2020-12-21 00:00:00+00:00 | 1936697 | 5 |
1 | 2020-12-22 00:00:00+00:00 | 10705828 | 27 |
2 | 2020-12-23 00:00:00+00:00 | 13049808 | 21 |
3 | 2020-12-24 00:00:00+00:00 | 8273078 | 19 |
4 | 2020-12-25 00:00:00+00:00 | 8510482 | 25 |
Isolate the last 7 weeks' exports from Saudi Arabia that are imported to China.