From 687d77d4eeca6c2cc04c9152244f66475fdbb069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Braulio=20R=C3=ADos?= Date: Thu, 2 Nov 2023 17:08:12 -0300 Subject: [PATCH 1/2] Use wday=0 for Monday in tick_calendar() --- .../implementation/numpy/operators/tick_calendar.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/temporian/implementation/numpy/operators/tick_calendar.py b/temporian/implementation/numpy/operators/tick_calendar.py index 7e46500fd..5646291c3 100644 --- a/temporian/implementation/numpy/operators/tick_calendar.py +++ b/temporian/implementation/numpy/operators/tick_calendar.py @@ -31,6 +31,14 @@ def __init__(self, operator: TickCalendar) -> None: assert isinstance(operator, TickCalendar) super().__init__(operator) + def _wday_py_to_cpp(self, py_wday: int) -> int: + """Converts wday number from Python (wday=0 for Monday) to C++ + convention (wday=0 for Sunday). + This is required to keep coherency between calendar_day_of_week() + operator (which uses datetime.weekday()) and tick_calendar() (which + uses tm_wday in tick_calendar.cc).""" + return (py_wday + 1) % 7 + def _get_arg_range( self, arg_value: Union[int, Literal["*"]], @@ -88,8 +96,8 @@ def __call__(self, input: EventSet) -> Dict[str, EventSet]: max_mday=mday_range[1], min_month=month_range[0], max_month=month_range[1], - min_wday=wday_range[0], - max_wday=wday_range[1], + min_wday=self._wday_py_to_cpp(wday_range[0]), + max_wday=self._wday_py_to_cpp(wday_range[1]), ) output_evset.set_index_value( index_key, From 9bb7f9a8631d32e919607d0f27b78d23bd98d87e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Braulio=20R=C3=ADos?= Date: Thu, 2 Nov 2023 17:46:31 -0300 Subject: [PATCH 2/2] Fix tick_calendar tests, bugfix & update CHANGELOG --- CHANGELOG.md | 2 ++ temporian/core/operators/test/test_tick_calendar.py | 2 +- .../implementation/numpy/operators/tick_calendar.py | 13 ++++++++----- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93eda9e3a..224c342fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ ### Fixes +- Use `wday=0` for Mondays in `tick_calendar` (like `calendar_day_of_week`). + ## 0.1.5 ### Features diff --git a/temporian/core/operators/test/test_tick_calendar.py b/temporian/core/operators/test/test_tick_calendar.py index 59c43f6b7..00f847c51 100644 --- a/temporian/core/operators/test/test_tick_calendar.py +++ b/temporian/core/operators/test/test_tick_calendar.py @@ -145,7 +145,7 @@ def test_weekdays(self): timestamps=timestamps, ) - result = evset.tick_calendar(hour="*", wday=6) + result = evset.tick_calendar(hour="*", wday=5) assertOperatorResult(self, result, expected_evset, check_sampling=False) diff --git a/temporian/implementation/numpy/operators/tick_calendar.py b/temporian/implementation/numpy/operators/tick_calendar.py index 5646291c3..b7ef5cddd 100644 --- a/temporian/implementation/numpy/operators/tick_calendar.py +++ b/temporian/implementation/numpy/operators/tick_calendar.py @@ -74,9 +74,12 @@ def __call__(self, input: EventSet) -> Dict[str, EventSet]: month_range = self._get_arg_range( self.operator.month, self.operator.month_max_range() ) - wday_range = self._get_arg_range( - self.operator.wday, self.operator.wday_max_range() - ) + + # Weekday: convert python (wday=0 for Mon) to C++ (wday=0 for Sun) + wday = self.operator.wday + if wday != "*": + wday = self._wday_py_to_cpp(wday) + wday_range = self._get_arg_range(wday, self.operator.wday_max_range()) # Fill output EventSet's data for index_key, index_data in input.data.items(): @@ -96,8 +99,8 @@ def __call__(self, input: EventSet) -> Dict[str, EventSet]: max_mday=mday_range[1], min_month=month_range[0], max_month=month_range[1], - min_wday=self._wday_py_to_cpp(wday_range[0]), - max_wday=self._wday_py_to_cpp(wday_range[1]), + min_wday=wday_range[0], + max_wday=wday_range[1], ) output_evset.set_index_value( index_key,