Skip to content

Commit f549cb4

Browse files
authored
TST: Mock pivot and pivot_table test for PerformanceWarning (#45112)
1 parent a6eb92b commit f549cb4

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

pandas/tests/frame/test_stack_unstack.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
date_range,
1919
)
2020
import pandas._testing as tm
21+
from pandas.core.reshape import reshape as reshape_lib
2122

2223

2324
class TestDataFrameReshape:
@@ -1826,19 +1827,26 @@ def test_unstack_unobserved_keys(self):
18261827
tm.assert_frame_equal(recons, df)
18271828

18281829
@pytest.mark.slow
1829-
def test_unstack_number_of_levels_larger_than_int32(self):
1830+
def test_unstack_number_of_levels_larger_than_int32(self, monkeypatch):
18301831
# GH#20601
18311832
# GH 26314: Change ValueError to PerformanceWarning
1832-
df = DataFrame(
1833-
np.random.randn(2 ** 16, 2), index=[np.arange(2 ** 16), np.arange(2 ** 16)]
1834-
)
1835-
msg = "The following operation may generate"
1836-
with tm.assert_produces_warning(PerformanceWarning, match=msg):
1837-
try:
1838-
df.unstack()
1839-
except MemoryError:
1840-
# Just checking the warning
1841-
return
1833+
1834+
class MockUnstacker(reshape_lib._Unstacker):
1835+
def __init__(self, *args, **kwargs):
1836+
# __init__ will raise the warning
1837+
super().__init__(*args, **kwargs)
1838+
raise Exception("Don't compute final result.")
1839+
1840+
with monkeypatch.context() as m:
1841+
m.setattr(reshape_lib, "_Unstacker", MockUnstacker)
1842+
df = DataFrame(
1843+
np.random.randn(2 ** 16, 2),
1844+
index=[np.arange(2 ** 16), np.arange(2 ** 16)],
1845+
)
1846+
msg = "The following operation may generate"
1847+
with tm.assert_produces_warning(PerformanceWarning, match=msg):
1848+
with pytest.raises(Exception, match="Don't compute final result."):
1849+
df.unstack()
18421850

18431851
def test_stack_order_with_unsorted_levels(self):
18441852
# GH#16323

pandas/tests/reshape/test_pivot.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
)
2424
import pandas._testing as tm
2525
from pandas.api.types import CategoricalDtype as CDT
26+
from pandas.core.reshape import reshape as reshape_lib
2627
from pandas.core.reshape.pivot import pivot_table
2728

2829

@@ -1991,22 +1992,27 @@ def test_pivot_string_func_vs_func(self, f, f_numpy):
19911992
tm.assert_frame_equal(result, expected)
19921993

19931994
@pytest.mark.slow
1994-
def test_pivot_number_of_levels_larger_than_int32(self):
1995+
def test_pivot_number_of_levels_larger_than_int32(self, monkeypatch):
19951996
# GH 20601
19961997
# GH 26314: Change ValueError to PerformanceWarning
1997-
df = DataFrame(
1998-
{"ind1": np.arange(2 ** 16), "ind2": np.arange(2 ** 16), "count": 0}
1999-
)
1998+
class MockUnstacker(reshape_lib._Unstacker):
1999+
def __init__(self, *args, **kwargs):
2000+
# __init__ will raise the warning
2001+
super().__init__(*args, **kwargs)
2002+
raise Exception("Don't compute final result.")
2003+
2004+
with monkeypatch.context() as m:
2005+
m.setattr(reshape_lib, "_Unstacker", MockUnstacker)
2006+
df = DataFrame(
2007+
{"ind1": np.arange(2 ** 16), "ind2": np.arange(2 ** 16), "count": 0}
2008+
)
20002009

2001-
msg = "The following operation may generate"
2002-
with tm.assert_produces_warning(PerformanceWarning, match=msg):
2003-
try:
2004-
df.pivot_table(
2005-
index="ind1", columns="ind2", values="count", aggfunc="count"
2006-
)
2007-
except MemoryError:
2008-
# Just checking the warning
2009-
return
2010+
msg = "The following operation may generate"
2011+
with tm.assert_produces_warning(PerformanceWarning, match=msg):
2012+
with pytest.raises(Exception, match="Don't compute final result."):
2013+
df.pivot_table(
2014+
index="ind1", columns="ind2", values="count", aggfunc="count"
2015+
)
20102016

20112017
def test_pivot_table_aggfunc_dropna(self, dropna):
20122018
# GH 22159

0 commit comments

Comments
 (0)