Skip to content

Commit 344fa71

Browse files
Backport PR pandas-dev#35875 on branch 1.1.x (COMPAT: Ensure rolling indexers return intp during take operations) (pandas-dev#37471)
Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 2cac884 commit 344fa71

File tree

7 files changed

+15
-33
lines changed

7 files changed

+15
-33
lines changed

pandas/core/window/indexers.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from pandas._libs.window.indexers import calculate_variable_window_bounds
88
from pandas.util._decorators import Appender
99

10+
from pandas.core.dtypes.common import ensure_platform_int
11+
1012
from pandas.tseries.offsets import Nano
1113

1214
get_window_bounds_doc = """
@@ -296,9 +298,9 @@ def get_window_bounds(
296298
start_arrays = []
297299
end_arrays = []
298300
window_indicies_start = 0
299-
for key, indicies in self.groupby_indicies.items():
301+
for key, indices in self.groupby_indicies.items():
300302
if self.index_array is not None:
301-
index_array = self.index_array.take(indicies)
303+
index_array = self.index_array.take(ensure_platform_int(indices))
302304
else:
303305
index_array = self.index_array
304306
indexer = self.rolling_indexer(
@@ -307,22 +309,22 @@ def get_window_bounds(
307309
**self.indexer_kwargs,
308310
)
309311
start, end = indexer.get_window_bounds(
310-
len(indicies), min_periods, center, closed
312+
len(indices), min_periods, center, closed
311313
)
312314
start = start.astype(np.int64)
313315
end = end.astype(np.int64)
314316
# Cannot use groupby_indicies as they might not be monotonic with the object
315317
# we're rolling over
316318
window_indicies = np.arange(
317-
window_indicies_start, window_indicies_start + len(indicies),
319+
window_indicies_start, window_indicies_start + len(indices),
318320
)
319-
window_indicies_start += len(indicies)
321+
window_indicies_start += len(indices)
320322
# Extend as we'll be slicing window like [start, end)
321323
window_indicies = np.append(
322324
window_indicies, [window_indicies[-1] + 1]
323325
).astype(np.int64)
324-
start_arrays.append(window_indicies.take(start))
325-
end_arrays.append(window_indicies.take(end))
326+
start_arrays.append(window_indicies.take(ensure_platform_int(start)))
327+
end_arrays.append(window_indicies.take(ensure_platform_int(end)))
326328
start = np.concatenate(start_arrays)
327329
end = np.concatenate(end_arrays)
328330
# GH 35552: Need to adjust start and end based on the nans appended to values

pandas/tests/resample/test_resampler_grouper.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pandas.util._test_decorators import async_mark
77

88
import pandas as pd
9-
from pandas import DataFrame, Series, Timestamp, compat
9+
from pandas import DataFrame, Series, Timestamp
1010
import pandas._testing as tm
1111
from pandas.core.indexes.datetimes import date_range
1212

@@ -317,7 +317,6 @@ def test_resample_groupby_with_label():
317317
tm.assert_frame_equal(result, expected)
318318

319319

320-
@pytest.mark.xfail(not compat.IS64, reason="GH-35148")
321320
def test_consistency_with_window():
322321

323322
# consistent return values with window

pandas/tests/window/test_api.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pandas.util._test_decorators as td
77

88
import pandas as pd
9-
from pandas import DataFrame, Index, Series, Timestamp, compat, concat
9+
from pandas import DataFrame, Index, Series, Timestamp, concat
1010
import pandas._testing as tm
1111
from pandas.core.base import SpecificationError
1212

@@ -277,7 +277,7 @@ def test_preserve_metadata():
277277
@pytest.mark.parametrize(
278278
"func,window_size,expected_vals",
279279
[
280-
pytest.param(
280+
(
281281
"rolling",
282282
2,
283283
[
@@ -289,7 +289,6 @@ def test_preserve_metadata():
289289
[35.0, 40.0, 60.0, 40.0],
290290
[60.0, 80.0, 85.0, 80],
291291
],
292-
marks=pytest.mark.xfail(not compat.IS64, reason="GH-35294"),
293292
),
294293
(
295294
"expanding",

pandas/tests/window/test_apply.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pandas.errors import NumbaUtilError
55
import pandas.util._test_decorators as td
66

7-
from pandas import DataFrame, Index, MultiIndex, Series, Timestamp, compat, date_range
7+
from pandas import DataFrame, Index, MultiIndex, Series, Timestamp, date_range
88
import pandas._testing as tm
99

1010

@@ -142,7 +142,6 @@ def test_invalid_kwargs_nopython():
142142

143143

144144
@pytest.mark.parametrize("args_kwargs", [[None, {"par": 10}], [(10,), None]])
145-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
146145
def test_rolling_apply_args_kwargs(args_kwargs):
147146
# GH 33433
148147
def foo(x, par):

pandas/tests/window/test_grouper.py

+1-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
import pandas as pd
5-
from pandas import DataFrame, Series, compat
5+
from pandas import DataFrame, Series
66
import pandas._testing as tm
77
from pandas.core.groupby.groupby import get_groupby
88

@@ -23,7 +23,6 @@ def test_mutated(self):
2323
g = get_groupby(self.frame, by="A", mutated=True)
2424
assert g.mutated
2525

26-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
2726
def test_getitem(self):
2827
g = self.frame.groupby("A")
2928
g_mutated = get_groupby(self.frame, by="A", mutated=True)
@@ -56,7 +55,6 @@ def test_getitem_multiple(self):
5655
result = r.B.count()
5756
tm.assert_series_equal(result, expected)
5857

59-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
6058
def test_rolling(self):
6159
g = self.frame.groupby("A")
6260
r = g.rolling(window=4)
@@ -74,7 +72,6 @@ def test_rolling(self):
7472
@pytest.mark.parametrize(
7573
"interpolation", ["linear", "lower", "higher", "midpoint", "nearest"]
7674
)
77-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
7875
def test_rolling_quantile(self, interpolation):
7976
g = self.frame.groupby("A")
8077
r = g.rolling(window=4)
@@ -105,7 +102,6 @@ def func(x):
105102
expected = g.apply(func)
106103
tm.assert_series_equal(result, expected)
107104

108-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
109105
def test_rolling_apply(self, raw):
110106
g = self.frame.groupby("A")
111107
r = g.rolling(window=4)
@@ -115,7 +111,6 @@ def test_rolling_apply(self, raw):
115111
expected = g.apply(lambda x: x.rolling(4).apply(lambda y: y.sum(), raw=raw))
116112
tm.assert_frame_equal(result, expected)
117113

118-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
119114
def test_rolling_apply_mutability(self):
120115
# GH 14013
121116
df = pd.DataFrame({"A": ["foo"] * 3 + ["bar"] * 3, "B": [1] * 6})
@@ -197,7 +192,6 @@ def test_expanding_apply(self, raw):
197192
tm.assert_frame_equal(result, expected)
198193

199194
@pytest.mark.parametrize("expected_value,raw_value", [[1.0, True], [0.0, False]])
200-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
201195
def test_groupby_rolling(self, expected_value, raw_value):
202196
# GH 31754
203197

@@ -215,7 +209,6 @@ def foo(x):
215209
)
216210
tm.assert_series_equal(result, expected)
217211

218-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
219212
def test_groupby_rolling_center_center(self):
220213
# GH 35552
221214
series = Series(range(1, 6))
@@ -281,7 +274,6 @@ def test_groupby_rolling_center_center(self):
281274
)
282275
tm.assert_frame_equal(result, expected)
283276

284-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
285277
def test_groupby_subselect_rolling(self):
286278
# GH 35486
287279
df = DataFrame(
@@ -307,7 +299,6 @@ def test_groupby_subselect_rolling(self):
307299
)
308300
tm.assert_series_equal(result, expected)
309301

310-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
311302
def test_groupby_rolling_custom_indexer(self):
312303
# GH 35557
313304
class SimpleIndexer(pd.api.indexers.BaseIndexer):
@@ -331,7 +322,6 @@ def get_window_bounds(
331322
expected = df.groupby(df.index).rolling(window=3, min_periods=1).sum()
332323
tm.assert_frame_equal(result, expected)
333324

334-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
335325
def test_groupby_rolling_subset_with_closed(self):
336326
# GH 35549
337327
df = pd.DataFrame(
@@ -356,7 +346,6 @@ def test_groupby_rolling_subset_with_closed(self):
356346
)
357347
tm.assert_series_equal(result, expected)
358348

359-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
360349
def test_groupby_subset_rolling_subset_with_closed(self):
361350
# GH 35549
362351
df = pd.DataFrame(
@@ -384,7 +373,6 @@ def test_groupby_subset_rolling_subset_with_closed(self):
384373
)
385374
tm.assert_series_equal(result, expected)
386375

387-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
388376
@pytest.mark.parametrize("func", ["max", "min"])
389377
def test_groupby_rolling_index_changed(self, func):
390378
# GH: #36018 nlevels of MultiIndex changed

pandas/tests/window/test_rolling.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pandas.util._test_decorators as td
88

99
import pandas as pd
10-
from pandas import DataFrame, Series, compat, date_range
10+
from pandas import DataFrame, Series, date_range
1111
import pandas._testing as tm
1212
from pandas.core.window import Rolling
1313

@@ -150,7 +150,6 @@ def test_closed_one_entry(func):
150150

151151

152152
@pytest.mark.parametrize("func", ["min", "max"])
153-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
154153
def test_closed_one_entry_groupby(func):
155154
# GH24718
156155
ser = pd.DataFrame(
@@ -683,7 +682,6 @@ def test_iter_rolling_datetime(expected, expected_index, window):
683682
),
684683
],
685684
)
686-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
687685
def test_rolling_positional_argument(grouping, _index, raw):
688686
# GH 34605
689687

pandas/tests/window/test_timeseries_window.py

-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
MultiIndex,
88
Series,
99
Timestamp,
10-
compat,
1110
date_range,
1211
to_datetime,
1312
)
@@ -657,7 +656,6 @@ def agg_by_day(x):
657656

658657
tm.assert_frame_equal(result, expected)
659658

660-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
661659
def test_groupby_monotonic(self):
662660

663661
# GH 15130
@@ -687,7 +685,6 @@ def test_groupby_monotonic(self):
687685
result = df.groupby("name").rolling("180D", on="date")["amount"].sum()
688686
tm.assert_series_equal(result, expected)
689687

690-
@pytest.mark.xfail(not compat.IS64, reason="GH-35294")
691688
def test_non_monotonic(self):
692689
# GH 13966 (similar to #15130, closed by #15175)
693690

0 commit comments

Comments
 (0)