|
| 1 | +# DataFrames and pagination |
| 2 | + |
| 3 | +The pandas helpers preserve the source context returned by the API and fetch |
| 4 | +complete result sets without requiring a manual page loop. |
| 5 | + |
| 6 | +Install the optional dependency: |
| 7 | + |
| 8 | +```bash |
| 9 | +python -m pip install "oilpriceapi[pandas]" |
| 10 | +``` |
| 11 | + |
| 12 | +## Historical data |
| 13 | + |
| 14 | +```python |
| 15 | +from oilpriceapi import OilPriceAPI |
| 16 | + |
| 17 | +client = OilPriceAPI() |
| 18 | +df = client.historical.to_dataframe( |
| 19 | + commodity="BRENT_CRUDE_USD", |
| 20 | + start="2026-01-01", |
| 21 | + end="2026-06-30", |
| 22 | + interval="daily", |
| 23 | + per_page=500, |
| 24 | +) |
| 25 | +``` |
| 26 | + |
| 27 | +The historical DataFrame contract is: |
| 28 | + |
| 29 | +- Every page is fetched automatically until the API reports completion. |
| 30 | +- `per_page` is an integer from 1 to 1000 and defaults to 500. |
| 31 | +- `per_page` controls each request; it does not limit the total rows returned. |
| 32 | +- `currency` and `unit` are taken from each API record. A missing currency |
| 33 | + remains null and is never converted to USD. |
| 34 | +- An exact record repeated on a later, overlapping page is emitted once. |
| 35 | + Distinct records, including records sharing a timestamp, remain intact. |
| 36 | +- An empty page terminates pagination even if stale metadata says another page |
| 37 | + exists. |
| 38 | +- The result is sorted by its `date` index. Empty results retain the columns |
| 39 | + `commodity`, `value`, `currency`, `unit`, and `type_name`. |
| 40 | + |
| 41 | +The convenience resource uses the same pagination behavior: |
| 42 | + |
| 43 | +```python |
| 44 | +df = client.prices.to_dataframe( |
| 45 | + commodity="EU_CARBON_EUR", |
| 46 | + start="2026-01-01", |
| 47 | + end="2026-06-30", |
| 48 | + per_page=250, |
| 49 | +) |
| 50 | +``` |
| 51 | + |
| 52 | +For model objects instead of pandas, use `client.historical.get_all(...)`. |
| 53 | +It accepts the same `per_page` range and automatically fetches every page. |
| 54 | + |
| 55 | +## Current prices |
| 56 | + |
| 57 | +Calling `client.prices.to_dataframe()` with no commodity returns all current |
| 58 | +prices and automatically follows the API's pagination headers: |
| 59 | + |
| 60 | +```python |
| 61 | +df = client.prices.to_dataframe(per_page=250) |
| 62 | +``` |
| 63 | + |
| 64 | +Each row keeps the API-provided currency and unit. Exact records repeated on a |
| 65 | +later page are returned once. |
| 66 | + |
| 67 | +## Manual page control |
| 68 | + |
| 69 | +Use `client.historical.get(...)` when a single page is intentional: |
| 70 | + |
| 71 | +```python |
| 72 | +page = client.historical.get( |
| 73 | + commodity="BRENT_CRUDE_USD", |
| 74 | + page=2, |
| 75 | + per_page=100, |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +Use `client.historical.iter_pages(...)` to process complete results one page |
| 80 | +at a time without retaining the entire response in memory. |
0 commit comments