From c197c633434a5b1d8e8f4c8904e3f2a8593c42cc Mon Sep 17 00:00:00 2001 From: joosissimo <29389546+joosissimo@users.noreply.github.com> Date: Fri, 21 Jun 2024 17:11:50 -0700 Subject: [PATCH] Add Polars example to readme --- README.md | 49 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 308f9b7..9e578b1 100644 --- a/README.md +++ b/README.md @@ -64,8 +64,7 @@ hourly_wind_speed_10m = next(filter(lambda x: x.Variable() == Variable.wind_spee ### Pandas -For `Pandas` you can prepare a data-frame from hourly data like follows: - +After using `NumPy` to create arrays for hourly data, you can use `Pandas` to create a DataFrame from hourly data like follows: ```python import pandas as pd @@ -80,13 +79,43 @@ hourly_data["temperature_2m"] = hourly_temperature_2m hourly_data["precipitation"] = hourly_precipitation hourly_data["wind_speed_10m"] = hourly_wind_speed_10m -hourly_dataframe = pd.DataFrame(data = hourly_data) -print(hourly_dataframe) -#date temperature_2m precipitation -#0 2023-08-01 00:00:00 16.945999 1.7 -#1 2023-08-01 01:00:00 16.996000 2.1 -#2 2023-08-01 02:00:00 16.996000 1.0 -#3 2023-08-01 03:00:00 16.846001 0.2 +hourly_dataframe_pd = pd.DataFrame(data = hourly_data) +print(hourly_dataframe_pd) +# date temperature_2m precipitation wind_speed_10m +# 0 2024-06-21 00:00:00 17.437000 0.0 6.569383 +# 1 2024-06-21 01:00:00 17.087000 0.0 6.151683 +# 2 2024-06-21 02:00:00 16.786999 0.0 7.421590 +# 3 2024-06-21 03:00:00 16.337000 0.0 5.154416 +``` + +### Polars + +Additionally, `Polars` can also be used to create a DataFrame from hourly data using the `NumPy` arrays created previously: + +```python +import polars as pl +from datetime import datetime, timedelta, timezone + +start = datetime.fromtimestamp(hourly.Time(), timezone.utc) +end = datetime.fromtimestamp(hourly.TimeEnd(), timezone.utc) +freq = timedelta(seconds = hourly.Interval()) + +hourly_dataframe_pl = pl.select( + date = pl.datetime_range(start, end, freq, closed = "left"), + temperature_2m = hourly_temperature_2m, + precipitation = hourly_precipitation, + wind_speed_10m = hourly_wind_speed_10m +) +print(hourly_dataframe_pl) +# ┌─────────────────────────┬────────────────┬───────────────┬────────────────┐ +# │ date ┆ temperature_2m ┆ precipitation ┆ wind_speed_10m │ +# │ --- ┆ --- ┆ --- ┆ --- │ +# │ datetime[μs, UTC] ┆ f32 ┆ f32 ┆ f32 │ +# ╞═════════════════════════╪════════════════╪═══════════════╪════════════════╡ +# │ 2024-06-21 00:00:00 UTC ┆ 17.437 ┆ 0.0 ┆ 6.569383 │ +# │ 2024-06-21 01:00:00 UTC ┆ 17.087 ┆ 0.0 ┆ 6.151683 │ +# │ 2024-06-21 02:00:00 UTC ┆ 16.786999 ┆ 0.0 ┆ 7.42159 │ +# │ 2024-06-21 03:00:00 UTC ┆ 16.337 ┆ 0.0 ┆ 5.154416 │ ``` ### Caching Data @@ -114,6 +143,7 @@ om = openmeteo_requests.Client(session=retry_session) ``` # TODO + - Document multi location/timeinterval usage - Document FlatBuffers data structure - Document time start/end/interval @@ -123,4 +153,5 @@ om = openmeteo_requests.Client(session=retry_session) - Consider dedicated pandas library to convert responses quickly # License + MIT