Skip to content

Commit

Permalink
Merge pull request #304 from google/fix-tick-calendar-wday
Browse files Browse the repository at this point in the history
Fix tick_calendar wday
  • Loading branch information
DonBraulio authored Nov 7, 2023
2 parents b4862f5 + 9bb7f9a commit 6e80418
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
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

0 comments on commit 6e80418

Please sign in to comment.