Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Python wrapper for the U.S. Energy Information Administration (EIA) API v2

License

NotificationsYou must be signed in to change notification settings

philsv/myeia

Repository files navigation

PyPI versionLicense: MITWeekly DownloadsMonthly DownloadsDownloads

myeia is a simple Python wrapper for the U.S. Energy Information Administration (EIA) APIv2. It is designed to be simple to use and to provide a consistent interface for accessing EIA data.

Installation

pip install myeia

Requirements

  • backoff
  • pandas
  • python-dateutil
  • python-dotenv
  • requests

eia OPEN DATA Registration

To obtain an API Key you need to register on theEIA website.

eia API Query Browser

To find all EIA Datasets visitAPI Dashboard.

How to use

frommyeiaimportAPIeia=API()

Environment Variables

# Create a .env file in your projects root directorytouch .env

By default theAPI class will look for yourEIA_TOKEN.

If you have registered for an API key you can set it in your.env file.

EIA_TOKEN=YOUR_TOKEN_HERE

Get Series

Lets look at an example of how to get theEIA Natural Gas Futures.You can use the simpler v1 API method where you only need to pass theseries_id or you can use the newer v2 API method where you need to pass theroute,series, andfrequency.

df=eia.get_series(series_id="NG.RNGC1.D")df=eia.get_series_via_route(route="natural-gas/pri/fut",series="RNGC1",frequency="daily",)df.head()

Output Example:

            Natural Gas Futures Contract 1 (Dollars per Million Btu)Date2022-09-13                                              8.2842022-09-12                                              8.2492022-09-09                                              7.9962022-09-08                                              7.9152022-09-07                                              7.842...                                                       ...

Different Facets

Lets look at another example theTotal OPEC Petroleum Supply where the facet is available asseriesId. By Default it is set asseries but we can define the facet asseriesId.

df=eia.get_series(series_id="STEO.PAPR_OPEC.M")df=eia.get_series_via_route(route="steo",series="PAPR_OPEC",frequency="monthly",facet="seriesId",)df.head()

Output Example:

            Total OPEC Petroleum SupplyDate2023-12-01                    34.5173142023-11-01                    34.4403972023-10-01                    34.3769712023-09-01                    34.4162422023-08-01                    34.451823...                                 ...

Filter by multiple facets

You can also filter by multiple facets. Lets look at theUAE Crude oil, NGPL, and other liquids where the facets we choose arecountryRegionId andproductId.The difference here is that both facet columns are present in the dataframe, unlike the previous examples where only one facet was present.

df=eia.get_series_via_route(route="international",series=["ARE",55],frequency="monthly",facet=["countryRegionId","productId"],)df.head()

Output Example:

           countryRegionId productId  Crude oil, NGPL, and other liquidsDate                                                            2024-03-01             ARE        55                         4132.3943342024-02-01             ARE        55                         4132.3943342024-01-01             ARE        55                         4142.3943342023-12-01             ARE        55                         4082.3943342023-11-01             ARE        55                         4082.394334...                    ...       ...                                 ...

Get Multiple Series

For multiple series you have to loop through the series and append the data to a list.

data= []foritemin ["RNGC1","RNGC2"]:df=eia.get_series_via_route(route="natural-gas/pri/fut",series=item,frequency="daily",facet="series",    )data.append(df)df=pd.concat(data,axis=1)df.head()

Output Example:

            Natural Gas Futures Contract 1 (Dollars per Million Btu)  Natural Gas Futures Contract 2 (Dollars per Million Btu)Date                                                                                                                  2023-08-29                                              2.556                                                     2.662   2023-08-28                                              2.579                                                     2.665   2023-08-25                                              2.540                                                     2.657   2023-08-24                                              2.519                                                     2.636   2023-08-23                                              2.497                                                     2.592   ...                                                       ...                                                       ...

Define a Start and End Date

You can define a start and end date for your query.

df=eia.get_series(series_id="NG.RNGC1.D",start_date="2021-01-01",end_date="2021-01-31",)df.head()

Output Example:

            Natural Gas Futures Contract 1 (Dollars per Million Btu)Date                                                        2021-01-29                                              2.564   2021-01-28                                              2.664   2021-01-27                                              2.760   2021-01-26                                              2.656   2021-01-25                                              2.602   ...                                                       ...

This also works for theget_series_via_route method.

df=eia.get_series_via_route(route="natural-gas/pri/fut",series="RNGC1",frequency="daily",start_date="2021-01-01",end_date="2021-01-31",)df.head()

Output Example:

            Natural Gas Futures Contract 1 (Dollars per Million Btu)Date2021-01-29                                              2.5642021-01-28                                              2.6642021-01-27                                              2.7602021-01-26                                              2.6562021-01-25                                              2.602...                                                       ...

Contributing

We love your input! We want to make contributing to this project as easy and transparent as possible.Read ourCONTRIBUTING.md to get started.

About

Python wrapper for the U.S. Energy Information Administration (EIA) API v2

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp