Skip to content

Commit 61d982c

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

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
@@ -479,6 +479,7 @@ Indexing
479479
- Bug in :meth:`DataFrame.copy` _item_cache not invalidated after copy causes post-copy value updates to not be reflected (:issue:`31784`)
480480
- Bug in `Series.__getitem__` with an integer key and a :class:`MultiIndex` with leading integer level failing to raise ``KeyError`` if the key is not present in the first level (:issue:`33355`)
481481
- Bug in :meth:`DataFrame.iloc` when slicing a single column-:class:`DataFrame`` with ``ExtensionDtype`` (e.g. ``df.iloc[:, :1]``) returning an invalid result (:issue:`32957`)
482+
- Bug in :meth:`Index.is_all_dates` incorrectly returning ``False`` when inferred type of index was other dates than datetime (:issue:`19204`)
482483

483484
Missing
484485
^^^^^^^

pandas/core/indexes/base.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,14 @@ def is_all_dates(self) -> bool:
19891989
"""
19901990
Whether or not the index values only consist of dates.
19911991
"""
1992-
return is_datetime_array(ensure_object(self.values))
1992+
return self.inferred_type in [
1993+
"datetime64",
1994+
"datetime",
1995+
"date",
1996+
"timedelta64",
1997+
"timedelta",
1998+
"period",
1999+
]
19932000

19942001
# --------------------------------------------------------------------
19952002
# Pickle Methods

pandas/tests/indexes/test_base.py

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

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

0 commit comments

Comments
 (0)