Skip to content

Commit 02dd36a

Browse files
committed
BUG: DataFrame(EA2D)
1 parent d5f7723 commit 02dd36a

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

pandas/core/internals/construction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ 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
300300
values = [values[:, n] for n in range(values.shape[1])]

pandas/tests/extension/base/dim2.py

Lines changed: 7 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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)