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 tick_calendar wday #304

Merged
merged 2 commits into from
Nov 7, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Fixes

- Use `wday=0` for Mondays in `tick_calendar` (like `calendar_day_of_week`).

## 0.1.5

### Features
Expand Down
2 changes: 1 addition & 1 deletion temporian/core/operators/test/test_tick_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
17 changes: 14 additions & 3 deletions temporian/implementation/numpy/operators/tick_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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["*"]],
Expand Down Expand Up @@ -66,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():
Expand Down