# Data Processing

Once you have weather data, Polars provides powerful tools for analysis. This guide shows common data processing patterns.


# Working with Polars DataFrames

weathervault returns [Polars DataFrames](https://pola-rs.github.io/polars/), which offer:

- fast performance (written in Rust)
- expressive API similar to pandas
- lazy evaluation support
- excellent datetime handling


``` python
import weathervault as wv
import polars as pl

weather = wv.get_weather_data("725030-14732", years=2024)
```


# Basic DataFrame Operations


## Inspecting Data


``` python
# Shape
print(weather.shape)

# Column names and types
print(weather.schema)

# First few rows
weather.head()

# Summary statistics
weather.describe()
```


    (13420, 10)
    Schema({'id': String, 'time': Datetime(time_unit='us', time_zone='America/New_York'), 'temp': Float64, 'dew_point': Float64, 'rh': Float64, 'wd': Int32, 'ws': Float64, 'atmos_pres': Float64, 'ceil_hgt': Int32, 'visibility': Int32})


shape: (9, 11)

| statistic | id | time | temp | dew_point | rh | wd | ws | atmos_pres | ceil_hgt | visibility |
|----|----|----|----|----|----|----|----|----|----|----|
| str | str | str | f64 | f64 | f64 | f64 | f64 | f64 | f64 | f64 |
| "count" | "13420" | "13420" | 13040.0 | 13043.0 | 13040.0 | 12320.0 | 13039.0 | 11549.0 | 11189.0 | 13043.0 |
| "null_count" | "0" | "0" | 380.0 | 377.0 | 380.0 | 1100.0 | 381.0 | 1871.0 | 2231.0 | 377.0 |
| "mean" | null | "2024-06-29 02:45:53.329359-04:… | 14.157531 | 6.04009 | 60.745429 | 184.961851 | 4.677598 | 1016.33699 | 10586.513272 | 14618.577628 |
| "std" | null | null | 9.100941 | 9.590025 | 18.151291 | 109.724944 | 2.373031 | 8.171406 | 9747.531112 | 3756.852125 |
| "min" | "725030-14732" | "2024-01-01 00:51:00-05:00" | -11.1 | -19.4 | 15.1 | 10.0 | 0.0 | 982.0 | 60.0 | 200.0 |
| "25%" | null | "2024-03-29 00:59:00-04:00" | 6.7 | -1.1 | 46.0 | 70.0 | 3.1 | 1011.6 | 1067.0 | 16000.0 |
| "50%" | null | "2024-06-27 20:00:00-04:00" | 14.4 | 6.7 | 59.6 | 180.0 | 4.6 | 1016.1 | 7620.0 | 16093.0 |
| "75%" | null | "2024-09-27 16:51:00-04:00" | 22.0 | 13.3 | 76.8 | 300.0 | 6.2 | 1021.2 | 22000.0 | 16093.0 |
| "max" | "725030-14732" | "2024-12-31 23:59:00-05:00" | 35.6 | 23.9 | 100.0 | 360.0 | 17.5 | 1048.6 | 22000.0 | 16093.0 |


## Selecting Columns


``` python
# Select specific columns
temps = weather.select(["time", "temp", "dew_point"])
temps
```


shape: (13_420, 3)

| time                             | temp | dew_point |
|----------------------------------|------|-----------|
| datetime\[μs, America/New_York\] | f64  | f64       |
| 2024-01-01 00:51:00 EST          | 6.1  | -2.8      |
| 2024-01-01 01:00:00 EST          | 6.1  | -2.8      |
| 2024-01-01 01:51:00 EST          | 6.1  | -2.8      |
| 2024-01-01 02:51:00 EST          | 5.6  | -2.2      |
| 2024-01-01 03:51:00 EST          | 5.6  | -1.7      |
| …                                | …    | …         |
| 2024-12-31 23:20:00 EST          | 7.2  | 4.4       |
| 2024-12-31 23:43:00 EST          | 7.2  | 5.0       |
| 2024-12-31 23:51:00 EST          | 7.2  | 5.0       |
| 2024-12-31 23:59:00 EST          | null | null      |
| 2024-12-31 23:59:00 EST          | null | null      |


``` python
# Select multiple columns with expressions
weather.select([
    pl.col("time"),
    pl.col("temp"),
    pl.col("ws").alias("wind_speed")
])
```


shape: (13_420, 3)

| time                             | temp | wind_speed |
|----------------------------------|------|------------|
| datetime\[μs, America/New_York\] | f64  | f64        |
| 2024-01-01 00:51:00 EST          | 6.1  | 3.1        |
| 2024-01-01 01:00:00 EST          | 6.1  | 3.1        |
| 2024-01-01 01:51:00 EST          | 6.1  | 4.1        |
| 2024-01-01 02:51:00 EST          | 5.6  | 4.1        |
| 2024-01-01 03:51:00 EST          | 5.6  | 3.1        |
| …                                | …    | …          |
| 2024-12-31 23:20:00 EST          | 7.2  | 7.7        |
| 2024-12-31 23:43:00 EST          | 7.2  | 4.6        |
| 2024-12-31 23:51:00 EST          | 7.2  | 5.7        |
| 2024-12-31 23:59:00 EST          | null | null       |
| 2024-12-31 23:59:00 EST          | null | null       |


## Filtering Rows


``` python
# Filter by temperature
hot_days = weather.filter(pl.col("temp") > 30)
hot_days
```


shape: (246, 10)

| id | time | temp | dew_point | rh | wd | ws | atmos_pres | ceil_hgt | visibility |
|----|----|----|----|----|----|----|----|----|----|
| str | datetime\[μs, America/New_York\] | f64 | f64 | f64 | i32 | f64 | f64 | i32 | i32 |
| "725030-14732" | 2024-06-19 12:51:00 EDT | 31.7 | 18.9 | 46.7 | null | 2.6 | 1026.5 | 7620 | 16093 |
| "725030-14732" | 2024-06-20 10:51:00 EDT | 31.1 | 16.7 | 42.1 | 230 | 4.6 | 1026.0 | 7620 | 16093 |
| "725030-14732" | 2024-06-20 11:00:00 EDT | 31.1 | 16.7 | 42.1 | 230 | 4.6 | 1026.0 | null | 16000 |
| "725030-14732" | 2024-06-20 11:51:00 EDT | 31.1 | 16.7 | 42.1 | 240 | 4.6 | 1025.8 | 7620 | 16093 |
| "725030-14732" | 2024-06-20 12:51:00 EDT | 33.3 | 16.7 | 37.1 | 210 | 4.1 | 1025.3 | 7620 | 16093 |
| … | … | … | … | … | … | … | … | … | … |
| "725030-14732" | 2024-08-28 18:51:00 EDT | 31.7 | 20.0 | 50.0 | 350 | 3.6 | 1013.2 | 4877 | 16093 |
| "725030-14732" | 2024-08-28 19:51:00 EDT | 31.7 | 20.0 | 50.0 | 10 | 3.6 | 1013.9 | 22000 | 16093 |
| "725030-14732" | 2024-08-28 20:00:00 EDT | 31.7 | 20.0 | 50.0 | 10 | 3.6 | 1013.9 | null | 16000 |
| "725030-14732" | 2024-08-28 20:51:00 EDT | 30.6 | 20.0 | 53.2 | 350 | 3.1 | 1014.6 | 22000 | 16093 |
| "725030-14732" | 2024-08-28 21:03:00 EDT | 30.6 | 20.0 | 53.2 | 350 | 2.6 | null | 22000 | 16093 |


``` python
# Multiple conditions
summer_hot = weather.filter(
    (pl.col("temp") > 25) &
    (pl.col("time").dt.month().is_in([6, 7, 8]))
)
summer_hot
```


shape: (1_381, 10)

| id | time | temp | dew_point | rh | wd | ws | atmos_pres | ceil_hgt | visibility |
|----|----|----|----|----|----|----|----|----|----|
| str | datetime\[μs, America/New_York\] | f64 | f64 | f64 | i32 | f64 | f64 | i32 | i32 |
| "725030-14732" | 2024-06-01 12:51:00 EDT | 26.1 | 6.1 | 27.9 | null | 1.5 | 1020.4 | 22000 | 16093 |
| "725030-14732" | 2024-06-01 13:51:00 EDT | 27.2 | 6.1 | 26.1 | 30 | 2.1 | 1019.6 | 22000 | 16093 |
| "725030-14732" | 2024-06-01 14:00:00 EDT | 27.2 | 6.1 | 26.1 | 30 | 2.1 | 1019.6 | 22000 | 16000 |
| "725030-14732" | 2024-06-01 14:51:00 EDT | 27.8 | 5.0 | 23.4 | 310 | 5.1 | 1019.0 | 22000 | 16093 |
| "725030-14732" | 2024-06-01 15:51:00 EDT | 28.9 | 3.9 | 20.3 | 250 | 3.6 | 1018.4 | 22000 | 16093 |
| … | … | … | … | … | … | … | … | … | … |
| "725030-14732" | 2024-08-31 14:51:00 EDT | 25.6 | 17.8 | 62.1 | 150 | 3.6 | 1017.2 | 853 | 16093 |
| "725030-14732" | 2024-08-31 15:43:00 EDT | 26.7 | 18.3 | 60.0 | 180 | 3.6 | null | 1280 | 16093 |
| "725030-14732" | 2024-08-31 15:51:00 EDT | 26.1 | 17.8 | 60.3 | 170 | 4.6 | 1016.1 | 1280 | 16093 |
| "725030-14732" | 2024-08-31 16:51:00 EDT | 25.6 | 17.2 | 59.8 | 160 | 4.6 | 1015.5 | 1372 | 16093 |
| "725030-14732" | 2024-08-31 17:00:00 EDT | 25.6 | 17.2 | 59.8 | 160 | 4.6 | 1015.5 | null | 16000 |


``` python
# Filter non-null values
complete_data = weather.filter(
    pl.col("temp").is_not_null() &
    pl.col("ws").is_not_null()
)
complete_data
```


shape: (13_036, 10)

| id | time | temp | dew_point | rh | wd | ws | atmos_pres | ceil_hgt | visibility |
|----|----|----|----|----|----|----|----|----|----|
| str | datetime\[μs, America/New_York\] | f64 | f64 | f64 | i32 | f64 | f64 | i32 | i32 |
| "725030-14732" | 2024-01-01 00:51:00 EST | 6.1 | -2.8 | 52.9 | 230 | 3.1 | 1015.7 | 1280 | 16093 |
| "725030-14732" | 2024-01-01 01:00:00 EST | 6.1 | -2.8 | 52.9 | 230 | 3.1 | 1015.7 | null | 16000 |
| "725030-14732" | 2024-01-01 01:51:00 EST | 6.1 | -2.8 | 52.9 | 270 | 4.1 | 1015.5 | 1829 | 16093 |
| "725030-14732" | 2024-01-01 02:51:00 EST | 5.6 | -2.2 | 57.2 | 270 | 4.1 | 1015.5 | 1097 | 16093 |
| "725030-14732" | 2024-01-01 03:51:00 EST | 5.6 | -1.7 | 59.4 | 280 | 3.1 | 1015.1 | 1219 | 16093 |
| … | … | … | … | … | … | … | … | … | … |
| "725030-14732" | 2024-12-31 22:51:00 EST | 6.7 | 4.4 | 85.3 | 40 | 8.8 | 999.9 | 427 | 4828 |
| "725030-14732" | 2024-12-31 23:09:00 EST | 6.7 | 4.4 | 85.3 | 40 | 6.2 | null | 274 | 16093 |
| "725030-14732" | 2024-12-31 23:20:00 EST | 7.2 | 4.4 | 82.4 | 50 | 7.7 | null | 335 | 16093 |
| "725030-14732" | 2024-12-31 23:43:00 EST | 7.2 | 5.0 | 85.9 | 40 | 4.6 | null | 671 | 16093 |
| "725030-14732" | 2024-12-31 23:51:00 EST | 7.2 | 5.0 | 85.9 | 40 | 5.7 | 998.9 | 671 | 16093 |


# Time-Based Analysis


## Extract Time Components


``` python
# Add time components as new columns
weather_with_time = weather.with_columns([
    pl.col("time").dt.year().alias("year"),
    pl.col("time").dt.month().alias("month"),
    pl.col("time").dt.day().alias("day"),
    pl.col("time").dt.hour().alias("hour"),
    pl.col("time").dt.weekday().alias("weekday"),
])

weather_with_time[["time", "year", "month", "day", "hour"]]
```


shape: (13_420, 5)

| time                             | year | month | day | hour |
|----------------------------------|------|-------|-----|------|
| datetime\[μs, America/New_York\] | i32  | i8    | i8  | i8   |
| 2024-01-01 00:51:00 EST          | 2024 | 1     | 1   | 0    |
| 2024-01-01 01:00:00 EST          | 2024 | 1     | 1   | 1    |
| 2024-01-01 01:51:00 EST          | 2024 | 1     | 1   | 1    |
| 2024-01-01 02:51:00 EST          | 2024 | 1     | 1   | 2    |
| 2024-01-01 03:51:00 EST          | 2024 | 1     | 1   | 3    |
| …                                | …    | …     | …   | …    |
| 2024-12-31 23:20:00 EST          | 2024 | 12    | 31  | 23   |
| 2024-12-31 23:43:00 EST          | 2024 | 12    | 31  | 23   |
| 2024-12-31 23:51:00 EST          | 2024 | 12    | 31  | 23   |
| 2024-12-31 23:59:00 EST          | 2024 | 12    | 31  | 23   |
| 2024-12-31 23:59:00 EST          | 2024 | 12    | 31  | 23   |


## Filter by Time Range


``` python
# Filter to a date range by extracting the date component
jan_2024 = weather.filter(
    (pl.col("time").dt.date() >= pl.date(2024, 1, 1)) &
    (pl.col("time").dt.date() < pl.date(2024, 2, 1))
)
jan_2024
```


shape: (1_238, 10)

| id | time | temp | dew_point | rh | wd | ws | atmos_pres | ceil_hgt | visibility |
|----|----|----|----|----|----|----|----|----|----|
| str | datetime\[μs, America/New_York\] | f64 | f64 | f64 | i32 | f64 | f64 | i32 | i32 |
| "725030-14732" | 2024-01-01 00:51:00 EST | 6.1 | -2.8 | 52.9 | 230 | 3.1 | 1015.7 | 1280 | 16093 |
| "725030-14732" | 2024-01-01 01:00:00 EST | 6.1 | -2.8 | 52.9 | 230 | 3.1 | 1015.7 | null | 16000 |
| "725030-14732" | 2024-01-01 01:51:00 EST | 6.1 | -2.8 | 52.9 | 270 | 4.1 | 1015.5 | 1829 | 16093 |
| "725030-14732" | 2024-01-01 02:51:00 EST | 5.6 | -2.2 | 57.2 | 270 | 4.1 | 1015.5 | 1097 | 16093 |
| "725030-14732" | 2024-01-01 03:51:00 EST | 5.6 | -1.7 | 59.4 | 280 | 3.1 | 1015.1 | 1219 | 16093 |
| … | … | … | … | … | … | … | … | … | … |
| "725030-14732" | 2024-01-31 22:00:00 EST | 3.9 | -1.1 | 69.9 | 310 | 2.1 | 1018.4 | null | 16000 |
| "725030-14732" | 2024-01-31 22:51:00 EST | 3.9 | -1.1 | 69.9 | 310 | 1.5 | 1018.5 | 975 | 16093 |
| "725030-14732" | 2024-01-31 23:51:00 EST | 4.4 | -1.1 | 67.5 | 310 | 2.1 | 1018.0 | 914 | 16093 |
| "725030-14732" | 2024-01-31 23:59:00 EST | null | null | null | null | null | null | null | null |
| "725030-14732" | 2024-01-31 23:59:00 EST | null | null | null | null | null | null | null | null |


## Resample to Different Time Intervals


``` python
# Daily aggregation
daily = weather.group_by_dynamic("time", every="1d").agg([
    pl.col("temp").mean().alias("mean_temp"),
    pl.col("temp").min().alias("min_temp"),
    pl.col("temp").max().alias("max_temp"),
    pl.col("ws").mean().alias("mean_wind_speed"),
])

daily
```


shape: (366, 5)

| time                             | mean_temp | min_temp | max_temp | mean_wind_speed |
|----------------------------------|-----------|----------|----------|-----------------|
| datetime\[μs, America/New_York\] | f64       | f64      | f64      | f64             |
| 2024-01-01 00:00:00 EST          | 5.7       | 2.2      | 7.2      | 3.011765        |
| 2024-01-02 00:00:00 EST          | 2.7375    | -1.1     | 6.1      | 4.2625          |
| 2024-01-03 00:00:00 EST          | 3.96875   | 1.7      | 6.1      | 4.0125          |
| 2024-01-04 00:00:00 EST          | 3.771875  | -1.7     | 6.7      | 6.6375          |
| 2024-01-05 00:00:00 EST          | 0.540625  | -2.2     | 3.3      | 5.34375         |
| …                                | …         | …        | …        | …               |
| 2024-12-27 00:00:00 EST          | 2.003125  | -2.2     | 6.1      | 2.03125         |
| 2024-12-28 00:00:00 EST          | 6.759524  | 3.9      | 10.0     | 1.371429        |
| 2024-12-29 00:00:00 EST          | 10.645946 | 7.8      | 14.4     | 4.045946        |
| 2024-12-30 00:00:00 EST          | 12.49     | 10.0     | 13.9     | 6.8025          |
| 2024-12-31 00:00:00 EST          | 8.642105  | 6.7      | 11.1     | 4.684211        |


``` python
# Monthly aggregation
monthly = weather.group_by_dynamic("time", every="1mo").agg([
    pl.col("temp").mean().alias("mean_temp"),
    pl.col("temp").min().alias("min_temp"),
    pl.col("temp").max().alias("max_temp"),
    pl.col("rh").mean().alias("mean_humidity"),
])

monthly
```


shape: (12, 5)

| time                             | mean_temp | min_temp | max_temp | mean_humidity |
|----------------------------------|-----------|----------|----------|---------------|
| datetime\[μs, America/New_York\] | f64       | f64      | f64      | f64           |
| 2024-01-01 00:00:00 EST          | 3.007711  | -7.2     | 15.0     | 64.971476     |
| 2024-02-01 00:00:00 EST          | 4.061704  | -4.4     | 15.6     | 57.237316     |
| 2024-03-01 00:00:00 EST          | 7.999189  | -1.1     | 21.7     | 60.362252     |
| 2024-04-01 00:00:00 EDT          | 11.37744  | 2.8      | 28.3     | 61.346553     |
| 2024-05-01 00:00:00 EDT          | 17.386817 | 9.4      | 30.0     | 68.012576     |
| …                                | …         | …        | …        | …             |
| 2024-08-01 00:00:00 EDT          | 24.299734 | 15.0     | 34.4     | 64.849023     |
| 2024-09-01 00:00:00 EDT          | 20.497729 | 13.9     | 28.9     | 64.147493     |
| 2024-10-01 00:00:00 EDT          | 16.513951 | 7.2      | 27.8     | 52.337659     |
| 2024-11-01 00:00:00 EDT          | 11.368163 | 0.0      | 26.7     | 51.905204     |
| 2024-12-01 00:00:00 EST          | 4.285727  | -11.1    | 15.6     | 60.722837     |


# Handling Missing Values


## Identify Missing Data


``` python
# Count nulls per column
null_counts = weather.null_count()
null_counts
```


shape: (1, 10)

| id  | time | temp | dew_point | rh  | wd   | ws  | atmos_pres | ceil_hgt | visibility |
|-----|------|------|-----------|-----|------|-----|------------|----------|------------|
| u32 | u32  | u32  | u32       | u32 | u32  | u32 | u32        | u32      | u32        |
| 0   | 0    | 380  | 377       | 380 | 1100 | 381 | 1871       | 2231     | 377        |


``` python
# Percentage of missing values
missing_pct = (weather.null_count() / weather.height) * 100
missing_pct
```


shape: (1, 10)

| id | time | temp | dew_point | rh | wd | ws | atmos_pres | ceil_hgt | visibility |
|----|----|----|----|----|----|----|----|----|----|
| f64 | f64 | f64 | f64 | f64 | f64 | f64 | f64 | f64 | f64 |
| 0.0 | 0.0 | 2.831595 | 2.80924 | 2.831595 | 8.196721 | 2.839046 | 13.941878 | 16.624441 | 2.80924 |


## Fill Missing Values


``` python
# Forward fill
weather_filled = weather.with_columns([
    pl.col("temp").forward_fill().alias("temp_filled")
])
```


``` python
# Fill with a specific value
weather_filled = weather.with_columns([
    pl.col("temp").fill_null(0)
])
```


``` python
# Drop rows with any null values
weather_complete = weather.drop_nulls()
```


# Temperature Conversions

While weathervault supports built-in temperature unit conversion, you can also convert manually:


``` python
# Celsius to Fahrenheit
weather_manual = weather.with_columns([
    ((pl.col("temp") * 9 / 5) + 32).alias("temp_f")
])

weather_manual[["temp", "temp_f"]]
```


shape: (13_420, 2)

| temp | temp_f |
|------|--------|
| f64  | f64    |
| 6.1  | 42.98  |
| 6.1  | 42.98  |
| 6.1  | 42.98  |
| 5.6  | 42.08  |
| 5.6  | 42.08  |
| …    | …      |
| 7.2  | 44.96  |
| 7.2  | 44.96  |
| 7.2  | 44.96  |
| null | null   |
| null | null   |


``` python
# Celsius to Kelvin
weather_manual = weather.with_columns([
    (pl.col("temp") + 273.15).alias("temp_k")
])

weather_manual[["temp", "temp_k"]]
```


shape: (13_420, 2)

| temp | temp_k |
|------|--------|
| f64  | f64    |
| 6.1  | 279.25 |
| 6.1  | 279.25 |
| 6.1  | 279.25 |
| 5.6  | 278.75 |
| 5.6  | 278.75 |
| …    | …      |
| 7.2  | 280.35 |
| 7.2  | 280.35 |
| 7.2  | 280.35 |
| null | null   |
| null | null   |


# Aggregations and Statistics


## Basic Statistics


``` python
# Calculate statistics for each numeric column
weather.select([
    pl.col("temp").mean().alias("mean_temp"),
    pl.col("temp").median().alias("median_temp"),
    pl.col("temp").std().alias("std_temp"),
    pl.col("temp").quantile(0.25).alias("q25_temp"),
    pl.col("temp").quantile(0.75).alias("q75_temp"),
])
```


shape: (1, 5)

| mean_temp | median_temp | std_temp | q25_temp | q75_temp |
|-----------|-------------|----------|----------|----------|
| f64       | f64         | f64      | f64      | f64      |
| 14.157531 | 14.4        | 9.100941 | 6.7      | 22.0     |


## Group By Operations


``` python
# Monthly statistics
monthly_stats = weather.group_by(pl.col("time").dt.month()).agg([
    pl.col("temp").mean().alias("mean_temp"),
    pl.col("temp").min().alias("min_temp"),
    pl.col("temp").max().alias("max_temp"),
    pl.col("rh").mean().alias("mean_humidity"),
    pl.col("ws").mean().alias("mean_wind_speed"),
    pl.len().alias("num_observations"),
])

monthly_stats.sort("time")
```


shape: (12, 7)

| time | mean_temp | min_temp | max_temp | mean_humidity | mean_wind_speed | num_observations |
|----|----|----|----|----|----|----|
| i8 | f64 | f64 | f64 | f64 | f64 | u32 |
| 1 | 3.007711 | -7.2 | 15.0 | 64.971476 | 5.262438 | 1238 |
| 2 | 4.061704 | -4.4 | 15.6 | 57.237316 | 4.721548 | 1051 |
| 3 | 7.999189 | -1.1 | 21.7 | 60.362252 | 5.494324 | 1140 |
| 4 | 11.37744 | 2.8 | 28.3 | 61.346553 | 5.351298 | 1148 |
| 5 | 17.386817 | 9.4 | 30.0 | 68.012576 | 4.182031 | 1185 |
| … | … | … | … | … | … | … |
| 8 | 24.299734 | 15.0 | 34.4 | 64.849023 | 4.245915 | 1158 |
| 9 | 20.497729 | 13.9 | 28.9 | 64.147493 | 4.177673 | 1088 |
| 10 | 16.513951 | 7.2 | 27.8 | 52.337659 | 4.09042 | 1057 |
| 11 | 11.368163 | 0.0 | 26.7 | 51.905204 | 5.088776 | 1012 |
| 12 | 4.285727 | -11.1 | 15.6 | 60.722837 | 4.950937 | 1153 |


``` python
# Hourly patterns (average conditions by hour of day)
hourly_pattern = weather.group_by(pl.col("time").dt.hour()).agg([
    pl.col("temp").mean().alias("mean_temp"),
    pl.col("ws").mean().alias("mean_wind_speed"),
]).sort("time")

hourly_pattern
```


shape: (24, 3)

| time | mean_temp | mean_wind_speed |
|------|-----------|-----------------|
| i8   | f64       | f64             |
| 0    | 12.993525 | 4.128297        |
| 1    | 10.836479 | 4.169147        |
| 2    | 14.155657 | 3.851529        |
| 3    | 12.40245  | 3.979065        |
| 4    | 10.159292 | 4.102832        |
| …    | …         | …               |
| 19   | 12.862842 | 5.120036        |
| 20   | 16.440787 | 4.784094        |
| 21   | 14.134824 | 4.596941        |
| 22   | 11.811538 | 4.510806        |
| 23   | 15.097192 | 4.297036        |


# Combining Multiple Stations

When analyzing multiple stations, you'll want to combine their data:


``` python
# Get data from multiple stations
lga = wv.get_weather_data("725030-14732", years=2024, incl_stn_info=True)
jfk = wv.get_weather_data("744860-94789", years=2024, incl_stn_info=True)
ewr = wv.get_weather_data("725020-14734", years=2024, incl_stn_info=True)

# Concatenate vertically
nyc_airports = pl.concat([lga, jfk, ewr])

# Compare average temperatures
comparison = nyc_airports.group_by(["id", "name"]).agg([
    pl.col("temp").mean().alias("mean_temp"),
    pl.col("temp").min().alias("min_temp"),
    pl.col("temp").max().alias("max_temp"),
])

comparison
```


shape: (3, 5)

| id             | name                             | mean_temp | min_temp | max_temp |
|----------------|----------------------------------|-----------|----------|----------|
| str            | str                              | f64       | f64      | f64      |
| "725030-14732" | "LA GUARDIA AIRPORT"             | 14.157531 | -11.1    | 35.6     |
| "744860-94789" | "JOHN F KENNEDY INTERNATIONAL A… | 13.798241 | -10.6    | 34.4     |
| "725020-14734" | "NEWARK LIBERTY INTERNATIONAL A… | 14.727874 | -11.7    | 37.2     |


# Exporting Data


## To CSV

``` python
# Save to CSV
weather.write_csv("weather_data.csv")
```


## To Parquet

``` python
# Save to Parquet (more efficient for large datasets)
weather.write_parquet("weather_data.parquet")
```


## To Pandas

weathervault uses Polars DataFrames natively. If you need pandas compatibility, use `df.to_pandas()` (requires pyarrow).


# Data Quality Checks


## Identify Extreme Values


``` python
# Find extreme temperatures
extreme_temps = weather.filter(
    (pl.col("temp") < -40) | (pl.col("temp") > 50)
)

if extreme_temps.height > 0:
    print(f"Found {extreme_temps.height} extreme temperature values")
    extreme_temps[["time", "temp"]]
```


## Check for Duplicate Timestamps


``` python
# Find duplicate times
duplicates = weather.filter(
    pl.col("time").is_duplicated()
)

print(f"Duplicate timestamps: {duplicates.height}")
```


    Duplicate timestamps: 40


## Data Completeness Report


``` python
# Create a completeness report
completeness = pl.DataFrame({
    "variable": weather.columns,
    "total_obs": [weather.height] * len(weather.columns),
    "missing": [weather[col].null_count() for col in weather.columns],
}).with_columns([
    (100 - (pl.col("missing") / pl.col("total_obs") * 100)).alias("completeness_pct")
])

completeness
```


shape: (10, 4)

| variable     | total_obs | missing | completeness_pct |
|--------------|-----------|---------|------------------|
| str          | i64       | i64     | f64              |
| "id"         | 13420     | 0       | 100.0            |
| "time"       | 13420     | 0       | 100.0            |
| "temp"       | 13420     | 380     | 97.168405        |
| "dew_point"  | 13420     | 377     | 97.19076         |
| "rh"         | 13420     | 380     | 97.168405        |
| "wd"         | 13420     | 1100    | 91.803279        |
| "ws"         | 13420     | 381     | 97.160954        |
| "atmos_pres" | 13420     | 1871    | 86.058122        |
| "ceil_hgt"   | 13420     | 2231    | 83.375559        |
| "visibility" | 13420     | 377     | 97.19076         |


# Advanced: Lazy Evaluation

For very large datasets, use Polars lazy API:


``` python
# Create a lazy query
lazy_weather = weather.lazy()

# Build a query (not executed yet)
result_lazy = (
    lazy_weather
    .filter(pl.col("temp").is_not_null())
    .group_by_dynamic("time", every="1d")
    .agg([
        pl.col("temp").mean().alias("mean_temp"),
        pl.col("temp").max().alias("max_temp"),
    ])
)

# Execute when needed
result = result_lazy.collect()
result
```


shape: (366, 3)

| time                             | mean_temp | max_temp |
|----------------------------------|-----------|----------|
| datetime\[μs, America/New_York\] | f64       | f64      |
| 2024-01-01 00:00:00 EST          | 5.7       | 7.2      |
| 2024-01-02 00:00:00 EST          | 2.7375    | 6.1      |
| 2024-01-03 00:00:00 EST          | 3.96875   | 6.1      |
| 2024-01-04 00:00:00 EST          | 3.771875  | 6.7      |
| 2024-01-05 00:00:00 EST          | 0.540625  | 3.3      |
| …                                | …         | …        |
| 2024-12-27 00:00:00 EST          | 2.003125  | 6.1      |
| 2024-12-28 00:00:00 EST          | 6.759524  | 10.0     |
| 2024-12-29 00:00:00 EST          | 10.645946 | 14.4     |
| 2024-12-30 00:00:00 EST          | 12.49     | 13.9     |
| 2024-12-31 00:00:00 EST          | 8.642105  | 11.1     |


# Performance Tips

For optimal performance, use Polars native operations instead of Python loops (vectorized operations are significantly faster). Always filter early by applying filters before aggregations to reduce data size. For complex queries on large datasets, consider using lazy evaluation with `.lazy()` to optimize the query execution plan. When saving or loading large datasets, prefer Parquet format over CSV as it's much faster and more efficient. Finally, select only the columns you need rather than loading entire datasets into memory.


# Next Steps

See [practical examples](05-examples.md) of weather data analysis with weathervault.
