Skip to content

Commit fa56390

Browse files
authored
BUG: DataFrame(EA2D) construction (#44593)
1 parent fb86932 commit fa56390

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

pandas/core/internals/construction.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,15 @@ def ndarray_to_mgr(
294294
if is_1d_only_ea_dtype(vdtype) or isinstance(dtype, ExtensionDtype):
295295
# GH#19157
296296

297-
if isinstance(values, np.ndarray) and values.ndim > 1:
297+
if isinstance(values, (np.ndarray, ExtensionArray)) and values.ndim > 1:
298298
# GH#12513 a EA dtype passed with a 2D array, split into
299299
# multiple EAs that view the values
300-
values = [values[:, n] for n in range(values.shape[1])]
300+
# error: No overload variant of "__getitem__" of "ExtensionArray"
301+
# matches argument type "Tuple[slice, int]"
302+
values = [
303+
values[:, n] # type: ignore[call-overload]
304+
for n in range(values.shape[1])
305+
]
301306
else:
302307
values = [values]
303308

pandas/tests/extension/base/dim2.py

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515

1616
class Dim2CompatTests(BaseExtensionTests):
17+
def test_frame_from_2d_array(self, data):
18+
arr2d = data.repeat(2).reshape(-1, 2)
19+
20+
df = pd.DataFrame(arr2d)
21+
expected = pd.DataFrame({0: arr2d[:, 0], 1: arr2d[:, 1]})
22+
self.assert_frame_equal(df, expected)
23+
1724
def test_swapaxes(self, data):
1825
arr2d = data.repeat(2).reshape(-1, 2)
1926

pandas/tests/frame/test_constructors.py

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@
7070

7171

7272
class TestDataFrameConstructors:
73+
def test_constructor_from_2d_datetimearray(self):
74+
dti = date_range("2016-01-01", periods=6, tz="US/Pacific")
75+
dta = dti._data.reshape(3, 2)
76+
77+
df = DataFrame(dta)
78+
expected = DataFrame({0: dta[:, 0], 1: dta[:, 1]})
79+
tm.assert_frame_equal(df, expected)
80+
7381
def test_constructor_dict_with_tzaware_scalar(self):
7482
# GH#42505
7583
dt = Timestamp("2019-11-03 01:00:00-0700").tz_convert("America/Los_Angeles")

0 commit comments

Comments
 (0)