|
| 1 | +--- |
| 2 | +name: eia |
| 3 | +description: Query U.S. energy data (EIA API v2). Use when the user asks about U.S. electricity, petroleum, natural gas, or coal data from the EIA. |
| 4 | +version: 1.0.0 |
| 5 | +--- |
| 6 | + |
| 7 | +# EIA Data Assistant |
| 8 | + |
| 9 | +You have access to the `python-eia` CLI and library for querying the U.S. Energy Information Administration (EIA) API v2. |
| 10 | + |
| 11 | +The EIA API is a **route-based tree**. You navigate routes to find data endpoints, then query them with facet filters and frequency options. |
| 12 | + |
| 13 | +## CLI Reference |
| 14 | + |
| 15 | +### Explore the API tree |
| 16 | + |
| 17 | +```bash |
| 18 | +# Top-level routes |
| 19 | +eia routes |
| 20 | + |
| 21 | +# Navigate deeper |
| 22 | +eia routes electricity |
| 23 | +eia routes electricity/rto |
| 24 | +eia routes electricity/rto/fuel-type-data # Error if leaf — use 'eia meta' instead |
| 25 | +``` |
| 26 | + |
| 27 | +### Inspect a data endpoint |
| 28 | + |
| 29 | +```bash |
| 30 | +# Show facets, frequencies, and data columns |
| 31 | +eia meta electricity/rto/fuel-type-data |
| 32 | +eia meta petroleum/pri/spt |
| 33 | +eia meta natural-gas/pri/sum |
| 34 | +``` |
| 35 | + |
| 36 | +### List facet values |
| 37 | + |
| 38 | +```bash |
| 39 | +# List all values for a facet |
| 40 | +eia facets electricity/rto/fuel-type-data respondent |
| 41 | +eia facets electricity/rto/fuel-type-data fueltype |
| 42 | +eia facets petroleum/pri/spt series --format csv |
| 43 | +``` |
| 44 | + |
| 45 | +### Fetch data |
| 46 | + |
| 47 | +```bash |
| 48 | +# Basic query |
| 49 | +eia get electricity/rto/fuel-type-data \ |
| 50 | + --start 2024-06-01 --end 2024-06-08 \ |
| 51 | + --frequency hourly \ |
| 52 | + --facet respondent=CISO \ |
| 53 | + --data value |
| 54 | + |
| 55 | +# Multiple facet values (repeat --facet) |
| 56 | +eia get electricity/retail-sales \ |
| 57 | + --start 2024-01-01 --end 2024-12-31 \ |
| 58 | + --facet stateid=CA --facet sectorid=RES --facet sectorid=COM \ |
| 59 | + --data revenue --data sales |
| 60 | + |
| 61 | +# Export to CSV |
| 62 | +eia get petroleum/pri/spt --start 2024-01-01 --end 2024-06-01 \ |
| 63 | + --format csv --output prices.csv |
| 64 | + |
| 65 | +# Sort |
| 66 | +eia get electricity/rto/fuel-type-data \ |
| 67 | + --start 2024-06-01 --end 2024-06-02 \ |
| 68 | + --frequency hourly --facet respondent=CISO --data value \ |
| 69 | + --sort period --sort-dir desc |
| 70 | +``` |
| 71 | + |
| 72 | +### Fetch + eval pandas expression |
| 73 | + |
| 74 | +```bash |
| 75 | +# Descriptive stats |
| 76 | +eia exec electricity/rto/fuel-type-data \ |
| 77 | + --start 2024-06-01 --end 2024-06-08 \ |
| 78 | + --frequency hourly --facet respondent=CISO --data value \ |
| 79 | + -x "df.describe()" |
| 80 | + |
| 81 | +# Group by fuel type |
| 82 | +eia exec electricity/rto/fuel-type-data \ |
| 83 | + --start 2024-06-01 --end 2024-06-08 \ |
| 84 | + --frequency hourly --facet respondent=CISO --data value \ |
| 85 | + -x "df.groupby('fueltype')['value'].mean()" |
| 86 | + |
| 87 | +# Daily aggregation |
| 88 | +eia exec natural-gas/pri/sum \ |
| 89 | + --start 2024-01-01 --end 2024-06-01 \ |
| 90 | + -x "df.groupby('process')['value'].mean().sort_values(ascending=False)" |
| 91 | +``` |
| 92 | + |
| 93 | +### Output options (all commands) |
| 94 | + |
| 95 | +``` |
| 96 | +--format table|csv|json (default: table) |
| 97 | +--output file.csv (write to file instead of stdout) |
| 98 | +``` |
| 99 | + |
| 100 | +## Common Routes |
| 101 | + |
| 102 | +| Route | Description | |
| 103 | +|-------|-------------| |
| 104 | +| `electricity/rto/fuel-type-data` | Real-time grid generation by fuel type | |
| 105 | +| `electricity/rto/region-data` | Real-time grid demand/generation by region | |
| 106 | +| `electricity/rto/interchange-data` | Real-time interchange between regions | |
| 107 | +| `electricity/retail-sales` | Retail electricity sales (monthly/annual) | |
| 108 | +| `electricity/electric-power-operational-data` | Power plant operational data | |
| 109 | +| `petroleum/pri/spt` | Spot petroleum prices (crude, gasoline, etc.) | |
| 110 | +| `petroleum/sum/sndw` | Weekly petroleum supply/demand | |
| 111 | +| `natural-gas/pri/sum` | Natural gas prices summary | |
| 112 | +| `natural-gas/sum/lsum` | Natural gas supply/demand summary | |
| 113 | +| `coal/shipments/receipts` | Coal shipments and receipts | |
| 114 | +| `total-energy/data` | Total energy overview (monthly/annual) | |
| 115 | + |
| 116 | +Use `eia routes` to discover more. Use `eia meta <route>` to see exact facets and frequencies. |
| 117 | + |
| 118 | +## Facet Conventions |
| 119 | + |
| 120 | +- **Format**: `--facet key=value` (repeatable) |
| 121 | +- **Multiple values**: repeat the flag — `--facet sectorid=RES --facet sectorid=COM` |
| 122 | +- **Common facets**: `respondent` (grid operator), `fueltype`, `stateid`, `sectorid`, `series` |
| 123 | +- Each endpoint has different facets — use `eia meta` and `eia facets` to discover them |
| 124 | + |
| 125 | +## Python Library |
| 126 | + |
| 127 | +```python |
| 128 | +from eia import EIAClient |
| 129 | + |
| 130 | +client = EIAClient() # reads config file, then EIA_API_KEY env var |
| 131 | + |
| 132 | +# Navigate the route tree |
| 133 | +route = client.route("electricity/rto/fuel-type-data") |
| 134 | +route.routes # Dict of child routes (if branch node) |
| 135 | +route.data # Data object (if leaf node) |
| 136 | + |
| 137 | +# Inspect metadata |
| 138 | +route.data.facets # FacetContainer with attribute access |
| 139 | +route.data.frequencies # List[FrequencyInfo] |
| 140 | +route.data.data_columns # Dict[str, DataColumnInfo] |
| 141 | +route.data.facets.respondent.get_values() # List[FacetValue] |
| 142 | + |
| 143 | +# Direct access to a data endpoint |
| 144 | +data = client.get_data_endpoint("electricity/rto/fuel-type-data") |
| 145 | + |
| 146 | +# Fetch data as DataFrame |
| 147 | +df = data.get( |
| 148 | + data_columns=["value"], |
| 149 | + facets={"respondent": "CISO"}, |
| 150 | + frequency="hourly", |
| 151 | + start="2024-01-01", |
| 152 | + end="2024-01-31", |
| 153 | + sort=[{"column": "period", "direction": "desc"}], |
| 154 | +) |
| 155 | +``` |
| 156 | + |
| 157 | +## Configuration |
| 158 | + |
| 159 | +```bash |
| 160 | +# Store your API key persistently (recommended) |
| 161 | +eia config set api-key YOUR_KEY |
| 162 | + |
| 163 | +# Verify it's stored |
| 164 | +eia config get api-key |
| 165 | +``` |
| 166 | + |
| 167 | +The config file is stored at `~/.config/eia/config.toml`. |
| 168 | + |
| 169 | +## Key Conventions |
| 170 | + |
| 171 | +- The `period` column is auto-converted to datetime (UTC for non-local frequencies) |
| 172 | +- The `value` column is auto-converted to numeric |
| 173 | +- Pagination is automatic by default (fetches all pages) |
| 174 | +- API key resolution: config file (`~/.config/eia/config.toml`) > `EIA_API_KEY` env var |
| 175 | +- API page limit is 5000 rows per request |
| 176 | +- Custom exception: `EIAError` (includes HTTP status code and API error code) |
0 commit comments