Skip to content

Commit 3e62a86

Browse files
adamaddammy
adam
authored andcommitted
BUG: ensure consistent behavior of Index.is_all_dates (pandas-dev#19204)
1 parent a3477c7 commit 3e62a86

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ Indexing
636636
- Bug in :meth:`Series.__getitem__` allowing missing labels with ``np.ndarray``, :class:`Index`, :class:`Series` indexers but not ``list``, these now all raise ``KeyError`` (:issue:`33646`)
637637
- Bug in :meth:`DataFrame.truncate` and :meth:`Series.truncate` where index was assumed to be monotone increasing (:issue:`33756`)
638638
- Indexing with a list of strings representing datetimes failed on :class:`DatetimeIndex` or :class:`PeriodIndex`(:issue:`11278`)
639+
- Bug in :meth:`Index.is_all_dates` incorrectly returning ``False`` when inferred type of index was other dates than datetime (:issue:`19204`)
639640

640641
Missing
641642
^^^^^^^

pandas/core/indexes/base.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1951,7 +1951,14 @@ def is_all_dates(self) -> bool:
19511951
"""
19521952
Whether or not the index values only consist of dates.
19531953
"""
1954-
return is_datetime_array(ensure_object(self._values))
1954+
return self.inferred_type in [
1955+
"datetime64",
1956+
"datetime",
1957+
"date",
1958+
"timedelta64",
1959+
"timedelta",
1960+
"period",
1961+
]
19551962

19561963
# --------------------------------------------------------------------
19571964
# Pickle Methods

pandas/tests/indexes/test_base.py

+10
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,16 @@ def test_is_object(self, indices, expected):
11601160
def test_is_all_dates(self, indices, expected):
11611161
assert indices.is_all_dates is expected
11621162

1163+
@pytest.mark.parametrize(
1164+
"index",
1165+
["datetime", "datetime-tz", "period", "timedelta", "empty"],
1166+
indirect=["index"],
1167+
)
1168+
def test_is_all_dates_consistency(self, index):
1169+
# GH 19204
1170+
non_date = pd.Index(["not a date"])
1171+
assert index.is_all_dates == index.append(non_date)[:-1].is_all_dates
1172+
11631173
def test_summary(self, indices):
11641174
self._check_method_works(Index._summary, indices)
11651175

0 commit comments

Comments
 (0)