Skip to content

Commit f4c6c7c

Browse files
authored
Merge pull request Project-OSmOSE#81 from mathieudpnt/feature/accept_local_timezone
sunrise/sunset accept local timezones
2 parents eb721aa + 6100d19 commit f4c6c7c

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

src/post_processing/dataclass/data_aplose.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,21 @@ def __getitem__(self, item: int) -> Series:
167167
return self.df.iloc[item]
168168

169169
def change_tz(self, tz: str | tzinfo) -> None:
170-
"""Change the timezone of the DataFrame and of the begin and end Timestamps."""
170+
"""Change the timezone of a DataAplose instance.
171+
172+
Examples
173+
--------
174+
>>> import pytz
175+
>>> data = DataAplose(...)
176+
>>> data.change_tz(pytz.timezone("Etc/GMT-2"))
177+
178+
>>> data = DataAplose(...)
179+
>>> data.change_tz("UTC")
180+
181+
>>> data = DataAplose(...)
182+
>>> data.change_tz("UTC+02:00")
183+
184+
"""
171185
self.df["start_datetime"] = [
172186
elem.tz_convert(tz)
173187
for elem in self.df["start_datetime"]

src/post_processing/utils/filtering_utils.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import csv
77
from typing import TYPE_CHECKING
88

9+
import pytz
910
from pandas import (
1011
DataFrame,
1112
Timedelta,
@@ -19,8 +20,6 @@
1920
if TYPE_CHECKING:
2021
from pathlib import Path
2122

22-
from dateutil.tz import tzoffset
23-
2423
from post_processing.dataclass.detection_filter import DetectionFilter
2524

2625

@@ -176,9 +175,18 @@ def get_dataset(df: DataFrame) -> list[str]:
176175
return datasets if len(datasets) > 1 else datasets[0]
177176

178177

179-
def get_timezone(df: DataFrame) -> tzoffset | list[tzoffset]:
178+
def get_timezone(df: DataFrame):
180179
"""Return timezone(s) from DataFrame."""
181-
timezones = {ts.tz for ts in df["start_datetime"] if ts.tz is not None}
180+
181+
def get_canonical_tz(tz):
182+
if hasattr(tz, "zone") and tz.zone:
183+
return pytz.timezone(tz.zone)
184+
if hasattr(tz, "key"):
185+
return pytz.timezone(tz.key)
186+
return pytz.UTC
187+
188+
timezones = {get_canonical_tz(ts.tzinfo) for ts in df["start_datetime"]}
189+
182190
if len(timezones) == 1:
183191
return next(iter(timezones))
184192
return list(timezones)

tests/test_filtering_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import csv
2-
from datetime import timezone
32
from pathlib import Path
43

54
import pytest
5+
import pytz
66
from pandas import DataFrame, Timedelta, Timestamp, date_range
77

88
from post_processing.utils.filtering_utils import (
@@ -170,10 +170,10 @@ def test_get_dataset(sample_df: DataFrame) -> None:
170170

171171
def test_get_timezone_single(sample_df: DataFrame) -> None:
172172
tz = get_timezone(sample_df)
173-
assert isinstance(tz, timezone)
173+
assert tz == pytz.utc
174174

175-
# %% read DataFrame
176175

176+
# %% read DataFrame
177177

178178
def test_read_dataframe_comma_delimiter(tmp_path: Path) -> None:
179179
csv_file = tmp_path / "test.csv"

0 commit comments

Comments
 (0)