Skip to content

Commit 7a9503d

Browse files
committed
BUG: DataFrame(nested_object_ndarray) match DataFrame(nested_pandas_array) GH#43986
1 parent 0f3c5e9 commit 7a9503d

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

pandas/core/apply.py

+2-2
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
)
@@ -1144,7 +1143,8 @@ def apply_standard(self) -> DataFrame | Series:
11441143
if len(mapped) and isinstance(mapped[0], ABCSeries):
11451144
# GH 25959 use pd.array instead of tolist
11461145
# so extension arrays can be used
1147-
return obj._constructor_expanddim(pd_array(mapped), index=obj.index)
1146+
# GH#43986 Need to do list(mapped) in order to get treated as nested
1147+
return obj._constructor_expanddim(list(mapped), index=obj.index)
11481148
else:
11491149
return obj._constructor(mapped, index=obj.index).__finalize__(
11501150
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)