Skip to content

Commit 30beab2

Browse files
mroeschkemeeseeksmachine
authored andcommitted
Backport PR pandas-dev#50682: BUG: pivot_table with nested elements and numpy 1.24
1 parent ce123cd commit 30beab2

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

doc/source/whatsnew/v1.5.3.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ Bug fixes
3232
- Bug in :meth:`Series.quantile` emitting warning from NumPy when :class:`Series` has only ``NA`` values (:issue:`50681`)
3333
- Bug when chaining several :meth:`.Styler.concat` calls, only the last styler was concatenated (:issue:`49207`)
3434
- Fixed bug when instantiating a :class:`DataFrame` subclass inheriting from ``typing.Generic`` that triggered a ``UserWarning`` on python 3.11 (:issue:`49649`)
35+
- Bug in :func:`pivot_table` with NumPy 1.24 or greater when the :class:`DataFrame` columns has nested elements (:issue:`50342`)
3536
- Bug in :func:`pandas.testing.assert_series_equal` (and equivalent ``assert_`` functions) when having nested data and using numpy >= 1.25 (:issue:`50360`)
36-
-
3737

3838
.. ---------------------------------------------------------------------------
3939
.. _whatsnew_153.other:

pandas/core/common.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,17 @@ def asarray_tuplesafe(values: Iterable, dtype: NpDtype | None = None) -> ArrayLi
242242
if isinstance(values, list) and dtype in [np.object_, object]:
243243
return construct_1d_object_array_from_listlike(values)
244244

245-
result = np.asarray(values, dtype=dtype)
245+
try:
246+
with warnings.catch_warnings():
247+
# Can remove warning filter once NumPy 1.24 is min version
248+
warnings.simplefilter("ignore", np.VisibleDeprecationWarning)
249+
result = np.asarray(values, dtype=dtype)
250+
except ValueError:
251+
# Using try/except since it's more performant than checking is_list_like
252+
# over each element
253+
# error: Argument 1 to "construct_1d_object_array_from_listlike"
254+
# has incompatible type "Iterable[Any]"; expected "Sized"
255+
return construct_1d_object_array_from_listlike(values) # type: ignore[arg-type]
246256

247257
if issubclass(result.dtype.type, str):
248258
result = np.asarray(values, dtype=object)

pandas/tests/reshape/test_pivot.py

+69
Original file line numberDiff line numberDiff line change
@@ -2276,6 +2276,75 @@ def test_pivot_table_datetime_warning(self):
22762276
)
22772277
tm.assert_frame_equal(result, expected)
22782278

2279+
def test_pivot_table_with_mixed_nested_tuples(self, using_array_manager):
2280+
# GH 50342
2281+
df = DataFrame(
2282+
{
2283+
"A": ["foo", "foo", "foo", "foo", "foo", "bar", "bar", "bar", "bar"],
2284+
"B": ["one", "one", "one", "two", "two", "one", "one", "two", "two"],
2285+
"C": [
2286+
"small",
2287+
"large",
2288+
"large",
2289+
"small",
2290+
"small",
2291+
"large",
2292+
"small",
2293+
"small",
2294+
"large",
2295+
],
2296+
"D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
2297+
"E": [2, 4, 5, 5, 6, 6, 8, 9, 9],
2298+
("col5",): [
2299+
"foo",
2300+
"foo",
2301+
"foo",
2302+
"foo",
2303+
"foo",
2304+
"bar",
2305+
"bar",
2306+
"bar",
2307+
"bar",
2308+
],
2309+
("col6", 6): [
2310+
"one",
2311+
"one",
2312+
"one",
2313+
"two",
2314+
"two",
2315+
"one",
2316+
"one",
2317+
"two",
2318+
"two",
2319+
],
2320+
(7, "seven"): [
2321+
"small",
2322+
"large",
2323+
"large",
2324+
"small",
2325+
"small",
2326+
"large",
2327+
"small",
2328+
"small",
2329+
"large",
2330+
],
2331+
}
2332+
)
2333+
result = pivot_table(
2334+
df, values="D", index=["A", "B"], columns=[(7, "seven")], aggfunc=np.sum
2335+
)
2336+
expected = DataFrame(
2337+
[[4.0, 5.0], [7.0, 6.0], [4.0, 1.0], [np.nan, 6.0]],
2338+
columns=Index(["large", "small"], name=(7, "seven")),
2339+
index=MultiIndex.from_arrays(
2340+
[["bar", "bar", "foo", "foo"], ["one", "two"] * 2], names=["A", "B"]
2341+
),
2342+
)
2343+
if using_array_manager:
2344+
# INFO(ArrayManager) column without NaNs can preserve int dtype
2345+
expected["small"] = expected["small"].astype("int64")
2346+
tm.assert_frame_equal(result, expected)
2347+
22792348

22802349
class TestPivot:
22812350
def test_pivot(self):

0 commit comments

Comments
 (0)