Skip to content

Commit 18bd98f

Browse files
proosttopper-123
authored andcommitted
ENH: Add support for DataFrame(Categorical) (pandas-dev#11363) (pandas-dev#30305)
1 parent 5c8bd04 commit 18bd98f

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
@@ -206,6 +206,7 @@ Other enhancements
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`)
208208
- :func:`to_parquet` now appropriately handles the ``schema`` argument for user defined schemas in the pyarrow engine. (:issue: `30270`)
209+
- DataFrame constructor preserve `ExtensionArray` dtype with `ExtensionArray` (:issue:`11363`)
209210

210211

211212
Build Changes

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
@@ -25,6 +25,7 @@
2525
date_range,
2626
isna,
2727
)
28+
from pandas.arrays import IntervalArray, PeriodArray
2829
from pandas.core.construction import create_series_with_explicit_dtype
2930
import pandas.util.testing as tm
3031

@@ -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)