diff --git a/doc/source/whatsnew/v1.1.5.rst b/doc/source/whatsnew/v1.1.5.rst index 7164830392f35..cfdeeb61d8f61 100644 --- a/doc/source/whatsnew/v1.1.5.rst +++ b/doc/source/whatsnew/v1.1.5.rst @@ -20,6 +20,7 @@ Fixed regressions - Fixed regression in inplace operations on :class:`Series` with ``ExtensionDtype`` with NumPy dtyped operand (:issue:`37910`) - Fixed regression in metadata propagation for ``groupby`` iterator (:issue:`37343`) - Fixed regression in :class:`MultiIndex` constructed from a :class:`DatetimeIndex` not retaining frequency (:issue:`35563`) +- Fixed regression in :class:`Index` constructor raising a ``AttributeError`` when passed a :class:`SparseArray` with datetime64 values (:issue:`35843`) - Fixed regression in :meth:`DataFrame.unstack` with columns with integer dtype (:issue:`37115`) - Fixed regression in indexing on a :class:`Series` with ``CategoricalDtype`` after unpickling (:issue:`37631`) - Fixed regression in :meth:`DataFrame.groupby` aggregation with out-of-bounds datetime objects in an object-dtype column (:issue:`36003`) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index ce70f929cc79d..1f6b933d643d6 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -38,6 +38,7 @@ is_float_dtype, is_object_dtype, is_period_dtype, + is_sparse, is_string_dtype, is_timedelta64_dtype, pandas_dtype, @@ -1956,7 +1957,11 @@ def sequence_to_dt64ns( data, copy = maybe_convert_dtype(data, copy) data_dtype = getattr(data, "dtype", None) - if is_object_dtype(data_dtype) or is_string_dtype(data_dtype): + if ( + is_object_dtype(data_dtype) + or is_string_dtype(data_dtype) + or is_sparse(data_dtype) + ): # TODO: We do not have tests specific to string-dtypes, # also complex or categorical or other extension copy = False diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index 3bb25adb0f36b..698da83d9e4ad 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -99,6 +99,17 @@ def test_dti_with_timedelta64_data_raises(self): with pytest.raises(TypeError, match=msg): to_datetime(pd.TimedeltaIndex(data)) + def test_constructor_from_sparse_array(self): + # https://github.com/pandas-dev/pandas/issues/35843 + values = [ + Timestamp("2012-05-01T01:00:00.000000"), + Timestamp("2016-05-01T01:00:00.000000"), + ] + arr = pd.arrays.SparseArray(values) + result = Index(arr) + expected = DatetimeIndex(values) + tm.assert_index_equal(result, expected) + def test_construction_caching(self): df = pd.DataFrame(