Skip to content

Commit 604ac48

Browse files
REGR: Fix Index construction from Sparse["datetime64[ns]"] (#38332)
1 parent 55e3bff commit 604ac48

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

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

pandas/core/arrays/datetimes.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
is_float_dtype,
3939
is_object_dtype,
4040
is_period_dtype,
41+
is_sparse,
4142
is_string_dtype,
4243
is_timedelta64_dtype,
4344
pandas_dtype,
@@ -1956,7 +1957,11 @@ def sequence_to_dt64ns(
19561957
data, copy = maybe_convert_dtype(data, copy)
19571958
data_dtype = getattr(data, "dtype", None)
19581959

1959-
if is_object_dtype(data_dtype) or is_string_dtype(data_dtype):
1960+
if (
1961+
is_object_dtype(data_dtype)
1962+
or is_string_dtype(data_dtype)
1963+
or is_sparse(data_dtype)
1964+
):
19601965
# TODO: We do not have tests specific to string-dtypes,
19611966
# also complex or categorical or other extension
19621967
copy = False

pandas/tests/indexes/datetimes/test_constructors.py

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

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

104115
df = pd.DataFrame(

0 commit comments

Comments
 (0)