Skip to content

Commit affcdf9

Browse files
BUG: pivot_table with nested elements and numpy 1.24 (#50682)
* Fix asarray_tuplesafe for numpy 1.24.1 deprecation * BUG: pivot_table with nested elements and numpy 1.24 * For all numpy versions * Undo unneeded variable * fix for arraymanager * use try except * typing * Update pandas/core/common.py Co-authored-by: Marc Garcia <[email protected]> * line length Co-authored-by: Marc Garcia <[email protected]>
1 parent 86cb950 commit affcdf9

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-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

+12-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
cast,
2626
overload,
2727
)
28+
import warnings
2829

2930
import numpy as np
3031

@@ -235,7 +236,17 @@ def asarray_tuplesafe(values: Iterable, dtype: NpDtype | None = None) -> ArrayLi
235236
if isinstance(values, list) and dtype in [np.object_, object]:
236237
return construct_1d_object_array_from_listlike(values)
237238

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

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

pandas/tests/reshape/test_pivot.py

+69
Original file line numberDiff line numberDiff line change
@@ -2312,6 +2312,75 @@ def test_pivot_table_datetime_warning(self):
23122312
)
23132313
tm.assert_frame_equal(result, expected)
23142314

2315+
def test_pivot_table_with_mixed_nested_tuples(self, using_array_manager):
2316+
# GH 50342
2317+
df = DataFrame(
2318+
{
2319+
"A": ["foo", "foo", "foo", "foo", "foo", "bar", "bar", "bar", "bar"],
2320+
"B": ["one", "one", "one", "two", "two", "one", "one", "two", "two"],
2321+
"C": [
2322+
"small",
2323+
"large",
2324+
"large",
2325+
"small",
2326+
"small",
2327+
"large",
2328+
"small",
2329+
"small",
2330+
"large",
2331+
],
2332+
"D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
2333+
"E": [2, 4, 5, 5, 6, 6, 8, 9, 9],
2334+
("col5",): [
2335+
"foo",
2336+
"foo",
2337+
"foo",
2338+
"foo",
2339+
"foo",
2340+
"bar",
2341+
"bar",
2342+
"bar",
2343+
"bar",
2344+
],
2345+
("col6", 6): [
2346+
"one",
2347+
"one",
2348+
"one",
2349+
"two",
2350+
"two",
2351+
"one",
2352+
"one",
2353+
"two",
2354+
"two",
2355+
],
2356+
(7, "seven"): [
2357+
"small",
2358+
"large",
2359+
"large",
2360+
"small",
2361+
"small",
2362+
"large",
2363+
"small",
2364+
"small",
2365+
"large",
2366+
],
2367+
}
2368+
)
2369+
result = pivot_table(
2370+
df, values="D", index=["A", "B"], columns=[(7, "seven")], aggfunc=np.sum
2371+
)
2372+
expected = DataFrame(
2373+
[[4.0, 5.0], [7.0, 6.0], [4.0, 1.0], [np.nan, 6.0]],
2374+
columns=Index(["large", "small"], name=(7, "seven")),
2375+
index=MultiIndex.from_arrays(
2376+
[["bar", "bar", "foo", "foo"], ["one", "two"] * 2], names=["A", "B"]
2377+
),
2378+
)
2379+
if using_array_manager:
2380+
# INFO(ArrayManager) column without NaNs can preserve int dtype
2381+
expected["small"] = expected["small"].astype("int64")
2382+
tm.assert_frame_equal(result, expected)
2383+
23152384

23162385
class TestPivot:
23172386
def test_pivot(self):

0 commit comments

Comments
 (0)