Skip to content

COMPAT: Ensure rolling indexers return intp #35228

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions pandas/_libs/window/indexers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def calculate_variable_window_bounds(

Returns
-------
(ndarray[int64], ndarray[int64])
(ndarray[intp], ndarray[intp])
"""
cdef:
bint left_closed = False
Expand All @@ -62,9 +62,9 @@ def calculate_variable_window_bounds(
if index[num_values - 1] < index[0]:
index_growth_sign = -1

start = np.empty(num_values, dtype='int64')
start = np.empty(num_values, dtype=np.intp)
start.fill(-1)
end = np.empty(num_values, dtype='int64')
end = np.empty(num_values, dtype=np.intp)
end.fill(-1)

start[0] = 0
Expand Down
26 changes: 11 additions & 15 deletions pandas/core/window/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

Returns
-------
A tuple of ndarray[int64]s, indicating the boundaries of each
A tuple of ndarray[intp]s, indicating the boundaries of each
window
"""

Expand Down Expand Up @@ -76,15 +76,15 @@ def get_window_bounds(
closed: Optional[str] = None,
) -> Tuple[np.ndarray, np.ndarray]:

start_s = np.zeros(self.window_size, dtype="int64")
start_s = np.zeros(self.window_size, dtype=np.intp)
start_e = (
np.arange(self.window_size, num_values, dtype="int64")
np.arange(self.window_size, num_values, dtype=np.intp)
- self.window_size
+ 1
)
start = np.concatenate([start_s, start_e])[:num_values]

end_s = np.arange(self.window_size, dtype="int64") + 1
end_s = np.arange(self.window_size, dtype=np.intp) + 1
end_e = start_e + self.window_size
end = np.concatenate([end_s, end_e])[:num_values]
return start, end
Expand Down Expand Up @@ -143,9 +143,9 @@ def get_window_bounds(
else:
index_growth_sign = 1

start = np.empty(num_values, dtype="int64")
start = np.empty(num_values, dtype=np.intp)
start.fill(-1)
end = np.empty(num_values, dtype="int64")
end = np.empty(num_values, dtype=np.intp)
end.fill(-1)

start[0] = 0
Expand Down Expand Up @@ -202,8 +202,8 @@ def get_window_bounds(
) -> Tuple[np.ndarray, np.ndarray]:

return (
np.zeros(num_values, dtype=np.int64),
np.arange(1, num_values + 1, dtype=np.int64),
np.zeros(num_values, dtype=np.intp),
np.arange(1, num_values + 1, dtype=np.intp),
)


Expand Down Expand Up @@ -249,9 +249,9 @@ def get_window_bounds(
"Forward-looking windows don't support setting the closed argument"
)

start = np.arange(num_values, dtype="int64")
start = np.arange(num_values, dtype=np.intp)
end_s = start[: -self.window_size] + self.window_size
end_e = np.full(self.window_size, num_values, dtype="int64")
end_e = np.full(self.window_size, num_values, dtype=np.intp)
end = np.concatenate([end_s, end_e])

return start, end
Expand Down Expand Up @@ -303,18 +303,14 @@ def get_window_bounds(
start, end = indexer.get_window_bounds(
len(indicies), min_periods, center, closed
)
start = start.astype(np.int64)
end = end.astype(np.int64)
# Cannot use groupby_indicies as they might not be monotonic with the object
# we're rolling over
window_indicies = np.arange(
window_indicies_start, window_indicies_start + len(indicies),
)
window_indicies_start += len(indicies)
# Extend as we'll be slicing window like [start, end)
window_indicies = np.append(
window_indicies, [window_indicies[-1] + 1]
).astype(np.int64)
window_indicies = np.append(window_indicies, [window_indicies[-1] + 1])
start_arrays.append(window_indicies.take(start))
end_arrays.append(window_indicies.take(end))
start = np.concatenate(start_arrays)
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2231,9 +2231,7 @@ def _create_blocks(self, obj: FrameOrSeries):
"""
# Ensure the object we're rolling over is monotonically sorted relative
# to the groups
groupby_order = np.concatenate(
list(self._groupby.grouper.indices.values())
).astype(np.int64)
groupby_order = np.concatenate(list(self._groupby.grouper.indices.values()))
obj = obj.take(groupby_order)
return super()._create_blocks(obj)

Expand Down