From 0f48a28765b94bf013d03167f2f4603c5993619f Mon Sep 17 00:00:00 2001 From: Jan Sebastian Rothe Date: Tue, 7 May 2024 16:07:24 +0200 Subject: [PATCH] fixed a reference week check --- pyproject.toml | 2 +- src/arbmark/functions/reference.py | 11 +++++------ tests/test_reference.py | 10 +++++++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5576d62..34f99b7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ssb-arbmark-fagfunksjoner" -version = "0.0.17" +version = "0.0.18" description = "SSB Arbeidsmarked og lønn Fag-fellesfunksjoner" authors = ["Jan Sebastian Rothe "] license = "MIT" diff --git a/src/arbmark/functions/reference.py b/src/arbmark/functions/reference.py index 1fe80f4..152044d 100644 --- a/src/arbmark/functions/reference.py +++ b/src/arbmark/functions/reference.py @@ -118,13 +118,12 @@ def ref_week( ) ) - # Calculate the week numbers using pandas with Monday as the starting day - from_weeks = from_dates.dt.isocalendar().week - to_weeks = to_dates.dt.isocalendar().week - ref_weeks = ref_days.dt.isocalendar().week + # Determine the start and end of the reference week + start_of_week = ref_days - pd.to_timedelta(ref_days.dt.weekday, unit="d") + end_of_week = start_of_week + pd.Timedelta(days=6) - # Check if any of the weeks between from_dates and to_dates is the reference week - result = np.logical_and(from_weeks <= ref_weeks, ref_weeks <= to_weeks) + # Check if the date range overlaps with the reference week + result = np.logical_and(from_dates <= end_of_week, to_dates >= start_of_week) # Return the result as a series of boolean values return pd.Series(result, dtype="boolean") diff --git a/tests/test_reference.py b/tests/test_reference.py index 0920e95..8c73070 100644 --- a/tests/test_reference.py +++ b/tests/test_reference.py @@ -24,9 +24,13 @@ def test_ref_day_outside_range() -> None: def test_ref_week_within_range() -> None: - from_dates = pd.Series(pd.to_datetime(["2023-01-22", "2023-04-01"])) - to_dates = pd.Series(pd.to_datetime(["2023-01-31", "2023-04-15"])) - expected = pd.Series([True, True]) + from_dates = pd.Series( + pd.to_datetime(["2022-02-20", "2022-04-01", "2022-01-01", "2022-12-01"]) + ) + to_dates = pd.Series( + pd.to_datetime(["2022-02-28", "2022-04-11", "2022-01-31", "2022-12-31"]) + ) + expected = pd.Series([True, True, True, True]) assert ( ref_week(from_dates, to_dates) == expected ).all(), "Reference week within range test failed"