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, which offer:

  • fast performance (written in Rust)
  • expressive API similar to pandas
  • lazy evaluation support
  • excellent datetime handling
import weathervault as wv
import polars as pl

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

Basic DataFrame Operations

Inspecting Data

# 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)
statisticidtimetempdew_pointrhwdwsatmos_presceil_hgtvisibility
strstrstrf64f64f64f64f64f64f64f64
"count""13420""13420"13040.013043.013040.012320.013039.011549.011189.013043.0
"null_count""0""0"380.0377.0380.01100.0381.01871.02231.0377.0
"mean"null"2024-06-29 02:45:53.329359-04:…14.1575316.0400960.745429184.9618514.6775981016.3369910586.51327214618.577628
"std"nullnull9.1009419.59002518.151291109.7249442.3730318.1714069747.5311123756.852125
"min""725030-14732""2024-01-01 00:51:00-05:00"-11.1-19.415.110.00.0982.060.0200.0
"25%"null"2024-03-29 00:59:00-04:00"6.7-1.146.070.03.11011.61067.016000.0
"50%"null"2024-06-27 20:00:00-04:00"14.46.759.6180.04.61016.17620.016093.0
"75%"null"2024-09-27 16:51:00-04:00"22.013.376.8300.06.21021.222000.016093.0
"max""725030-14732""2024-12-31 23:59:00-05:00"35.623.9100.0360.017.51048.622000.016093.0

Selecting Columns

# Select specific columns
temps = weather.select(["time", "temp", "dew_point"])
temps
shape: (13_420, 3)
timetempdew_point
datetime[μs, America/New_York]f64f64
2024-01-01 00:51:00 EST6.1-2.8
2024-01-01 01:00:00 EST6.1-2.8
2024-01-01 01:51:00 EST6.1-2.8
2024-01-01 02:51:00 EST5.6-2.2
2024-01-01 03:51:00 EST5.6-1.7
………
2024-12-31 23:20:00 EST7.24.4
2024-12-31 23:43:00 EST7.25.0
2024-12-31 23:51:00 EST7.25.0
2024-12-31 23:59:00 ESTnullnull
2024-12-31 23:59:00 ESTnullnull
# Select multiple columns with expressions
weather.select([
    pl.col("time"),
    pl.col("temp"),
    pl.col("ws").alias("wind_speed")
])
shape: (13_420, 3)
timetempwind_speed
datetime[μs, America/New_York]f64f64
2024-01-01 00:51:00 EST6.13.1
2024-01-01 01:00:00 EST6.13.1
2024-01-01 01:51:00 EST6.14.1
2024-01-01 02:51:00 EST5.64.1
2024-01-01 03:51:00 EST5.63.1
………
2024-12-31 23:20:00 EST7.27.7
2024-12-31 23:43:00 EST7.24.6
2024-12-31 23:51:00 EST7.25.7
2024-12-31 23:59:00 ESTnullnull
2024-12-31 23:59:00 ESTnullnull

Filtering Rows

# Filter by temperature
hot_days = weather.filter(pl.col("temp") > 30)
hot_days
shape: (246, 10)
idtimetempdew_pointrhwdwsatmos_presceil_hgtvisibility
strdatetime[μs, America/New_York]f64f64f64i32f64f64i32i32
"725030-14732"2024-06-19 12:51:00 EDT31.718.946.7null2.61026.5762016093
"725030-14732"2024-06-20 10:51:00 EDT31.116.742.12304.61026.0762016093
"725030-14732"2024-06-20 11:00:00 EDT31.116.742.12304.61026.0null16000
"725030-14732"2024-06-20 11:51:00 EDT31.116.742.12404.61025.8762016093
"725030-14732"2024-06-20 12:51:00 EDT33.316.737.12104.11025.3762016093
…………………………
"725030-14732"2024-08-28 18:51:00 EDT31.720.050.03503.61013.2487716093
"725030-14732"2024-08-28 19:51:00 EDT31.720.050.0103.61013.92200016093
"725030-14732"2024-08-28 20:00:00 EDT31.720.050.0103.61013.9null16000
"725030-14732"2024-08-28 20:51:00 EDT30.620.053.23503.11014.62200016093
"725030-14732"2024-08-28 21:03:00 EDT30.620.053.23502.6null2200016093
# 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)
idtimetempdew_pointrhwdwsatmos_presceil_hgtvisibility
strdatetime[μs, America/New_York]f64f64f64i32f64f64i32i32
"725030-14732"2024-06-01 12:51:00 EDT26.16.127.9null1.51020.42200016093
"725030-14732"2024-06-01 13:51:00 EDT27.26.126.1302.11019.62200016093
"725030-14732"2024-06-01 14:00:00 EDT27.26.126.1302.11019.62200016000
"725030-14732"2024-06-01 14:51:00 EDT27.85.023.43105.11019.02200016093
"725030-14732"2024-06-01 15:51:00 EDT28.93.920.32503.61018.42200016093
…………………………
"725030-14732"2024-08-31 14:51:00 EDT25.617.862.11503.61017.285316093
"725030-14732"2024-08-31 15:43:00 EDT26.718.360.01803.6null128016093
"725030-14732"2024-08-31 15:51:00 EDT26.117.860.31704.61016.1128016093
"725030-14732"2024-08-31 16:51:00 EDT25.617.259.81604.61015.5137216093
"725030-14732"2024-08-31 17:00:00 EDT25.617.259.81604.61015.5null16000
# 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)
idtimetempdew_pointrhwdwsatmos_presceil_hgtvisibility
strdatetime[μs, America/New_York]f64f64f64i32f64f64i32i32
"725030-14732"2024-01-01 00:51:00 EST6.1-2.852.92303.11015.7128016093
"725030-14732"2024-01-01 01:00:00 EST6.1-2.852.92303.11015.7null16000
"725030-14732"2024-01-01 01:51:00 EST6.1-2.852.92704.11015.5182916093
"725030-14732"2024-01-01 02:51:00 EST5.6-2.257.22704.11015.5109716093
"725030-14732"2024-01-01 03:51:00 EST5.6-1.759.42803.11015.1121916093
…………………………
"725030-14732"2024-12-31 22:51:00 EST6.74.485.3408.8999.94274828
"725030-14732"2024-12-31 23:09:00 EST6.74.485.3406.2null27416093
"725030-14732"2024-12-31 23:20:00 EST7.24.482.4507.7null33516093
"725030-14732"2024-12-31 23:43:00 EST7.25.085.9404.6null67116093
"725030-14732"2024-12-31 23:51:00 EST7.25.085.9405.7998.967116093

Time-Based Analysis

Extract Time Components

# 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)
timeyearmonthdayhour
datetime[μs, America/New_York]i32i8i8i8
2024-01-01 00:51:00 EST2024110
2024-01-01 01:00:00 EST2024111
2024-01-01 01:51:00 EST2024111
2024-01-01 02:51:00 EST2024112
2024-01-01 03:51:00 EST2024113
……………
2024-12-31 23:20:00 EST2024123123
2024-12-31 23:43:00 EST2024123123
2024-12-31 23:51:00 EST2024123123
2024-12-31 23:59:00 EST2024123123
2024-12-31 23:59:00 EST2024123123

Filter by Time Range

# 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)
idtimetempdew_pointrhwdwsatmos_presceil_hgtvisibility
strdatetime[μs, America/New_York]f64f64f64i32f64f64i32i32
"725030-14732"2024-01-01 00:51:00 EST6.1-2.852.92303.11015.7128016093
"725030-14732"2024-01-01 01:00:00 EST6.1-2.852.92303.11015.7null16000
"725030-14732"2024-01-01 01:51:00 EST6.1-2.852.92704.11015.5182916093
"725030-14732"2024-01-01 02:51:00 EST5.6-2.257.22704.11015.5109716093
"725030-14732"2024-01-01 03:51:00 EST5.6-1.759.42803.11015.1121916093
…………………………
"725030-14732"2024-01-31 22:00:00 EST3.9-1.169.93102.11018.4null16000
"725030-14732"2024-01-31 22:51:00 EST3.9-1.169.93101.51018.597516093
"725030-14732"2024-01-31 23:51:00 EST4.4-1.167.53102.11018.091416093
"725030-14732"2024-01-31 23:59:00 ESTnullnullnullnullnullnullnullnull
"725030-14732"2024-01-31 23:59:00 ESTnullnullnullnullnullnullnullnull

Resample to Different Time Intervals

# 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)
timemean_tempmin_tempmax_tempmean_wind_speed
datetime[μs, America/New_York]f64f64f64f64
2024-01-01 00:00:00 EST5.72.27.23.011765
2024-01-02 00:00:00 EST2.7375-1.16.14.2625
2024-01-03 00:00:00 EST3.968751.76.14.0125
2024-01-04 00:00:00 EST3.771875-1.76.76.6375
2024-01-05 00:00:00 EST0.540625-2.23.35.34375
……………
2024-12-27 00:00:00 EST2.003125-2.26.12.03125
2024-12-28 00:00:00 EST6.7595243.910.01.371429
2024-12-29 00:00:00 EST10.6459467.814.44.045946
2024-12-30 00:00:00 EST12.4910.013.96.8025
2024-12-31 00:00:00 EST8.6421056.711.14.684211
# 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)
timemean_tempmin_tempmax_tempmean_humidity
datetime[μs, America/New_York]f64f64f64f64
2024-01-01 00:00:00 EST3.007711-7.215.064.971476
2024-02-01 00:00:00 EST4.061704-4.415.657.237316
2024-03-01 00:00:00 EST7.999189-1.121.760.362252
2024-04-01 00:00:00 EDT11.377442.828.361.346553
2024-05-01 00:00:00 EDT17.3868179.430.068.012576
……………
2024-08-01 00:00:00 EDT24.29973415.034.464.849023
2024-09-01 00:00:00 EDT20.49772913.928.964.147493
2024-10-01 00:00:00 EDT16.5139517.227.852.337659
2024-11-01 00:00:00 EDT11.3681630.026.751.905204
2024-12-01 00:00:00 EST4.285727-11.115.660.722837

Handling Missing Values

Identify Missing Data

# Count nulls per column
null_counts = weather.null_count()
null_counts
shape: (1, 10)
idtimetempdew_pointrhwdwsatmos_presceil_hgtvisibility
u32u32u32u32u32u32u32u32u32u32
00380377380110038118712231377
# Percentage of missing values
missing_pct = (weather.null_count() / weather.height) * 100
missing_pct
shape: (1, 10)
idtimetempdew_pointrhwdwsatmos_presceil_hgtvisibility
f64f64f64f64f64f64f64f64f64f64
0.00.02.8315952.809242.8315958.1967212.83904613.94187816.6244412.80924

Fill Missing Values

# Forward fill
weather_filled = weather.with_columns([
    pl.col("temp").forward_fill().alias("temp_filled")
])
# Fill with a specific value
weather_filled = weather.with_columns([
    pl.col("temp").fill_null(0)
])
# 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:

# 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)
temptemp_f
f64f64
6.142.98
6.142.98
6.142.98
5.642.08
5.642.08
……
7.244.96
7.244.96
7.244.96
nullnull
nullnull
# 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)
temptemp_k
f64f64
6.1279.25
6.1279.25
6.1279.25
5.6278.75
5.6278.75
……
7.2280.35
7.2280.35
7.2280.35
nullnull
nullnull

Aggregations and Statistics

Basic Statistics

# 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_tempmedian_tempstd_tempq25_tempq75_temp
f64f64f64f64f64
14.15753114.49.1009416.722.0

Group By Operations

# 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)
timemean_tempmin_tempmax_tempmean_humiditymean_wind_speednum_observations
i8f64f64f64f64f64u32
13.007711-7.215.064.9714765.2624381238
24.061704-4.415.657.2373164.7215481051
37.999189-1.121.760.3622525.4943241140
411.377442.828.361.3465535.3512981148
517.3868179.430.068.0125764.1820311185
…………………
824.29973415.034.464.8490234.2459151158
920.49772913.928.964.1474934.1776731088
1016.5139517.227.852.3376594.090421057
1111.3681630.026.751.9052045.0887761012
124.285727-11.115.660.7228374.9509371153
# 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)
timemean_tempmean_wind_speed
i8f64f64
012.9935254.128297
110.8364794.169147
214.1556573.851529
312.402453.979065
410.1592924.102832
………
1912.8628425.120036
2016.4407874.784094
2114.1348244.596941
2211.8115384.510806
2315.0971924.297036

Combining Multiple Stations

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

# 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)
idnamemean_tempmin_tempmax_temp
strstrf64f64f64
"725030-14732""LA GUARDIA AIRPORT"14.157531-11.135.6
"744860-94789""JOHN F KENNEDY INTERNATIONAL A…13.798241-10.634.4
"725020-14734""NEWARK LIBERTY INTERNATIONAL A…14.727874-11.737.2

Exporting Data

To CSV

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

To Parquet

# 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

# 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

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

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

Data Completeness Report

# 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)
variabletotal_obsmissingcompleteness_pct
stri64i64f64
"id"134200100.0
"time"134200100.0
"temp"1342038097.168405
"dew_point"1342037797.19076
"rh"1342038097.168405
"wd"13420110091.803279
"ws"1342038197.160954
"atmos_pres"13420187186.058122
"ceil_hgt"13420223183.375559
"visibility"1342037797.19076

Advanced: Lazy Evaluation

For very large datasets, use Polars lazy API:

# 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)
timemean_tempmax_temp
datetime[μs, America/New_York]f64f64
2024-01-01 00:00:00 EST5.77.2
2024-01-02 00:00:00 EST2.73756.1
2024-01-03 00:00:00 EST3.968756.1
2024-01-04 00:00:00 EST3.7718756.7
2024-01-05 00:00:00 EST0.5406253.3
………
2024-12-27 00:00:00 EST2.0031256.1
2024-12-28 00:00:00 EST6.75952410.0
2024-12-29 00:00:00 EST10.64594614.4
2024-12-30 00:00:00 EST12.4913.9
2024-12-31 00:00:00 EST8.64210511.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 of weather data analysis with weathervault.