Skip to content

Commit

Permalink
REGR: Fix Index construction from Sparse["datetime64[ns]"] (#38332)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjayhawkins committed Dec 7, 2020
1 parent 55e3bff commit 604ac48
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.5.rst
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
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
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

0 comments on commit 604ac48

Please sign in to comment.