Skip to content

Commit 66923e1

Browse files
Backport PR #38332: REGR: Fix Index construction from Sparse["datetime64[ns]"] (#38341)
Co-authored-by: Simon Hawkins <[email protected]>
1 parent 247ecef commit 66923e1

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Diff for: doc/source/whatsnew/v1.1.5.rst

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Fixed regressions
2020
- Fixed regression in inplace operations on :class:`Series` with ``ExtensionDtype`` with NumPy dtyped operand (:issue:`37910`)
2121
- Fixed regression in metadata propagation for ``groupby`` iterator (:issue:`37343`)
2222
- Fixed regression in :class:`MultiIndex` constructed from a :class:`DatetimeIndex` not retaining frequency (:issue:`35563`)
23+
- Fixed regression in :class:`Index` constructor raising a ``AttributeError`` when passed a :class:`SparseArray` with datetime64 values (:issue:`35843`)
2324
- Fixed regression in :meth:`DataFrame.unstack` with columns with integer dtype (:issue:`37115`)
2425
- Fixed regression in indexing on a :class:`Series` with ``CategoricalDtype`` after unpickling (:issue:`37631`)
2526
- Fixed regression in :meth:`DataFrame.groupby` aggregation with out-of-bounds datetime objects in an object-dtype column (:issue:`36003`)

Diff for: pandas/core/arrays/datetimes.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
is_float_dtype,
3737
is_object_dtype,
3838
is_period_dtype,
39+
is_sparse,
3940
is_string_dtype,
4041
is_timedelta64_dtype,
4142
pandas_dtype,
@@ -1937,7 +1938,11 @@ def sequence_to_dt64ns(
19371938
data, copy = maybe_convert_dtype(data, copy)
19381939
data_dtype = getattr(data, "dtype", None)
19391940

1940-
if is_object_dtype(data_dtype) or is_string_dtype(data_dtype):
1941+
if (
1942+
is_object_dtype(data_dtype)
1943+
or is_string_dtype(data_dtype)
1944+
or is_sparse(data_dtype)
1945+
):
19411946
# TODO: We do not have tests specific to string-dtypes,
19421947
# also complex or categorical or other extension
19431948
copy = False

Diff for: pandas/tests/indexes/datetimes/test_constructors.py

+11
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ def test_dti_with_timedelta64_data_raises(self):
9797
with pytest.raises(TypeError, match=msg):
9898
to_datetime(pd.TimedeltaIndex(data))
9999

100+
def test_constructor_from_sparse_array(self):
101+
# https://github.com/pandas-dev/pandas/issues/35843
102+
values = [
103+
Timestamp("2012-05-01T01:00:00.000000"),
104+
Timestamp("2016-05-01T01:00:00.000000"),
105+
]
106+
arr = pd.arrays.SparseArray(values)
107+
result = Index(arr)
108+
expected = DatetimeIndex(values)
109+
tm.assert_index_equal(result, expected)
110+
100111
def test_construction_caching(self):
101112

102113
df = pd.DataFrame(

0 commit comments

Comments
 (0)