Skip to content

Commit 35ec05a

Browse files
author
adam
committed
BUG: ensure consistent behavior of Index.is_all_dates (pandas-dev#19204)
1 parent 60b0e9f commit 35ec05a

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ Indexing
341341
- Fix to preserve the ability to index with the "nearest" method with xarray's CFTimeIndex, an :class:`Index` subclass (`pydata/xarray#3751 <https://github.com/pydata/xarray/issues/3751>`_, :issue:`32905`).
342342
- Bug in :class:`Index` constructor where an unhelpful error message was raised for ``numpy`` scalars (:issue:`33017`)
343343
- Bug in :meth:`DataFrame.lookup` incorrectly raising an ``AttributeError`` when ``frame.index`` or ``frame.columns`` is not unique; this will now raise a ``ValueError`` with a helpful error message (:issue:`33041`)
344+
- Bug in :meth:`Index.is_all_dates` incorrectly returning ``False`` when inferred type of index was other dates than datetime (:issue:`19204`)
344345

345346
Missing
346347
^^^^^^^

pandas/core/indexes/base.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1975,7 +1975,14 @@ def is_all_dates(self) -> bool:
19751975
"""
19761976
Whether or not the index values only consist of dates.
19771977
"""
1978-
return is_datetime_array(ensure_object(self.values))
1978+
return self.inferred_type in [
1979+
"datetime64",
1980+
"datetime",
1981+
"date",
1982+
"timedelta64",
1983+
"timedelta",
1984+
"period",
1985+
]
19791986

19801987
# --------------------------------------------------------------------
19811988
# Pickle Methods

pandas/tests/indexes/test_base.py

+16
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,22 @@ def test_is_object(self, index, expected):
12061206
def test_is_all_dates(self, index, expected):
12071207
assert index.is_all_dates is expected
12081208

1209+
@pytest.mark.parametrize(
1210+
"index",
1211+
[
1212+
"datetime",
1213+
"datetime-tz",
1214+
"period",
1215+
"timedelta",
1216+
"empty"
1217+
],
1218+
indirect=["index"],
1219+
)
1220+
def test_is_all_dates_consistency(self, index):
1221+
# GH 19204
1222+
non_date = pd.Index(['not a date'])
1223+
assert index.is_all_dates == index.append(non_date)[:-1].is_all_dates
1224+
12091225
def test_summary(self, indices):
12101226
self._check_method_works(Index._summary, indices)
12111227

0 commit comments

Comments
 (0)