diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 3d069cf11b571..3330ab1178804 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -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`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index def2a2dbd61cf..4f588075bc830 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -9467,12 +9467,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) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index aca7373983a61..ea1b8259eeadd 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -26,7 +26,6 @@ find_common_type, infer_dtype_from, infer_dtype_from_scalar, - maybe_box_datetimelike, maybe_downcast_numeric, maybe_downcast_to_dtype, maybe_infer_dtype_type, @@ -746,11 +745,6 @@ def replace( return [self] if inplace else [self.copy()] values = self.values - if lib.is_scalar(to_replace) and isinstance(values, np.ndarray): - # The only non-DatetimeLike class that also has a non-trivial - # try_coerce_args is ObjectBlock, but that overrides replace, - # so does not get here. - to_replace = convert_scalar_for_putitemlike(to_replace, values.dtype) mask = missing.mask_missing(values, to_replace) if not mask.any(): @@ -845,7 +839,6 @@ def comp(s: Scalar, mask: np.ndarray, regex: bool = False) -> np.ndarray: if isna(s): return ~mask - s = maybe_box_datetimelike(s) return compare_or_regex_search(self.values, s, regex, mask) if self.is_object: @@ -919,8 +912,6 @@ def setitem(self, indexer, value): arr = self.array_values().T arr[indexer] = value return self - elif lib.is_scalar(value): - value = convert_scalar_for_putitemlike(value, values.dtype) else: # current dtype cannot store value, coerce to common dtype @@ -1067,9 +1058,6 @@ def putmask(self, mask, new, axis: int = 0) -> List["Block"]: arr.putmask(mask, new) return [self] - if lib.is_scalar(new): - new = convert_scalar_for_putitemlike(new, self.values.dtype) - if transpose: new_values = new_values.T diff --git a/pandas/core/series.py b/pandas/core/series.py index af8acc0a36e13..d4ae5c2245b5b 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -436,13 +436,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 diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index e8c4a834bdeb1..2199c32dbd0ba 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -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 diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index 274efda55414c..3f4c21389daed 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -2441,17 +2441,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): diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 397a064f6adad..66a4f9598c49b 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -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() @@ -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) diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index aaa58cdb390f7..f94e174a26824 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -177,7 +177,7 @@ def test_fillna_downcast(self): expected = Series([1, 0]) tm.assert_series_equal(result, expected) - def test_timedelta_fillna(self): + def test_timedelta_fillna(self, frame_or_series): # GH#3371 ser = Series( [ @@ -188,9 +188,10 @@ def test_timedelta_fillna(self): ] ) td = ser.diff() + obj = frame_or_series(td) # reg fillna - result = td.fillna(Timedelta(seconds=0)) + result = obj.fillna(Timedelta(seconds=0)) expected = Series( [ timedelta(0), @@ -199,13 +200,14 @@ def test_timedelta_fillna(self): timedelta(days=1, seconds=9 * 3600 + 60 + 1), ] ) - tm.assert_series_equal(result, expected) + expected = frame_or_series(expected) + tm.assert_equal(result, expected) # interpreted as seconds, deprecated with pytest.raises(TypeError, match="Passing integers to fillna"): - td.fillna(1) + obj.fillna(1) - result = td.fillna(Timedelta(seconds=1)) + result = obj.fillna(Timedelta(seconds=1)) expected = Series( [ timedelta(seconds=1), @@ -214,9 +216,10 @@ def test_timedelta_fillna(self): timedelta(days=1, seconds=9 * 3600 + 60 + 1), ] ) - tm.assert_series_equal(result, expected) + expected = frame_or_series(expected) + tm.assert_equal(result, expected) - result = td.fillna(timedelta(days=1, seconds=1)) + result = obj.fillna(timedelta(days=1, seconds=1)) expected = Series( [ timedelta(days=1, seconds=1), @@ -225,9 +228,10 @@ def test_timedelta_fillna(self): timedelta(days=1, seconds=9 * 3600 + 60 + 1), ] ) - tm.assert_series_equal(result, expected) + expected = frame_or_series(expected) + tm.assert_equal(result, expected) - result = td.fillna(np.timedelta64(int(1e9))) + result = obj.fillna(np.timedelta64(int(1e9))) expected = Series( [ timedelta(seconds=1), @@ -236,9 +240,10 @@ def test_timedelta_fillna(self): timedelta(days=1, seconds=9 * 3600 + 60 + 1), ] ) - tm.assert_series_equal(result, expected) + expected = frame_or_series(expected) + tm.assert_equal(result, expected) - result = td.fillna(NaT) + result = obj.fillna(NaT) expected = Series( [ NaT, @@ -248,21 +253,27 @@ def test_timedelta_fillna(self): ], dtype="m8[ns]", ) - tm.assert_series_equal(result, expected) + expected = frame_or_series(expected) + tm.assert_equal(result, expected) # ffill td[2] = np.nan - result = td.ffill() + obj = frame_or_series(td) + result = obj.ffill() expected = td.fillna(Timedelta(seconds=0)) expected[0] = np.nan - tm.assert_series_equal(result, expected) + expected = frame_or_series(expected) + + tm.assert_equal(result, expected) # bfill td[2] = np.nan - result = td.bfill() + obj = frame_or_series(td) + result = obj.bfill() expected = td.fillna(Timedelta(seconds=0)) expected[2] = timedelta(days=1, seconds=9 * 3600 + 60 + 1) - tm.assert_series_equal(result, expected) + expected = frame_or_series(expected) + tm.assert_equal(result, expected) def test_datetime64_fillna(self): @@ -553,7 +564,7 @@ def test_fillna_period(self): tm.assert_series_equal(res, exp) assert res.dtype == "Period[M]" - def test_fillna_dt64_timestamp(self): + def test_fillna_dt64_timestamp(self, frame_or_series): ser = Series( [ Timestamp("20130101"), @@ -563,9 +574,10 @@ def test_fillna_dt64_timestamp(self): ] ) ser[2] = np.nan + obj = frame_or_series(ser) # reg fillna - result = ser.fillna(Timestamp("20130104")) + result = obj.fillna(Timestamp("20130104")) expected = Series( [ Timestamp("20130101"), @@ -574,11 +586,12 @@ def test_fillna_dt64_timestamp(self): Timestamp("20130103 9:01:01"), ] ) - tm.assert_series_equal(result, expected) + expected = frame_or_series(expected) + tm.assert_equal(result, expected) - result = ser.fillna(NaT) - expected = ser - tm.assert_series_equal(result, expected) + result = obj.fillna(NaT) + expected = obj + tm.assert_equal(result, expected) def test_fillna_dt64_non_nao(self): # GH#27419 diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index eabd6a1eb0743..ca7a171947ca0 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1623,8 +1623,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) diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 75e7f8a17eda3..836135c3d6310 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -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) diff --git a/pandas/tests/window/moments/test_moments_rolling_apply.py b/pandas/tests/window/moments/test_moments_rolling_apply.py index e9e672e1d3dae..e48d88b365d8d 100644 --- a/pandas/tests/window/moments/test_moments_rolling_apply.py +++ b/pandas/tests/window/moments/test_moments_rolling_apply.py @@ -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 ) @@ -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)