'Is there a way to specify the timezone in the Meteostat API to retrieve historical daily weather information?

I'm using the Meteostat Python API. I'm trying to fetch historical daily weather data. For doing this, the Daily function can be used. This function receives an start datetime and an end datetime.

I would like to get daily historical weather data with respect to the local timezone. Is there a way to specify the timezone of this datetime? If not, with respect to what timezone should the start and end datetimes be represented?



Solution 1:[1]

Actually, at the time of writing this answer, the timezone option is only implemented in the Hourly class of the Meteosat module.

The workaround I found is to create a new class TimezoneDaily inherited from the original Daily class already implemented. I added in it the timezone conversion method from the Hourly class. Here is the piece of code:

from meteostat import Daily, Point
import pandas as pd
from typing import Union
from datetime import datetime, timedelta
import pytz
import numpy as np


class TimezoneDaily(Daily):
    def __init__(self,
             loc: Union[pd.DataFrame, Point, list, str],
             start: datetime = None, end: datetime = None,
             timezone: str = None, model: bool = True, flags: bool = False):
    # Inherit the attributes from Daily class
    super(Daily, self).__init__()
    self._loc = loc
    self._start = start
    self._end = end
    self._timezone = timezone
    self._model = model
    self._flags = flags
    # Download data as annual chunks
    self.chunked = True

    # Set time zone and adapt period
    self._set_time(start, end, timezone)
    # Initialize time series
    self._init_time_series(loc, start, end, model, flags)

    # Implementation from hourly.py located in:
    # https://github.com/meteostat/meteostat-python/blob/master/meteostat/interface/hourly.py
    def _set_time(self, start: datetime = None, end: datetime = None,
              timezone: str = None) -> None:

        if timezone in pytz.all_timezones:

            # Save timezone
            self._timezone = timezone

            if start and end:
                # Initialize time zone
                timezone = pytz.timezone(self._timezone)
                # Set start and end date
                start = timezone.localize(start).astimezone(pytz.utc)
                end = timezone.localize(end).astimezone(pytz.utc)
        else:
            print("Timezone is is not implemented in pytz.\n"
                  "Please use a timezone inside pytz.all_timezones list.")
    
        # Query data as chunks of one year
        if self.chunked:
            self._annual_steps = [
                (start + timedelta(days=365 * i)).year
                for i in range(end.year - start.year + 1)
            ]

        # Since datetime have already been converted to datetime object in
        # specified timezone, the TZ can be avoided to prevent deprecating 
        # warning in Numpy.
        self._start = np.datetime64(start.replace(tzinfo=None))
        self._end = np.datetime64(end.replace(tzinfo=None))

Hope this solves your problem!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eidal