Skip to content

Commit

Permalink
Backport PR #38679: Revert "Deprecated casting an object-dtype index …
Browse files Browse the repository at this point in the history
…of datetime objects to DatetimeIndex in the Series constructor (23598)" (#38699)

Co-authored-by: Simon Hawkins <simonjayhawkins@gmail.com>
  • Loading branch information
meeseeksmachine and simonjayhawkins committed Dec 26, 2020
1 parent e7454af commit 2f47238
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 71 deletions.
1 change: 0 additions & 1 deletion doc/source/whatsnew/v1.2.0.rst
Expand Up @@ -524,7 +524,6 @@ Deprecations
- Deprecated indexing :class:`DataFrame` rows with a single datetime-like string as ``df[string]``
(given the ambiguity whether it is indexing the rows or selecting a column), use
``df.loc[string]`` instead (:issue:`36179`)
- Deprecated casting an object-dtype index of ``datetime`` objects to :class:`.DatetimeIndex` in the :class:`Series` constructor (:issue:`23598`)
- Deprecated :meth:`Index.is_all_dates` (:issue:`27744`)
- The default value of ``regex`` for :meth:`Series.str.replace` will change from ``True`` to ``False`` in a future release. In addition, single character regular expressions will *not* be treated as literal strings when ``regex=True`` is set. (:issue:`24804`)
- Deprecated automatic alignment on comparison operations between :class:`DataFrame` and :class:`Series`, do ``frame, ser = frame.align(ser, axis=1, copy=False)`` before e.g. ``frame == ser`` (:issue:`28759`)
Expand Down
6 changes: 0 additions & 6 deletions pandas/core/generic.py
Expand Up @@ -9689,12 +9689,6 @@ def truncate(
# if we have a date index, convert to dates, otherwise
# treat like a slice
if ax._is_all_dates:
if is_object_dtype(ax.dtype):
warnings.warn(
"Treating object-dtype Index of date objects as DatetimeIndex "
"is deprecated, will be removed in a future version.",
FutureWarning,
)
from pandas.core.tools.datetimes import to_datetime

before = to_datetime(before)
Expand Down
7 changes: 0 additions & 7 deletions pandas/core/series.py
Expand Up @@ -430,13 +430,6 @@ def _set_axis(self, axis: int, labels, fastpath: bool = False) -> None:
# need to set here because we changed the index
if fastpath:
self._mgr.set_axis(axis, labels)
warnings.warn(
"Automatically casting object-dtype Index of datetimes to "
"DatetimeIndex is deprecated and will be removed in a "
"future version. Explicitly cast to DatetimeIndex instead.",
FutureWarning,
stacklevel=3,
)
except (tslibs.OutOfBoundsDatetime, ValueError):
# labels may exceeds datetime bounds,
# or not be a DatetimeIndex
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/indexing/test_indexing.py
Expand Up @@ -535,9 +535,7 @@ def test_string_slice(self):
df["2011"]

with pytest.raises(KeyError, match="'2011'"):
with tm.assert_produces_warning(FutureWarning):
# This does an is_all_dates check
df.loc["2011", 0]
df.loc["2011", 0]

df = DataFrame()
assert not df.index._is_all_dates
Expand Down
14 changes: 5 additions & 9 deletions pandas/tests/io/pytables/test_store.py
Expand Up @@ -2358,17 +2358,13 @@ def test_series(self, setup_path):
ts = tm.makeTimeSeries()
self._check_roundtrip(ts, tm.assert_series_equal, path=setup_path)

with tm.assert_produces_warning(FutureWarning):
# auto-casting object->DatetimeIndex deprecated
ts2 = Series(ts.index, Index(ts.index, dtype=object))
ts2 = Series(ts.index, Index(ts.index, dtype=object))
self._check_roundtrip(ts2, tm.assert_series_equal, path=setup_path)

with tm.assert_produces_warning(FutureWarning):
# auto-casting object->DatetimeIndex deprecated
ts3 = Series(
ts.values, Index(np.asarray(ts.index, dtype=object), dtype=object)
)
self._check_roundtrip(ts3, tm.assert_series_equal, path=setup_path)
ts3 = Series(ts.values, Index(np.asarray(ts.index, dtype=object), dtype=object))
self._check_roundtrip(
ts3, tm.assert_series_equal, path=setup_path, check_index_type=False
)

def test_float_index(self, setup_path):

Expand Down
26 changes: 6 additions & 20 deletions pandas/tests/plotting/test_datetimelike.py
Expand Up @@ -277,16 +277,9 @@ def test_irreg_hf(self):
_, ax = self.plt.subplots()
df2 = df.copy()
df2.index = df.index.astype(object)
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# This warning will be emitted
# pandas/core/frame.py:3216:
# FutureWarning: Automatically casting object-dtype Index of datetimes
# to DatetimeIndex is deprecated and will be removed in a future version.
# Explicitly cast to DatetimeIndex instead.
# return klass(values, index=self.index, name=name, fastpath=True)
df2.plot(ax=ax)
diffs = Series(ax.get_lines()[0].get_xydata()[:, 0]).diff()
assert (np.fabs(diffs[1:] - sec) < 1e-8).all()
df2.plot(ax=ax)
diffs = Series(ax.get_lines()[0].get_xydata()[:, 0]).diff()
assert (np.fabs(diffs[1:] - sec) < 1e-8).all()

def test_irregular_datetime64_repr_bug(self):
ser = tm.makeTimeSeries()
Expand Down Expand Up @@ -997,16 +990,9 @@ def test_irreg_dtypes(self):
# np.datetime64
idx = date_range("1/1/2000", periods=10)
idx = idx[[0, 2, 5, 9]].astype(object)
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# This warning will be emitted
# pandas/core/frame.py:3216:
# FutureWarning: Automatically casting object-dtype Index of datetimes
# to DatetimeIndex is deprecated and will be removed in a future version.
# Explicitly cast to DatetimeIndex instead.
# return klass(values, index=self.index, name=name, fastpath=True)
df = DataFrame(np.random.randn(len(idx), 3), idx)
_, ax = self.plt.subplots()
_check_plot_works(df.plot, ax=ax)
df = DataFrame(np.random.randn(len(idx), 3), idx)
_, ax = self.plt.subplots()
_check_plot_works(df.plot, ax=ax)

def test_time(self):
t = datetime(1, 1, 1, 3, 30, 0)
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/series/test_constructors.py
Expand Up @@ -1614,8 +1614,7 @@ def test_constructor_infer_index_tz(self):
class TestSeriesConstructorIndexCoercion:
def test_series_constructor_datetimelike_index_coercion(self):
idx = tm.makeDateIndex(10000)
with tm.assert_produces_warning(FutureWarning):
ser = Series(np.random.randn(len(idx)), idx.astype(object))
ser = Series(np.random.randn(len(idx)), idx.astype(object))
with tm.assert_produces_warning(FutureWarning):
assert ser.index.is_all_dates
assert isinstance(ser.index, DatetimeIndex)
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/series/test_repr.py
Expand Up @@ -184,9 +184,7 @@ def test_timeseries_repr_object_dtype(self):
index = Index(
[datetime(2000, 1, 1) + timedelta(i) for i in range(1000)], dtype=object
)
with tm.assert_produces_warning(FutureWarning):
# Index.is_all_dates deprecated
ts = Series(np.random.randn(len(index)), index)
ts = Series(np.random.randn(len(index)), index)
repr(ts)

ts = tm.makeTimeSeries(1000)
Expand Down
34 changes: 14 additions & 20 deletions pandas/tests/window/moments/test_moments_rolling_apply.py
Expand Up @@ -122,16 +122,13 @@ def test_center_reindex_series(raw, series):
s = [f"x{x:d}" for x in range(12)]
minp = 10

warn = None if raw else FutureWarning
with tm.assert_produces_warning(warn, check_stacklevel=False):
# GH#36697 is_all_dates deprecated
series_xp = (
series.reindex(list(series.index) + s)
.rolling(window=25, min_periods=minp)
.apply(f, raw=raw)
.shift(-12)
.reindex(series.index)
)
series_xp = (
series.reindex(list(series.index) + s)
.rolling(window=25, min_periods=minp)
.apply(f, raw=raw)
.shift(-12)
.reindex(series.index)
)
series_rs = series.rolling(window=25, min_periods=minp, center=True).apply(
f, raw=raw
)
Expand All @@ -143,15 +140,12 @@ def test_center_reindex_frame(raw, frame):
s = [f"x{x:d}" for x in range(12)]
minp = 10

warn = None if raw else FutureWarning
with tm.assert_produces_warning(warn, check_stacklevel=False):
# GH#36697 is_all_dates deprecated
frame_xp = (
frame.reindex(list(frame.index) + s)
.rolling(window=25, min_periods=minp)
.apply(f, raw=raw)
.shift(-12)
.reindex(frame.index)
)
frame_xp = (
frame.reindex(list(frame.index) + s)
.rolling(window=25, min_periods=minp)
.apply(f, raw=raw)
.shift(-12)
.reindex(frame.index)
)
frame_rs = frame.rolling(window=25, min_periods=minp, center=True).apply(f, raw=raw)
tm.assert_frame_equal(frame_xp, frame_rs)

0 comments on commit 2f47238

Please sign in to comment.