Skip to content

Commit 1a966b1

Browse files
authored
BUG: DataFrame(nested_object_ndarray) match DataFrame(nested_pandas_array) (#45040)
1 parent 93631a9 commit 1a966b1

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ Conversion
669669
^^^^^^^^^^
670670
- Bug in :class:`UInt64Index` constructor when passing a list containing both positive integers small enough to cast to int64 and integers too large too hold in int64 (:issue:`42201`)
671671
- Bug in :class:`Series` constructor returning 0 for missing values with dtype ``int64`` and ``False`` for dtype ``bool`` (:issue:`43017`, :issue:`43018`)
672+
- Bug in constructing a :class:`DataFrame` from a :class:`PandasArray` containing :class:`Series` objects behaving differently than an equivalent ``np.ndarray`` (:issue:`43986`)
672673
- Bug in :class:`IntegerDtype` not allowing coercion from string dtype (:issue:`25472`)
673674
- Bug in :func:`to_datetime` with ``arg:xr.DataArray`` and ``unit="ns"`` specified raises TypeError (:issue:`44053`)
674675
- Bug in :meth:`DataFrame.convert_dtypes` not returning the correct type when a subclass does not overload :meth:`_constructor_sliced` (:issue:`43201`)

pandas/core/apply.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
)
5858
import pandas.core.common as com
5959
from pandas.core.construction import (
60-
array as pd_array,
6160
create_series_with_explicit_dtype,
6261
ensure_wrapped_if_datetimelike,
6362
)
@@ -1142,9 +1141,9 @@ def apply_standard(self) -> DataFrame | Series:
11421141
)
11431142

11441143
if len(mapped) and isinstance(mapped[0], ABCSeries):
1145-
# GH 25959 use pd.array instead of tolist
1146-
# so extension arrays can be used
1147-
return obj._constructor_expanddim(pd_array(mapped), index=obj.index)
1144+
# GH#43986 Need to do list(mapped) in order to get treated as nested
1145+
# See also GH#25959 regarding EA support
1146+
return obj._constructor_expanddim(list(mapped), index=obj.index)
11481147
else:
11491148
return obj._constructor(mapped, index=obj.index).__finalize__(
11501149
obj, method="apply"

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ def __init__(
666666
typ=manager,
667667
)
668668

669-
elif isinstance(data, (np.ndarray, Series, Index)):
669+
elif isinstance(data, (np.ndarray, Series, Index, ExtensionArray)):
670670
if data.dtype.names:
671671
# i.e. numpy structured array
672672
data = cast(np.ndarray, data)

pandas/tests/frame/test_constructors.py

+14
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,20 @@ def test_constructor_list_of_lists(self):
13051305
result = DataFrame(data)
13061306
tm.assert_frame_equal(result, expected)
13071307

1308+
def test_nested_pandasarray_matches_nested_ndarray(self):
1309+
# GH#43986
1310+
ser = Series([1, 2])
1311+
1312+
arr = np.array([None, None], dtype=object)
1313+
arr[0] = ser
1314+
arr[1] = ser * 2
1315+
1316+
df = DataFrame(arr)
1317+
expected = DataFrame(pd.array(arr))
1318+
tm.assert_frame_equal(df, expected)
1319+
assert df.shape == (2, 1)
1320+
tm.assert_numpy_array_equal(df[0].values, arr)
1321+
13081322
def test_constructor_list_like_data_nested_list_column(self):
13091323
# GH 32173
13101324
arrays = [list("abcd"), list("cdef")]

0 commit comments

Comments
 (0)