diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 7218c77e43409..7b60e29f6c57f 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -669,6 +669,7 @@ Conversion ^^^^^^^^^^ - 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`) - Bug in :class:`Series` constructor returning 0 for missing values with dtype ``int64`` and ``False`` for dtype ``bool`` (:issue:`43017`, :issue:`43018`) +- Bug in constructing a :class:`DataFrame` from a :class:`PandasArray` containing :class:`Series` objects behaving differently than an equivalent ``np.ndarray`` (:issue:`43986`) - Bug in :class:`IntegerDtype` not allowing coercion from string dtype (:issue:`25472`) - Bug in :func:`to_datetime` with ``arg:xr.DataArray`` and ``unit="ns"`` specified raises TypeError (:issue:`44053`) - Bug in :meth:`DataFrame.convert_dtypes` not returning the correct type when a subclass does not overload :meth:`_constructor_sliced` (:issue:`43201`) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 291ad2b071665..64ee843f1d946 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -57,7 +57,6 @@ ) import pandas.core.common as com from pandas.core.construction import ( - array as pd_array, create_series_with_explicit_dtype, ensure_wrapped_if_datetimelike, ) @@ -1142,9 +1141,9 @@ def apply_standard(self) -> DataFrame | Series: ) if len(mapped) and isinstance(mapped[0], ABCSeries): - # GH 25959 use pd.array instead of tolist - # so extension arrays can be used - return obj._constructor_expanddim(pd_array(mapped), index=obj.index) + # GH#43986 Need to do list(mapped) in order to get treated as nested + # See also GH#25959 regarding EA support + return obj._constructor_expanddim(list(mapped), index=obj.index) else: return obj._constructor(mapped, index=obj.index).__finalize__( obj, method="apply" diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 794fb2afc7f9e..ae1e083e6604f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -666,7 +666,7 @@ def __init__( typ=manager, ) - elif isinstance(data, (np.ndarray, Series, Index)): + elif isinstance(data, (np.ndarray, Series, Index, ExtensionArray)): if data.dtype.names: # i.e. numpy structured array data = cast(np.ndarray, data) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index e7c2e17bc9ac1..b7d553707a91b 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1305,6 +1305,20 @@ def test_constructor_list_of_lists(self): result = DataFrame(data) tm.assert_frame_equal(result, expected) + def test_nested_pandasarray_matches_nested_ndarray(self): + # GH#43986 + ser = Series([1, 2]) + + arr = np.array([None, None], dtype=object) + arr[0] = ser + arr[1] = ser * 2 + + df = DataFrame(arr) + expected = DataFrame(pd.array(arr)) + tm.assert_frame_equal(df, expected) + assert df.shape == (2, 1) + tm.assert_numpy_array_equal(df[0].values, arr) + def test_constructor_list_like_data_nested_list_column(self): # GH 32173 arrays = [list("abcd"), list("cdef")]