Skip to content

Commit ffa6e20

Browse files
authored
CI: typo fixup for ResourceWarning (pandas-dev#40088)
1 parent bfd8922 commit ffa6e20

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

pandas/core/dtypes/cast.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
PeriodDtype,
9595
)
9696
from pandas.core.dtypes.generic import (
97+
ABCDataFrame,
9798
ABCExtensionArray,
9899
ABCSeries,
99100
)
@@ -238,6 +239,9 @@ def maybe_downcast_to_dtype(
238239
try to cast to the specified dtype (e.g. convert back to bool/int
239240
or could be an astype of float64->float32
240241
"""
242+
if isinstance(result, ABCDataFrame):
243+
# see test_pivot_table_doctest_case
244+
return result
241245
do_round = False
242246

243247
if isinstance(dtype, str):
@@ -307,7 +311,7 @@ def maybe_downcast_numeric(
307311
-------
308312
ndarray or ExtensionArray
309313
"""
310-
if not isinstance(dtype, np.dtype):
314+
if not isinstance(dtype, np.dtype) or not isinstance(result.dtype, np.dtype):
311315
# e.g. SparseDtype has no itemsize attr
312316
return result
313317

pandas/tests/io/parser/common/test_chunksize.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def test_warn_if_chunks_have_mismatched_type(all_parsers, request):
193193
# 2021-02-21 this occasionally fails on the CI with an unexpected
194194
# ResourceWarning that we have been unable to track down,
195195
# see GH#38630
196-
if "ResourceError" not in str(err) or parser.engine != "python":
196+
if "ResourceWarning" not in str(err) or parser.engine != "python":
197197
raise
198198

199199
# Check the main assertion of the test before re-raising

pandas/tests/reshape/test_pivot.py

+49
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,55 @@ def agg(arr):
20632063
with pytest.raises(KeyError, match="notpresent"):
20642064
foo.pivot_table("notpresent", "X", "Y", aggfunc=agg)
20652065

2066+
def test_pivot_table_doctest_case(self):
2067+
# TODO: better name. the relevant characteristic is that
2068+
# the call to maybe_downcast_to_dtype(agged[v], data[v].dtype) in
2069+
# __internal_pivot_table has `agged[v]` a DataFrame instead of Series,
2070+
# i.e agged.columns is not unique
2071+
df = DataFrame(
2072+
{
2073+
"A": ["foo", "foo", "foo", "foo", "foo", "bar", "bar", "bar", "bar"],
2074+
"B": ["one", "one", "one", "two", "two", "one", "one", "two", "two"],
2075+
"C": [
2076+
"small",
2077+
"large",
2078+
"large",
2079+
"small",
2080+
"small",
2081+
"large",
2082+
"small",
2083+
"small",
2084+
"large",
2085+
],
2086+
"D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
2087+
"E": [2, 4, 5, 5, 6, 6, 8, 9, 9],
2088+
}
2089+
)
2090+
2091+
table = pivot_table(
2092+
df,
2093+
values=["D", "E"],
2094+
index=["A", "C"],
2095+
aggfunc={"D": np.mean, "E": [min, max, np.mean]},
2096+
)
2097+
cols = MultiIndex.from_tuples(
2098+
[("D", "mean"), ("E", "max"), ("E", "mean"), ("E", "min")]
2099+
)
2100+
index = MultiIndex.from_tuples(
2101+
[("bar", "large"), ("bar", "small"), ("foo", "large"), ("foo", "small")],
2102+
names=["A", "C"],
2103+
)
2104+
vals = np.array(
2105+
[
2106+
[5.5, 9.0, 7.5, 6.0],
2107+
[5.5, 9.0, 8.5, 8.0],
2108+
[2.0, 5.0, 4.5, 4.0],
2109+
[2.33333333, 6.0, 4.33333333, 2.0],
2110+
]
2111+
)
2112+
expected = DataFrame(vals, columns=cols, index=index)
2113+
tm.assert_frame_equal(table, expected)
2114+
20662115

20672116
class TestPivot:
20682117
def test_pivot(self):

0 commit comments

Comments
 (0)