Skip to content

BUG: ensure consistent behavior of Index.is_all_dates (#19204) #33104

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

Closed
wants to merge 3 commits into from
Closed
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.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ Indexing
- 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`)
- Bug in :meth:`DataFrame.truncate` and :meth:`Series.truncate` where index was assumed to be monotone increasing (:issue:`33756`)
- Indexing with a list of strings representing datetimes failed on :class:`DatetimeIndex` or :class:`PeriodIndex`(:issue:`11278`)
- Bug in :meth:`Index.is_all_dates` incorrectly returning ``False`` when inferred type of index was other dates than datetime (:issue:`19204`)

Missing
^^^^^^^
Expand Down
12 changes: 9 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pandas._libs import algos as libalgos, index as libindex, lib
import pandas._libs.join as libjoin
from pandas._libs.lib import is_datetime_array, no_default
from pandas._libs.lib import no_default
from pandas._libs.tslibs import OutOfBoundsDatetime, Timestamp
from pandas._libs.tslibs.period import IncompatibleFrequency
from pandas._libs.tslibs.timezones import tz_compare
Expand All @@ -25,7 +25,6 @@
)
from pandas.core.dtypes.common import (
ensure_int64,
ensure_object,
ensure_platform_int,
is_bool,
is_bool_dtype,
Expand Down Expand Up @@ -1951,7 +1950,14 @@ def is_all_dates(self) -> bool:
"""
Whether or not the index values only consist of dates.
"""
return is_datetime_array(ensure_object(self._values))
return self.inferred_type in [
"datetime64",
"datetime",
"date",
"timedelta64",
"timedelta",
"period",
]

# --------------------------------------------------------------------
# Pickle Methods
Expand Down
14 changes: 13 additions & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,14 +1152,26 @@ def test_is_object(self, indices, expected):
("bool", False),
("categorical", False),
("int", False),
("datetime", True),
("float", False),
("datetime", True),
("datetime-tz", True),
("period", True),
("timedelta", True),
("empty", False),
],
indirect=["indices"],
)
def test_is_all_dates(self, indices, expected):
assert indices.is_all_dates is expected

@pytest.mark.parametrize(
"index", ["datetime", "datetime-tz", "period", "timedelta"], indirect=["index"],
)
def test_is_all_dates_consistency(self, index):
# GH 19204
non_date = pd.Index(["not a date"])
assert index.is_all_dates == index.append(non_date)[:-1].is_all_dates

def test_summary(self, indices):
self._check_method_works(Index._summary, indices)

Expand Down