Skip to main content

Time Series

Meteostat provides access to time series data for thousands of weather stations worldwide. These time series are provided in different granularities and can be consumed through a single interface.

🚀 Example

Let's fetch some hourly data for Atlanta, USA from January 1 to December 31, 2018:

# Import Meteostat library and dependencies
from datetime import datetime
import meteostat as ms

# Set time period
start = datetime(2018, 1, 1)
end = datetime(2018, 12, 31, 23, 59)

# Get hourly data
ts = ms.hourly(ms.Station(id='72219'), start, end)
df = ts.fetch()

# Print DataFrame
print(df)

🎯 Location Input

When working with time series data in Meteostat, you can specify weather stations and geographical points as location input. We are using the hourly function as an example, but the same options apply to other time series functions.

Performance Tip

When working with the default providers, you may specify a new Station object with just the station ID. That will improve performance by avoiding unnecessary metadata lookups. Example: ms.Station(id='10637').

Station ID

You can access time series data by specifying a weather station using its Meteostat ID:

ts = ms.hourly('10637', START, END)

You can also provide a list of station IDs to access data from multiple stations simultaneously:

ts = ms.hourly(['10637', '10635'], START, END)

Station

You can pass a Station object to specify a weather station. This could also be a list of Station objects. Passing a Station object allows you to specify a station's metadata yourself. This is useful if you want to include stations that are not (yet) part of the Meteostat database.

STATION = ms.Station(
id='10637',
name='Frankfurt Airport',
country='DE',
region='HE',
lat=50.05,
lon=8.6842,
elevation=100
)

ts = ms.hourly(STATION, START, END)

DataFrame

You can pass a Pandas DataFrame with an index named id. This is mainly used for accessing data for a list of nearby weather stations obtained from the meteostat.stations.nearby function.

# Specify location and time range
POINT = ms.Point(50.1155, 8.6842, 113)

# Get nearby weather stations as Pandas DataFrame
stations = ms.stations.nearby(POINT, limit=4)

# Get hourly data
ts = ms.hourly(stations, START, END)

Point

You can pass a Point object to specify a geographical point. This could also be a list of Point objects. Passing a Point object allows you to specify a location's metadata yourself. This is useful if you want to access data for a geographical point, not a specific weather station.

warning

Note that when using a Point object, only providers that support accessing data by geographical point can be used.

POINT = ms.Point(50.05, 8.6842, 100)

ts = ms.hourly(POINT, START, END, providers=[ms.Provider.METNO_FORECAST])

🔀 Merging Time Series

Meteostat allows you to merge multiple time series objects into a single object using the merge function. This is useful when you want to combine data from different sources or time periods into one cohesive dataset. The merge function can handle various types of time series data, including hourly, daily, and monthly data. However, it is important to ensure that the time series being merged are compatible in terms of their structure and parameters.

Requirements

  • All time series objects to be merged must have the same granularity (e.g., all hourly, all daily, or all monthly).
  • In case of hourly data, the time zones of the time series objects must match.

Example

from datetime import date
import meteostat as ms

# Specify time range
START1 = date(2020, 1, 1)
END1 = date(2020, 1, 10)
START2 = date(2020, 1, 11)
END2 = date(2020, 1, 20)

# Get daily data for two different periods
ts1 = ms.daily('72503', START1, END1)
ts2 = ms.daily('72503', START2, END2)

# Merge time series
ts = ms.merge([ts1, ts2])

# Fetch combined data as Pandas DataFrame
df = ts.fetch()

print(df)

👀 Learn More

🔍 API

meteostat.TimeSeries