Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of time series with mixed timezone offsets #68

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ __pycache__/*
.cache/*
.*.swp
*/.ipynb_checkpoints/*
.ipynb_checkpoints/*
.DS_Store

# Project files
Expand Down
7 changes: 3 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ exclude: '^docs/conf.py'

repos:
- repo: https://github.com/psf/black
rev: 23.1.0
rev: 24.8.0
hooks:
- id: black
language_version: python3.9

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: check-added-large-files
Expand All @@ -24,7 +23,7 @@ repos:
args: ['--fix=no']

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 7.1.1
hooks:
- id: flake8
args: ['--max-line-length=88'] # default of Black
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
PyScaffold helps you to put up the scaffold of your new Python project.
Learn more under: https://pyscaffold.org/
"""

from setuptools import setup

if __name__ == "__main__":
Expand Down
7 changes: 5 additions & 2 deletions src/pywaterinfo/waterinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(
self._request = requests_cache.CachedSession(
cache_name="pywaterinfo_cache.sqlite",
use_memory=False,
cache_control=True,
cache_control=False,
expire_after=CACHE_RETENTION,
stale_if_error=False,
use_cache_dir=True,
Expand Down Expand Up @@ -631,7 +631,10 @@ def get_timeseries_values(
df[key_name] = section[key_name]
# convert datetime objects to Pandas timestamp
if "Timestamp" in df.columns:
df["Timestamp"] = pd.to_datetime(df["Timestamp"])
# round trip via UTC to handle mixed time series
df["Timestamp"] = pd.to_datetime(
df["Timestamp"], utc=True
).dt.tz_convert(timezone)
time_series.append(df)

return pd.concat(time_series)
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

@pytest.fixture
def patch_retention(monkeypatch):
retention = datetime.timedelta(seconds=0.0001)
retention = datetime.timedelta(seconds=2)
monkeypatch.setattr("pywaterinfo.waterinfo.CACHE_RETENTION", retention)


Expand Down
12 changes: 6 additions & 6 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ def test_cache_hic(hic_cached_connection):
assert res.from_cache


{"request": "getTimeseriesValueLayer", "timeseriesgroup_id": "156207"}


def test_clear_cache(vmm_cached_connection):
"""Cache is cleared."""
data, res = vmm_cached_connection.request_kiwis({"request": "getRequestInfo"})
Expand Down Expand Up @@ -66,15 +63,18 @@ def test_cache_retention(patch_retention):
expired cache and check for `from_cache` in a new request.
"""
vmm = Waterinfo("vmm", cache=True)
# patch/exclude cache control to use retention time only
vmm._request.settings.cache_control = False
vmm.clear_cache()
_, res = vmm.request_kiwis({"request": "getRequestInfo"})
assert not res.from_cache

time.sleep(1)
vmm._request.cache
_, res = vmm.request_kiwis({"request": "getRequestInfo"})
assert vmm._request.cache.contains( # noqa
url="https://download.waterinfo.be/tsmdownload/KiWIS/KiWIS?"
"request=getRequestInfo&service=kisters&type=QueryServices&"
"format=json&datasource=1&timezone=UTC"
)
time.sleep(1)
assert res.is_expired

vmm._request.cache.delete(expired=True)
Expand Down
20 changes: 17 additions & 3 deletions tests/test_waterinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,24 @@ def test_overwrite_timezone(self, connection, request):
end="20190501 14:10",
timezone="CET",
)
assert is_datetime64tz_dtype(df["Timestamp"])
assert df["Timestamp"].dt.tz == datetime.timezone(
datetime.timedelta(seconds=7200)
assert isinstance(df["Timestamp"].dtype, pd.DatetimeTZDtype)
assert df["Timestamp"].dt.tz == pytz.timezone("CET")

def test_timezone_mixed_timezone(self, connection, request):
"""Queries resulting in mixed timezone offsets are handled without warning

See also https://github.com/fluves/pywaterinfo/issues/67
"""
connection = request.getfixturevalue(connection)
df = connection.get_timeseries_values(
"69928042",
start="2013-03-21 00:00:00",
end="2013-04-01 23:00:00",
timezone="CET",
returnfields="Timestamp,Value",
)
assert isinstance(df["Timestamp"].dtype, pd.DatetimeTZDtype)
assert df["Timestamp"].dt.tz == pytz.timezone("CET")

def test_start_end_timezone(self, connection, request):
"""pywaterinfo can handle start/end dates already containing tz info"""
Expand Down
Loading