Skip to content

REGR: Fix Index construction from Sparse["datetime64[ns]"] #38332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
is_float_dtype,
is_object_dtype,
is_period_dtype,
is_sparse,
is_string_dtype,
is_timedelta64_dtype,
pandas_dtype,
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down