Skip to content

Commit

Permalink
Merge pull request #52 from statisticsnorway/ref-tuesday
Browse files Browse the repository at this point in the history
fixed a reference week check
  • Loading branch information
uhhssb authored May 7, 2024
2 parents c0d4d26 + 0f48a28 commit fae40c8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
Expand Down
11 changes: 5 additions & 6 deletions src/arbmark/functions/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 7 additions & 3 deletions tests/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit fae40c8

Please sign in to comment.