diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index e44fec112c5c1..90e0c31f1a1a5 100755 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -206,6 +206,7 @@ Other enhancements now preserve those data types with pyarrow >= 1.0.0 (:issue:`20612`). - The ``partition_cols`` argument in :meth:`DataFrame.to_parquet` now accepts a string (:issue:`27117`) - :func:`to_parquet` now appropriately handles the ``schema`` argument for user defined schemas in the pyarrow engine. (:issue: `30270`) +- DataFrame constructor preserve `ExtensionArray` dtype with `ExtensionArray` (:issue:`11363`) Build Changes diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b699961cf07e8..394d128164509 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -450,7 +450,7 @@ def __init__( # For data is list-like, or Iterable (will consume into list) elif isinstance(data, abc.Iterable) and not isinstance(data, (str, bytes)): - if not isinstance(data, abc.Sequence): + if not isinstance(data, (abc.Sequence, ExtensionArray)): data = list(data) if len(data) > 0: if is_list_like(data[0]) and getattr(data[0], "ndim", 1) == 1: diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index ad6e0c963e730..adec846802e66 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -25,6 +25,7 @@ date_range, isna, ) +from pandas.arrays import IntervalArray, PeriodArray from pandas.core.construction import create_series_with_explicit_dtype import pandas.util.testing as tm @@ -2396,6 +2397,21 @@ class List(list): result = DataFrame(List([List([1, 2, 3]), List([4, 5, 6])])) tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize( + "extension_arr", + [ + Categorical(list("aabbc")), + pd.SparseArray([1, np.nan, np.nan, np.nan]), + IntervalArray([pd.Interval(0, 1), pd.Interval(1, 5)]), + PeriodArray(pd.period_range(start="1/1/2017", end="1/1/2018", freq="M")), + ], + ) + def test_constructor_with_extension_array(self, extension_arr): + # GH11363 + expected = DataFrame(Series(extension_arr)) + result = DataFrame(extension_arr) + tm.assert_frame_equal(result, expected) + class TestDataFrameConstructorWithDatetimeTZ: def test_from_dict(self):