Skip to content

Commit 8d1abe2

Browse files
committed
ENH: Add support for DataFrame(Categorical) (#11363)
1 parent 37dfcc1 commit 8d1abe2

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ Other enhancements
205205
(:meth:`~DataFrame.to_parquet` / :func:`read_parquet`) using the `'pyarrow'` engine
206206
now preserve those data types with pyarrow >= 1.0.0 (:issue:`20612`).
207207
- The ``partition_cols`` argument in :meth:`DataFrame.to_parquet` now accepts a string (:issue:`27117`)
208+
- DataFrame constructor preserve `ExtensionArray` dtype with `ExtensionArray` (:issue:`11363`)
208209

209210
Build Changes
210211
^^^^^^^^^^^^^

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ def __init__(
450450

451451
# For data is list-like, or Iterable (will consume into list)
452452
elif isinstance(data, abc.Iterable) and not isinstance(data, (str, bytes)):
453-
if not isinstance(data, abc.Sequence):
453+
if not isinstance(data, (abc.Sequence, ExtensionArray)):
454454
data = list(data)
455455
if len(data) > 0:
456456
if is_list_like(data[0]) and getattr(data[0], "ndim", 1) == 1:

pandas/tests/frame/test_constructors.py

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import numpy.ma.mrecords as mrecords
99
import pytest
1010

11+
from pandas.arrays import IntervalArray, PeriodArray
1112
from pandas.compat import is_platform_little_endian
1213

1314
from pandas.core.dtypes.common import is_integer_dtype
@@ -2396,6 +2397,21 @@ class List(list):
23962397
result = DataFrame(List([List([1, 2, 3]), List([4, 5, 6])]))
23972398
tm.assert_frame_equal(result, expected)
23982399

2400+
@pytest.mark.parametrize(
2401+
"extension_arr",
2402+
[
2403+
Categorical(list("aabbc")),
2404+
pd.SparseArray([1, np.nan, np.nan, np.nan]),
2405+
IntervalArray([pd.Interval(0, 1), pd.Interval(1, 5)]),
2406+
PeriodArray(pd.period_range(start="1/1/2017", end="1/1/2018", freq="M")),
2407+
],
2408+
)
2409+
def test_constructor_with_extension_array(self, extension_arr):
2410+
# GH11363
2411+
expected = DataFrame(Series(extension_arr))
2412+
result = DataFrame(extension_arr)
2413+
tm.assert_frame_equal(result, expected)
2414+
23992415

24002416
class TestDataFrameConstructorWithDatetimeTZ:
24012417
def test_from_dict(self):

0 commit comments

Comments
 (0)