Get Temperature Data by Location with Python

Author:Murphy  |  View: 20934  |  Time: 2025-03-23 18:35:17

Introduction

I have been working on some projects when I needed to find the average temperature by location to be able to add that as a possible variable to my model. After some time researching, I found this great and easy-to-use package in Python called meteostat.

This pack has some interesting methods and one was exactly what I needed: if I enter a latitude and longitude, it retrieves me the historical temperature for that place. Additionally, you can ask it by a timeframe. So, you can get only an year or multiple ones.

Another cool feature is that you can request your data to be daily or monthly. Amazing!

Let's see how the code works.

Code

First, let's install meteostat with pip install meteostat. Next, we can import the needed packages.

# Import packages
import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
from meteostat import Point, Monthly

If you visit the documentation, you will see this get started code, which is basically what you need to make that work. We set a time frame – I used 2022. Then we create a variable to hold the lat-long information. I will use this Point(40.78325, -73.96565) , which is the lat-long of the Central Park in NYC. Finally, we will ask for Monthly data between the dates chosen and we'll fetch the data.

# Set time period
start = datetime(2022, 1, 1)
end = datetime(2022, 12, 31)

# Create Point for New York, NY
tpr = Point(40.78325, -73.96565)

# Get monthly data for 2022
data = Monthly(tpr, start, end)
data = data.fetch()

That is pretty much what you need to do. Here's what the fetched data looks like.

2022 temperatures in Celsius from NYC – Manhattan. Image by the author.

If you want daily data, instead of importing Monthly, import Daily and replace it in the code.

You can plot the data easily too with Pandas.

# Plot line chart including average, minimum and maximum temperature
data.plot(y=['tavg', 'tmin', 'tmax'], 
          title='Temperature 2022 - Central Park, NYC')
plt.show()

The code yields the plot as follows.

Plot of the temperatures. Image by the author.

If we want to look at the stations that gives us these results, here is the code.

# Import Meteostat library
from meteostat import Stations

# Get nearby weather stations from our interest point
stations = Stations()
stations = stations.nearby(40.78325, -73.96565)
# Fetching 5 stations
station = stations.fetch(5)

# Print DataFrame
pd.DataFrame(station)
5 weather stations near the Central Park. Image by the author.

Before You Go

That's pretty much it. A quick, but useful tutorial.

GitHub repo:

Studying/Python/meteostat at master · gurezende/Studying

If you need temperature data for your project, I believe meteostat can help you. Check their documentation for more information.

If you liked this content, be sure to follow me for more.

Gustavo Santos – Medium

Also, find me on LinkedIn or book some time to chat with me about Data Science on topmate.

Reference

Python Library | Meteostat Developers

Tags: Data Science Meteostat Python Temperature Weather

Comment