Skip to content

Commit 90a6135

Browse files
authored
DEPR: is_all_dates (#36697)
1 parent 78bca00 commit 90a6135

File tree

20 files changed

+87
-41
lines changed

20 files changed

+87
-41
lines changed

doc/source/whatsnew/v1.2.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ Deprecations
214214
- :meth:`DataFrame.lookup` is deprecated and will be removed in a future version, use :meth:`DataFrame.melt` and :meth:`DataFrame.loc` instead (:issue:`18682`)
215215
- The :meth:`Index.to_native_types` is deprecated. Use ``.astype(str)`` instead (:issue:`28867`)
216216
- Deprecated indexing :class:`DataFrame` rows with datetime-like strings ``df[string]``, use ``df.loc[string]`` instead (:issue:`36179`)
217+
- Deprecated casting an object-dtype index of ``datetime`` objects to :class:`DatetimeIndex` in the :class:`Series` constructor (:issue:`23598`)
218+
- Deprecated :meth:`Index.is_all_dates` (:issue:`27744`)
217219

218220
.. ---------------------------------------------------------------------------
219221

pandas/core/generic.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9528,7 +9528,13 @@ def truncate(
95289528

95299529
# if we have a date index, convert to dates, otherwise
95309530
# treat like a slice
9531-
if ax.is_all_dates:
9531+
if ax._is_all_dates:
9532+
if is_object_dtype(ax.dtype):
9533+
warnings.warn(
9534+
"Treating object-dtype Index of date objects as DatetimeIndex "
9535+
"is deprecated, will be removed in a future version.",
9536+
FutureWarning,
9537+
)
95329538
from pandas.core.tools.datetimes import to_datetime
95339539

95349540
before = to_datetime(before)

pandas/core/indexes/base.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2086,12 +2086,25 @@ def inferred_type(self) -> str_t:
20862086
return lib.infer_dtype(self._values, skipna=False)
20872087

20882088
@cache_readonly
2089-
def is_all_dates(self) -> bool:
2089+
def _is_all_dates(self) -> bool:
20902090
"""
20912091
Whether or not the index values only consist of dates.
20922092
"""
20932093
return is_datetime_array(ensure_object(self._values))
20942094

2095+
@cache_readonly
2096+
def is_all_dates(self):
2097+
"""
2098+
Whether or not the index values only consist of dates.
2099+
"""
2100+
warnings.warn(
2101+
"Index.is_all_dates is deprecated, will be removed in a future version. "
2102+
"check index.inferred_type instead",
2103+
FutureWarning,
2104+
stacklevel=2,
2105+
)
2106+
return self._is_all_dates
2107+
20952108
# --------------------------------------------------------------------
20962109
# Pickle Methods
20972110

pandas/core/indexes/datetimelike.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class DatetimeIndexOpsMixin(ExtensionIndex):
9898
_hasnans = hasnans # for index / array -agnostic code
9999

100100
@property
101-
def is_all_dates(self) -> bool:
101+
def _is_all_dates(self) -> bool:
102102
return True
103103

104104
# ------------------------------------------------------------------------

pandas/core/indexes/interval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ def func(self, other, sort=sort):
10921092
# --------------------------------------------------------------------
10931093

10941094
@property
1095-
def is_all_dates(self) -> bool:
1095+
def _is_all_dates(self) -> bool:
10961096
"""
10971097
This is False even when left/right contain datetime-like objects,
10981098
as the check is done on the Interval itself

pandas/core/indexes/multi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,7 @@ def to_flat_index(self):
17331733
return Index(self._values, tupleize_cols=False)
17341734

17351735
@property
1736-
def is_all_dates(self) -> bool:
1736+
def _is_all_dates(self) -> bool:
17371737
return False
17381738

17391739
def is_lexsorted(self) -> bool:

pandas/core/indexes/numeric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def _assert_safe_casting(cls, data, subarr):
149149
pass
150150

151151
@property
152-
def is_all_dates(self) -> bool:
152+
def _is_all_dates(self) -> bool:
153153
"""
154154
Checks that all the labels are datetime objects.
155155
"""

pandas/core/missing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def interpolate_1d(
199199
return yvalues
200200

201201
if method == "time":
202-
if not getattr(xvalues, "is_all_dates", None):
202+
if not getattr(xvalues, "_is_all_dates", None):
203203
# if not issubclass(xvalues.dtype.type, np.datetime64):
204204
raise ValueError(
205205
"time-weighted interpolation only works "
@@ -327,7 +327,7 @@ def _interpolate_scipy_wrapper(
327327
"piecewise_polynomial": _from_derivatives,
328328
}
329329

330-
if getattr(x, "is_all_dates", False):
330+
if getattr(x, "_is_all_dates", False):
331331
# GH 5975, scipy.interp1d can't handle datetime64s
332332
x, new_x = x._values.astype("i8"), new_x.astype("i8")
333333

pandas/core/series.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,20 @@ def _set_axis(self, axis: int, labels, fastpath: bool = False) -> None:
409409
if not fastpath:
410410
labels = ensure_index(labels)
411411

412-
is_all_dates = labels.is_all_dates
413-
if is_all_dates:
412+
if labels._is_all_dates:
414413
if not isinstance(labels, (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
415414
try:
416415
labels = DatetimeIndex(labels)
417416
# need to set here because we changed the index
418417
if fastpath:
419418
self._mgr.set_axis(axis, labels)
419+
warnings.warn(
420+
"Automatically casting object-dtype Index of datetimes to "
421+
"DatetimeIndex is deprecated and will be removed in a "
422+
"future version. Explicitly cast to DatetimeIndex instead.",
423+
FutureWarning,
424+
stacklevel=3,
425+
)
420426
except (tslibs.OutOfBoundsDatetime, ValueError):
421427
# labels may exceeds datetime bounds,
422428
# or not be a DatetimeIndex

pandas/plotting/_matplotlib/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ def get_label(i):
12381238
# would be too close together.
12391239
condition = (
12401240
not self._use_dynamic_x()
1241-
and (data.index.is_all_dates and self.use_index)
1241+
and (data.index._is_all_dates and self.use_index)
12421242
and (not self.subplots or (self.subplots and self.sharex))
12431243
)
12441244

0 commit comments

Comments
 (0)