library(EIAapi)
library(plotly)
library(tsibble)
Data
In this notebook, we will pull the required time series data from the EIA API and reformat it.
The EIA API is a great resource for time series data. It provides access to various types of time series data from the energy industry. We will use the EIAapi library to pull the following time series:
- Number of natural gas consumers in California
- US monthly demand for natural gas
- Hourly demand for electricity in California by balancing authority subregion PGAE

Load the Libraries
We will use the EIAapi library to pull the required time series from the API and use the plotly library to plot the data, and the tsibble library to reformat the data into time series objects.
API Settings
<- Sys.getenv("EIA_API_KEY") api_key
Number of Natural Gas Consumers in California
Series details:
- Series description: Total number of natural gas consumers in California
- Area: California
- Process: Number of Residential Consumers
- API dashboard link
- API path: ‘natural-gas/cons/num/data/’
- Query schema:
{
"frequency": "annual",
"data": [
"value"
],
"facets": {
"duoarea": [
"SCA"
],
"process": [
"VN3"
]
},
"start": null,
"end": null,
"sort": [
{
"column": "period",
"direction": "desc"
}
],
"offset": 0,
"length": 5000
}
<- eia_get(
ts_1 api_key = api_key,
api_path = "natural-gas/cons/num/data/",
# frequency = "monthly",
data = "value",
facets = list(duoarea = "SCA", process = "VN3")
|>
) ::arrange(period)
dplyr
head(ts_1)
period duoarea area-name product product-name process
1 1986 SCA CALIFORNIA EPG0 Natural Gas VN3
2 1987 SCA CALIFORNIA EPG0 Natural Gas VN3
3 1988 SCA CALIFORNIA EPG0 Natural Gas VN3
4 1989 SCA CALIFORNIA EPG0 Natural Gas VN3
5 1990 SCA CALIFORNIA EPG0 Natural Gas VN3
6 1991 SCA CALIFORNIA EPG0 Natural Gas VN3
process-name series
1 Number of Residential Consumers NA1501_SCA_8
2 Number of Residential Consumers NA1501_SCA_8
3 Number of Residential Consumers NA1501_SCA_8
4 Number of Residential Consumers NA1501_SCA_8
5 Number of Residential Consumers NA1501_SCA_8
6 Number of Residential Consumers NA1501_SCA_8
series-description value units
1 California Natural Gas Number of Residential Consumers (Count) 7626 COUNT
2 California Natural Gas Number of Residential Consumers (Count) 7904858 COUNT
3 California Natural Gas Number of Residential Consumers (Count) 8113034 COUNT
4 California Natural Gas Number of Residential Consumers (Count) 8313776 COUNT
5 California Natural Gas Number of Residential Consumers (Count) 8497848 COUNT
6 California Natural Gas Number of Residential Consumers (Count) 8634774 COUNT
<- ts_1 |>
ts1 ::select(index = period, y = value, series_id = duoarea) |>
dplyras_tsibble(index = index)
head(ts1)
# A tsibble: 6 x 3 [1Y]
index y series_id
<int> <int> <chr>
1 1986 7626 SCA
2 1987 7904858 SCA
3 1988 8113034 SCA
4 1989 8313776 SCA
5 1990 8497848 SCA
6 1991 8634774 SCA
plot_ly(data = ts1, x = ~ index, y = ~ y, type = "scatter", mode = "lines") |>
::layout(title = "Number of Natural Gas Consumers in California",
plotlyyaxis = list(title = "Number of Consumers"),
xaxis = list(title = "Period"))
US Monthly Demand for Natural Gas
Series details:
- Series description: US total monthly demand for natural gas
- Area: US
- Process: Delivered to Consumers
- API dashboard link
- API path: ‘natural-gas/cons/sum/data/’
- Query schema:
{
"frequency": "monthly",
"data": [
"value"
],
"facets": {
"duoarea": [
"NUS"
],
"process": [
"VGT"
]
},
"start": null,
"end": null,
"sort": [
{
"column": "period",
"direction": "desc"
}
],
"offset": 0,
"length": 5000
}
Let’s pull the data:
<- eia_get(
ts_2 api_key = api_key,
api_path = "natural-gas/cons/sum/data/",
frequency = "monthly",
data = "value",
facets = list(duoarea = "NUS", process = "VGT")
|>
) ::arrange(period) dplyr
And reformat it:
<- ts_2 |>
ts2 ::mutate(date = lubridate::ym(period),
dplyrindex = yearmonth(date)) |>
::select(index, date, y = value, series_id = duoarea) |>
dplyr::arrange(date) |>
dplyras_tsibble(index = index)
And plot it:
plot_ly(data = ts2, x = ~ date, y = ~ y, type = "scatter", mode = "lines") |>
::layout(title = "US Monthly Demand for Natural Gas",
plotlyyaxis = list(title = "MMCF"),
xaxis = list(title = "Period"))
Saving the Data
save(ts1, ts2, ts3, file = "./data/ts.RData")