{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Module 7\n",
"\n",
"## Video 32: Imports/Exports Data\n",
"**Python for the Energy Industry**\n",
"\n",
"Another use of the CargoTimeSeries endpoint is accessing data on imports and exports. That is the focus of this lesson.\n",
"\n",
"*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.*\n",
"\n",
"We start with the usual config:\n",
"\n",
"[Cargo Time Series documentation.](https://vortechsa.github.io/python-sdk/endpoints/cargo_timeseries/)\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# initial imports\n",
"import pandas as pd\n",
"import numpy as np\n",
"from datetime import datetime\n",
"from dateutil.relativedelta import relativedelta\n",
"import vortexasdk as v\n",
"\n",
"# The cargo unit for the time series (barrels)\n",
"TS_UNIT = 'b'\n",
"\n",
"# The granularity of the time series\n",
"TS_FREQ = 'day'\n",
"\n",
"# datetimes to access last 7 weeks of data\n",
"now = datetime.utcnow()\n",
"seven_weeks_ago = now - relativedelta(weeks=7)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How would we get the last 7 weeks' imports to China?"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Find China ID\n",
"#china = [g.id for g in v.Geographies().search('china').to_list() if 'country' in g.layer]\n",
"china = v.Geographies().search('China',exact_term_match=True)[0]['id']\n",
"\n",
"search_result = v.CargoTimeSeries().search(\n",
" timeseries_frequency=TS_FREQ,\n",
" timeseries_unit=TS_UNIT,\n",
" filter_destinations=china,\n",
" filter_time_min=seven_weeks_ago,\n",
" filter_time_max=now,\n",
" filter_activity=\"unloading_start\",\n",
")\n",
"\n",
"imports_df = search_result.to_df()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" key | \n",
" value | \n",
" count | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2020-12-21 00:00:00+00:00 | \n",
" 617142 | \n",
" 12 | \n",
"
\n",
" \n",
" 1 | \n",
" 2020-12-22 00:00:00+00:00 | \n",
" 10558469 | \n",
" 105 | \n",
"
\n",
" \n",
" 2 | \n",
" 2020-12-23 00:00:00+00:00 | \n",
" 15172418 | \n",
" 116 | \n",
"
\n",
" \n",
" 3 | \n",
" 2020-12-24 00:00:00+00:00 | \n",
" 14449385 | \n",
" 107 | \n",
"
\n",
" \n",
" 4 | \n",
" 2020-12-25 00:00:00+00:00 | \n",
" 18163683 | \n",
" 109 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" key value count\n",
"0 2020-12-21 00:00:00+00:00 617142 12\n",
"1 2020-12-22 00:00:00+00:00 10558469 105\n",
"2 2020-12-23 00:00:00+00:00 15172418 116\n",
"3 2020-12-24 00:00:00+00:00 14449385 107\n",
"4 2020-12-25 00:00:00+00:00 18163683 109"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"imports_df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How about the last 7 weeks' exports from Saudi Arabia?"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Find Saudia Arabia ID\n",
"saudi = v.Geographies().search('Saudi Arabia',exact_term_match=True)[0]['id']\n",
"\n",
"search_result = v.CargoTimeSeries().search(\n",
" timeseries_frequency=TS_FREQ,\n",
" timeseries_unit=TS_UNIT,\n",
" filter_origins=saudi,\n",
" filter_time_min=seven_weeks_ago,\n",
" filter_time_max=now,\n",
" filter_activity=\"loading_end\",\n",
")\n",
"\n",
"exports_df = search_result.to_df()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" key | \n",
" value | \n",
" count | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2020-12-21 00:00:00+00:00 | \n",
" 1936697 | \n",
" 5 | \n",
"
\n",
" \n",
" 1 | \n",
" 2020-12-22 00:00:00+00:00 | \n",
" 10705828 | \n",
" 27 | \n",
"
\n",
" \n",
" 2 | \n",
" 2020-12-23 00:00:00+00:00 | \n",
" 13049808 | \n",
" 21 | \n",
"
\n",
" \n",
" 3 | \n",
" 2020-12-24 00:00:00+00:00 | \n",
" 8273078 | \n",
" 19 | \n",
"
\n",
" \n",
" 4 | \n",
" 2020-12-25 00:00:00+00:00 | \n",
" 8510482 | \n",
" 25 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" key value count\n",
"0 2020-12-21 00:00:00+00:00 1936697 5\n",
"1 2020-12-22 00:00:00+00:00 10705828 27\n",
"2 2020-12-23 00:00:00+00:00 13049808 21\n",
"3 2020-12-24 00:00:00+00:00 8273078 19\n",
"4 2020-12-25 00:00:00+00:00 8510482 25"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"exports_df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise\n",
"\n",
"Isolate the last 7 weeks' exports from Saudi Arabia that are imported to China."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}