diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index f4ce36db32813..1ea54edaa1e05 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -200,6 +200,7 @@ starting with 3.0, so it can be safely removed from your code. Other Deprecations ^^^^^^^^^^^^^^^^^^ +- Deprecated :meth:`series.values`, now don't drops the ``tz`` from ``dt64tz``, converts ``interval`` to ``object``, converts ``period`` to ``object``. - Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`56680`) - Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`) - Deprecated allowing non-keyword arguments in :meth:`DataFrame.all`, :meth:`DataFrame.min`, :meth:`DataFrame.max`, :meth:`DataFrame.sum`, :meth:`DataFrame.prod`, :meth:`DataFrame.mean`, :meth:`DataFrame.median`, :meth:`DataFrame.sem`, :meth:`DataFrame.var`, :meth:`DataFrame.std`, :meth:`DataFrame.skew`, :meth:`DataFrame.kurt`, :meth:`Series.all`, :meth:`Series.min`, :meth:`Series.max`, :meth:`Series.sum`, :meth:`Series.prod`, :meth:`Series.mean`, :meth:`Series.median`, :meth:`Series.sem`, :meth:`Series.var`, :meth:`Series.std`, :meth:`Series.skew`, and :meth:`Series.kurt`. (:issue:`57087`) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index cffb1f658a640..1f599dfdea248 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -2349,11 +2349,21 @@ def external_values(values: ArrayLike) -> ArrayLike: proper extension array). """ if isinstance(values, (PeriodArray, IntervalArray)): + warnings.warn( + "series.values will stop converting tz from dt64tz, interval to object and period to object", + FutureWarning, + stacklevel=find_stack_level(), + ) return values.astype(object) elif isinstance(values, (DatetimeArray, TimedeltaArray)): # NB: for datetime64tz this is different from np.asarray(values), since # that returns an object-dtype ndarray of Timestamps. # Avoid raising in .astype in casting from dt64tz to dt64 + warnings.warn( + "series.values will stop converting tz from dt64tz, interval to object and period to object", + FutureWarning, + stacklevel=find_stack_level(), + ) values = values._ndarray if isinstance(values, np.ndarray): diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index f9807310460b4..04e0cb5780493 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -2329,9 +2329,17 @@ def test_dti_add_series(self, tz_naive_fixture, names): tm.assert_series_equal(result2, expected) expected = index + Timedelta(seconds=5) - result3 = ser.values + index + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result3 = ser.values + index tm.assert_index_equal(result3, expected) - result4 = index + ser.values + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result4 = index + ser.values tm.assert_index_equal(result4, expected) @pytest.mark.parametrize("op", [operator.add, roperator.radd, operator.sub]) diff --git a/pandas/tests/arrays/test_timedeltas.py b/pandas/tests/arrays/test_timedeltas.py index bcc52f197ee51..a064d90478f57 100644 --- a/pandas/tests/arrays/test_timedeltas.py +++ b/pandas/tests/arrays/test_timedeltas.py @@ -74,7 +74,11 @@ def test_total_seconds_nanoseconds(self): # issue #48521 start_time = pd.Series(["2145-11-02 06:00:00"]).astype("datetime64[ns]") end_time = pd.Series(["2145-11-02 07:06:00"]).astype("datetime64[ns]") - expected = (end_time - start_time).values / np.timedelta64(1, "s") + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + expected = (end_time - start_time).values / np.timedelta64(1, "s") result = (end_time - start_time).dt.total_seconds().values assert result == expected diff --git a/pandas/tests/base/test_value_counts.py b/pandas/tests/base/test_value_counts.py index c72abfeb9f3e7..0e8b8be1f873c 100644 --- a/pandas/tests/base/test_value_counts.py +++ b/pandas/tests/base/test_value_counts.py @@ -254,7 +254,11 @@ def test_value_counts_datetime64(index_or_series, unit): # with NaT s = df["dt"].copy() - s = klass(list(s.values) + [pd.NaT] * 4) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + s = klass(list(s.values) + [pd.NaT] * 4) if klass is Series: s = s.dt.as_unit(unit) else: diff --git a/pandas/tests/computation/test_eval.py b/pandas/tests/computation/test_eval.py index d52f33fe80434..e896e473f06f7 100644 --- a/pandas/tests/computation/test_eval.py +++ b/pandas/tests/computation/test_eval.py @@ -1507,12 +1507,16 @@ def test_nested_period_index_subscript_expression(self): def test_date_boolean(self, engine, parser): df = DataFrame(np.random.default_rng(2).standard_normal((5, 3))) df["dates1"] = date_range("1/1/2012", periods=5) - res = self.eval( - "df.dates1 < 20130101", - local_dict={"df": df}, - engine=engine, - parser=parser, - ) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + res = self.eval( + "df.dates1 < 20130101", + local_dict={"df": df}, + engine=engine, + parser=parser, + ) expec = df.dates1 < "20130101" tm.assert_series_equal(res, expec) diff --git a/pandas/tests/copy_view/test_astype.py b/pandas/tests/copy_view/test_astype.py index 2d959bb16e7d5..3af2175b19ae0 100644 --- a/pandas/tests/copy_view/test_astype.py +++ b/pandas/tests/copy_view/test_astype.py @@ -187,7 +187,11 @@ def test_astype_arrow_timestamp(): }, dtype="M8[ns]", ) - result = df.astype("timestamp[ns][pyarrow]") + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result = df.astype("timestamp[ns][pyarrow]") assert not result._mgr._has_no_reference(0) if pa_version_under12p0: assert not np.shares_memory( diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index 1fe11c62188e8..6c910616f7b6a 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -305,7 +305,11 @@ def test_frame_setitem_datetime64_col_other_units(self, unit): df[unit] = vals assert df[unit].dtype == ex_vals.dtype - assert (df[unit].values == ex_vals).all() + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + assert (df[unit].values == ex_vals).all() @pytest.mark.parametrize("unit", ["h", "m", "s", "ms", "D", "M", "Y"]) def test_frame_setitem_existing_datetime64_col_other_units(self, unit): @@ -322,7 +326,11 @@ def test_frame_setitem_existing_datetime64_col_other_units(self, unit): # We overwrite existing dt64 column with new, non-nano dt64 vals df["dates"] = vals - assert (df["dates"].values == ex_vals).all() + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + assert (df["dates"].values == ex_vals).all() def test_setitem_dt64tz(self, timezone_frame): df = timezone_frame diff --git a/pandas/tests/frame/methods/test_convert_dtypes.py b/pandas/tests/frame/methods/test_convert_dtypes.py index 521d2cb14ac6a..33bbeea9266e9 100644 --- a/pandas/tests/frame/methods/test_convert_dtypes.py +++ b/pandas/tests/frame/methods/test_convert_dtypes.py @@ -61,7 +61,11 @@ def test_pyarrow_dtype_backend(self): "g": pd.Series(pd.timedelta_range("1D", periods=3)), } ) - result = df.convert_dtypes(dtype_backend="pyarrow") + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result = df.convert_dtypes(dtype_backend="pyarrow") expected = pd.DataFrame( { "a": pd.arrays.ArrowExtensionArray( @@ -176,7 +180,11 @@ def test_convert_dtypes_pyarrow_timestamp(self): # GH 54191 pytest.importorskip("pyarrow") ser = pd.Series(pd.date_range("2020-01-01", "2020-01-02", freq="1min")) - expected = ser.astype("timestamp[ms][pyarrow]") + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + expected = ser.astype("timestamp[ms][pyarrow]") result = expected.convert_dtypes(dtype_backend="pyarrow") tm.assert_series_equal(result, expected) diff --git a/pandas/tests/frame/methods/test_duplicated.py b/pandas/tests/frame/methods/test_duplicated.py index 6052b61ea8db5..748615dd721bb 100644 --- a/pandas/tests/frame/methods/test_duplicated.py +++ b/pandas/tests/frame/methods/test_duplicated.py @@ -109,7 +109,11 @@ def test_frame_datetime64_duplicated(): dates = date_range("2010-07-01", end="2010-08-05") tst = DataFrame({"symbol": "AAA", "date": dates}) - result = tst.duplicated(["date", "symbol"]) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result = tst.duplicated(["date", "symbol"]) assert (-result).all() tst = DataFrame({"date": dates}) diff --git a/pandas/tests/frame/test_block_internals.py b/pandas/tests/frame/test_block_internals.py index 3f0e829f66361..488a52ecf3acc 100644 --- a/pandas/tests/frame/test_block_internals.py +++ b/pandas/tests/frame/test_block_internals.py @@ -293,13 +293,21 @@ def test_consolidate_datetime64(self): ) ser_starting = df.starting - ser_starting.index = ser_starting.values + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + ser_starting.index = ser_starting.values ser_starting = ser_starting.tz_localize("US/Eastern") ser_starting = ser_starting.tz_convert("UTC") ser_starting.index.name = "starting" ser_ending = df.ending - ser_ending.index = ser_ending.values + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + ser_ending.index = ser_ending.values ser_ending = ser_ending.tz_localize("US/Eastern") ser_ending = ser_ending.tz_convert("UTC") ser_ending.index.name = "ending" diff --git a/pandas/tests/frame/test_query_eval.py b/pandas/tests/frame/test_query_eval.py index 643d342b052a4..515ce4ab85046 100644 --- a/pandas/tests/frame/test_query_eval.py +++ b/pandas/tests/frame/test_query_eval.py @@ -421,9 +421,13 @@ def test_date_query_with_attribute_access(self, engine, parser): df["dates1"] = date_range("1/1/2012", periods=5) df["dates2"] = date_range("1/1/2013", periods=5) df["dates3"] = date_range("1/1/2014", periods=5) - res = df.query( - "@df.dates1 < 20130101 < @df.dates3", engine=engine, parser=parser - ) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + res = df.query( + "@df.dates1 < 20130101 < @df.dates3", engine=engine, parser=parser + ) expec = df[(df.dates1 < "20130101") & ("20130101" < df.dates3)] tm.assert_frame_equal(res, expec) @@ -432,7 +436,11 @@ def test_date_query_no_attribute_access(self, engine, parser): df["dates1"] = date_range("1/1/2012", periods=5) df["dates2"] = date_range("1/1/2013", periods=5) df["dates3"] = date_range("1/1/2014", periods=5) - res = df.query("dates1 < 20130101 < dates3", engine=engine, parser=parser) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + res = df.query("dates1 < 20130101 < dates3", engine=engine, parser=parser) expec = df[(df.dates1 < "20130101") & ("20130101" < df.dates3)] tm.assert_frame_equal(res, expec) @@ -444,7 +452,11 @@ def test_date_query_with_NaT(self, engine, parser): df["dates3"] = date_range("1/1/2014", periods=n) df.loc[np.random.default_rng(2).random(n) > 0.5, "dates1"] = pd.NaT df.loc[np.random.default_rng(2).random(n) > 0.5, "dates3"] = pd.NaT - res = df.query("dates1 < 20130101 < dates3", engine=engine, parser=parser) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + res = df.query("dates1 < 20130101 < dates3", engine=engine, parser=parser) expec = df[(df.dates1 < "20130101") & ("20130101" < df.dates3)] tm.assert_frame_equal(res, expec) @@ -455,7 +467,11 @@ def test_date_index_query(self, engine, parser): df["dates3"] = date_range("1/1/2014", periods=n) return_value = df.set_index("dates1", inplace=True, drop=True) assert return_value is None - res = df.query("index < 20130101 < dates3", engine=engine, parser=parser) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + res = df.query("index < 20130101 < dates3", engine=engine, parser=parser) expec = df[(df.index < "20130101") & ("20130101" < df.dates3)] tm.assert_frame_equal(res, expec) @@ -470,7 +486,11 @@ def test_date_index_query_with_NaT(self, engine, parser): df.iloc[0, 0] = pd.NaT return_value = df.set_index("dates1", inplace=True, drop=True) assert return_value is None - res = df.query("index < 20130101 < dates3", engine=engine, parser=parser) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + res = df.query("index < 20130101 < dates3", engine=engine, parser=parser) expec = df[(df.index < "20130101") & ("20130101" < df.dates3)] tm.assert_frame_equal(res, expec) @@ -483,7 +503,11 @@ def test_date_index_query_with_NaT_duplicates(self, engine, parser): df.loc[np.random.default_rng(2).random(n) > 0.5, "dates1"] = pd.NaT return_value = df.set_index("dates1", inplace=True, drop=True) assert return_value is None - res = df.query("dates1 < 20130101 < dates3", engine=engine, parser=parser) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + res = df.query("dates1 < 20130101 < dates3", engine=engine, parser=parser) expec = df[(df.index.to_series() < "20130101") & ("20130101" < df.dates3)] tm.assert_frame_equal(res, expec) @@ -493,16 +517,28 @@ def test_date_query_with_non_date(self, engine, parser): {"dates": date_range("1/1/2012", periods=n), "nondate": np.arange(n)} ) - result = df.query("dates == nondate", parser=parser, engine=engine) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result = df.query("dates == nondate", parser=parser, engine=engine) assert len(result) == 0 - result = df.query("dates != nondate", parser=parser, engine=engine) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result = df.query("dates != nondate", parser=parser, engine=engine) tm.assert_frame_equal(result, df) msg = r"Invalid comparison between dtype=datetime64\[ns\] and ndarray" for op in ["<", ">", "<=", ">="]: with pytest.raises(TypeError, match=msg): - df.query(f"dates {op} nondate", parser=parser, engine=engine) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + df.query(f"dates {op} nondate", parser=parser, engine=engine) def test_query_syntax_error(self, engine, parser): df = DataFrame({"i": range(10), "+": range(3, 13), "r": range(4, 14)}) @@ -748,11 +784,19 @@ def test_check_tz_aware_index_query(self, tz_aware_fixture): ) expected = DataFrame(index=df_index) df = DataFrame(index=df_index) - result = df.query('"2018-01-03 00:00:00+00" < time') + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result = df.query('"2018-01-03 00:00:00+00" < time') tm.assert_frame_equal(result, expected) expected = DataFrame(df_index) - result = df.reset_index().query('"2018-01-03 00:00:00+00" < time') + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result = df.reset_index().query('"2018-01-03 00:00:00+00" < time') tm.assert_frame_equal(result, expected) def test_method_calls_in_query(self, engine, parser): @@ -798,9 +842,15 @@ def test_date_query_no_attribute_access(self, engine, parser): df["dates1"] = date_range("1/1/2012", periods=5) df["dates2"] = date_range("1/1/2013", periods=5) df["dates3"] = date_range("1/1/2014", periods=5) - res = df.query( - "(dates1 < 20130101) & (20130101 < dates3)", engine=engine, parser=parser - ) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + res = df.query( + "(dates1 < 20130101) & (20130101 < dates3)", + engine=engine, + parser=parser, + ) expec = df[(df.dates1 < "20130101") & ("20130101" < df.dates3)] tm.assert_frame_equal(res, expec) @@ -812,9 +862,15 @@ def test_date_query_with_NaT(self, engine, parser): df["dates3"] = date_range("1/1/2014", periods=n) df.loc[np.random.default_rng(2).random(n) > 0.5, "dates1"] = pd.NaT df.loc[np.random.default_rng(2).random(n) > 0.5, "dates3"] = pd.NaT - res = df.query( - "(dates1 < 20130101) & (20130101 < dates3)", engine=engine, parser=parser - ) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + res = df.query( + "(dates1 < 20130101) & (20130101 < dates3)", + engine=engine, + parser=parser, + ) expec = df[(df.dates1 < "20130101") & ("20130101" < df.dates3)] tm.assert_frame_equal(res, expec) @@ -825,9 +881,13 @@ def test_date_index_query(self, engine, parser): df["dates3"] = date_range("1/1/2014", periods=n) return_value = df.set_index("dates1", inplace=True, drop=True) assert return_value is None - res = df.query( - "(index < 20130101) & (20130101 < dates3)", engine=engine, parser=parser - ) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + res = df.query( + "(index < 20130101) & (20130101 < dates3)", engine=engine, parser=parser + ) expec = df[(df.index < "20130101") & ("20130101" < df.dates3)] tm.assert_frame_equal(res, expec) @@ -842,9 +902,13 @@ def test_date_index_query_with_NaT(self, engine, parser): df.iloc[0, 0] = pd.NaT return_value = df.set_index("dates1", inplace=True, drop=True) assert return_value is None - res = df.query( - "(index < 20130101) & (20130101 < dates3)", engine=engine, parser=parser - ) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + res = df.query( + "(index < 20130101) & (20130101 < dates3)", engine=engine, parser=parser + ) expec = df[(df.index < "20130101") & ("20130101" < df.dates3)] tm.assert_frame_equal(res, expec) @@ -858,7 +922,11 @@ def test_date_index_query_with_NaT_duplicates(self, engine, parser): assert return_value is None msg = r"'BoolOp' nodes are not implemented" with pytest.raises(NotImplementedError, match=msg): - df.query("index < 20130101 < dates3", engine=engine, parser=parser) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + df.query("index < 20130101 < dates3", engine=engine, parser=parser) def test_nested_scope(self, engine, parser): # smoke test diff --git a/pandas/tests/indexing/test_datetime.py b/pandas/tests/indexing/test_datetime.py index af7533399ea74..0222236ed2d92 100644 --- a/pandas/tests/indexing/test_datetime.py +++ b/pandas/tests/indexing/test_datetime.py @@ -175,17 +175,25 @@ def test_getitem_str_slice_millisecond_resolution(self, frame_or_series): def test_getitem_pyarrow_index(self, frame_or_series): # GH 53644 pytest.importorskip("pyarrow") - obj = frame_or_series( - range(5), - index=date_range("2020", freq="D", periods=5).astype( - "timestamp[us][pyarrow]" - ), - ) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + obj = frame_or_series( + range(5), + index=date_range("2020", freq="D", periods=5).astype( + "timestamp[us][pyarrow]" + ), + ) result = obj.loc[obj.index[:-3]] - expected = frame_or_series( - range(2), - index=date_range("2020", freq="D", periods=2).astype( - "timestamp[us][pyarrow]" - ), - ) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + expected = frame_or_series( + range(2), + index=date_range("2020", freq="D", periods=2).astype( + "timestamp[us][pyarrow]" + ), + ) tm.assert_equal(result, expected) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 01dab14c7e528..61513776c3634 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -700,8 +700,18 @@ def test_loc_assign_non_ns_datetime(self, unit): } ) - df.loc[:, unit] = df.loc[:, "timestamp"].values.astype(f"datetime64[{unit}]") - df["expected"] = df.loc[:, "timestamp"].values.astype(f"datetime64[{unit}]") + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + df.loc[:, unit] = df.loc[:, "timestamp"].values.astype( + f"datetime64[{unit}]" + ) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + df["expected"] = df.loc[:, "timestamp"].values.astype(f"datetime64[{unit}]") expected = Series(df.loc[:, "expected"], name=unit) tm.assert_series_equal(df.loc[:, unit], expected) diff --git a/pandas/tests/io/excel/test_readers.py b/pandas/tests/io/excel/test_readers.py index f0a72ba6163fa..570992f5ef3e1 100644 --- a/pandas/tests/io/excel/test_readers.py +++ b/pandas/tests/io/excel/test_readers.py @@ -624,12 +624,16 @@ def test_dtype_backend(self, read_ext, dtype_backend, engine, tmp_excel): from pandas.arrays import ArrowExtensionArray - expected = DataFrame( - { - col: ArrowExtensionArray(pa.array(df[col], from_pandas=True)) - for col in df.columns - } - ) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + expected = DataFrame( + { + col: ArrowExtensionArray(pa.array(df[col], from_pandas=True)) + for col in df.columns + } + ) # pyarrow by default infers timestamp resolution as us, not ns expected["i"] = ArrowExtensionArray( expected["i"].array._pa_array.cast(pa.timestamp(unit="us")) diff --git a/pandas/tests/plotting/frame/test_frame_subplots.py b/pandas/tests/plotting/frame/test_frame_subplots.py index a98f4b56ebf4d..1f0e9ba6c1447 100644 --- a/pandas/tests/plotting/frame/test_frame_subplots.py +++ b/pandas/tests/plotting/frame/test_frame_subplots.py @@ -148,10 +148,17 @@ def test_subplots_timeseries_y_axis(self, col): "text": ["This", "should", "fail"], } testdata = DataFrame(data) - - ax = testdata.plot(y=col) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + ax = testdata.plot(y=col) result = ax.get_lines()[0].get_data()[1] - expected = testdata[col].values + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + expected = testdata[col].values assert (result == expected).all() def test_subplots_timeseries_y_text_error(self): diff --git a/pandas/tests/series/accessors/test_dt_accessor.py b/pandas/tests/series/accessors/test_dt_accessor.py index 8c60f7beb317d..100cb5cf8e923 100644 --- a/pandas/tests/series/accessors/test_dt_accessor.py +++ b/pandas/tests/series/accessors/test_dt_accessor.py @@ -119,20 +119,32 @@ def test_dt_namespace_accessor_datetime64(self, freq): assert result.dtype == object result = ser.dt.tz_localize("US/Eastern") - exp_values = DatetimeIndex(ser.values).tz_localize("US/Eastern") + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + exp_values = DatetimeIndex(ser.values).tz_localize("US/Eastern") expected = Series(exp_values, index=ser.index, name="xxx") tm.assert_series_equal(result, expected) tz_result = result.dt.tz assert str(tz_result) == "US/Eastern" freq_result = ser.dt.freq - assert freq_result == DatetimeIndex(ser.values, freq="infer").freq + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + assert freq_result == DatetimeIndex(ser.values, freq="infer").freq # let's localize, then convert result = ser.dt.tz_localize("UTC").dt.tz_convert("US/Eastern") - exp_values = ( - DatetimeIndex(ser.values).tz_localize("UTC").tz_convert("US/Eastern") - ) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + exp_values = ( + DatetimeIndex(ser.values).tz_localize("UTC").tz_convert("US/Eastern") + ) expected = Series(exp_values, index=ser.index, name="xxx") tm.assert_series_equal(result, expected) @@ -162,7 +174,11 @@ def test_dt_namespace_accessor_datetime64tz(self): tz_result = result.dt.tz assert str(tz_result) == "CET" freq_result = ser.dt.freq - assert freq_result == DatetimeIndex(ser.values, freq="infer").freq + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + assert freq_result == DatetimeIndex(ser.values, freq="infer").freq def test_dt_namespace_accessor_timedelta(self): # GH#7207, GH#11128 @@ -203,7 +219,11 @@ def test_dt_namespace_accessor_timedelta(self): assert result.dtype == "float64" freq_result = ser.dt.freq - assert freq_result == TimedeltaIndex(ser.values, freq="infer").freq + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + assert freq_result == TimedeltaIndex(ser.values, freq="infer").freq def test_dt_namespace_accessor_period(self): # GH#7207, GH#11128 @@ -222,7 +242,11 @@ def test_dt_namespace_accessor_period(self): getattr(ser.dt, prop) freq_result = ser.dt.freq - assert freq_result == PeriodIndex(ser.values).freq + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + assert freq_result == PeriodIndex(ser.values).freq def test_dt_namespace_accessor_index_and_values(self): # both diff --git a/pandas/tests/series/methods/test_astype.py b/pandas/tests/series/methods/test_astype.py index 4b2122e25f819..dfdc5b838df69 100644 --- a/pandas/tests/series/methods/test_astype.py +++ b/pandas/tests/series/methods/test_astype.py @@ -259,8 +259,11 @@ def test_astype_datetime64tz(self): expected = Series(ser.astype(object), dtype=object) tm.assert_series_equal(result, expected) - result = Series(ser.values).dt.tz_localize("UTC").dt.tz_convert(ser.dt.tz) - tm.assert_series_equal(result, ser) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result = Series(ser.values).dt.tz_localize("UTC").dt.tz_convert(ser.dt.tz) # astype - object, preserves on construction result = Series(ser.astype(object)) @@ -271,11 +274,19 @@ def test_astype_datetime64tz(self): msg = "Cannot use .astype to convert from timezone-naive" with pytest.raises(TypeError, match=msg): # dt64->dt64tz astype deprecated - Series(ser.values).astype("datetime64[ns, US/Eastern]") + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + Series(ser.values).astype("datetime64[ns, US/Eastern]") with pytest.raises(TypeError, match=msg): # dt64->dt64tz astype deprecated - Series(ser.values).astype(ser.dtype) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + Series(ser.values).astype(ser.dtype) result = ser.astype("datetime64[ns, CET]") expected = Series(date_range("20130101 06:00:00", periods=3, tz="CET")) diff --git a/pandas/tests/series/methods/test_copy.py b/pandas/tests/series/methods/test_copy.py index ad5417d330d51..6fc25b5f8f063 100644 --- a/pandas/tests/series/methods/test_copy.py +++ b/pandas/tests/series/methods/test_copy.py @@ -49,10 +49,14 @@ def test_copy_tzaware(self, deep): # INFO(CoW) a shallow copy doesn't yet copy the data # but parent will not be modified (CoW) - if deep is None or deep is False: - assert np.may_share_memory(ser.values, ser2.values) - else: - assert not np.may_share_memory(ser.values, ser2.values) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + if deep is None or deep is False: + assert np.may_share_memory(ser.values, ser2.values) + else: + assert not np.may_share_memory(ser.values, ser2.values) ser2[0] = Timestamp("1999/01/01", tz="UTC") diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index 592dba253532d..4f0746890c7f5 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -30,7 +30,11 @@ def test_fillna_nat(self): series = Series([0, 1, 2, NaT._value], dtype="M8[ns]") filled = series.ffill() - filled2 = series.fillna(value=series.values[2]) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + filled2 = series.fillna(value=series.values[2]) expected = series.copy() expected.iloc[3] = expected.iloc[2] @@ -40,7 +44,11 @@ def test_fillna_nat(self): df = DataFrame({"A": series}) filled = df.ffill() - filled2 = df.fillna(value=series.values[2]) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + filled2 = df.fillna(value=series.values[2]) expected = DataFrame({"A": expected}) tm.assert_frame_equal(filled, expected) tm.assert_frame_equal(filled2, expected) diff --git a/pandas/tests/series/methods/test_isin.py b/pandas/tests/series/methods/test_isin.py index 937b85a547bcd..abce1074edcef 100644 --- a/pandas/tests/series/methods/test_isin.py +++ b/pandas/tests/series/methods/test_isin.py @@ -50,7 +50,11 @@ def test_isin_datetimelike_mismatched_reso(self): ser = Series(date_range("jan-01-2013", "jan-05-2013")) # fails on dtype conversion in the first place - day_values = np.asarray(ser[0:2].values).astype("datetime64[D]") + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + day_values = np.asarray(ser[0:2].values).astype("datetime64[D]") result = ser.isin(day_values) tm.assert_series_equal(result, expected) @@ -79,7 +83,11 @@ def test_isin_with_i8(self): result = s.isin(s[0:2]) tm.assert_series_equal(result, expected) - result = s.isin(s[0:2].values) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result = s.isin(s[0:2].values) tm.assert_series_equal(result, expected) result = s.isin([s[1]]) diff --git a/pandas/tests/series/methods/test_values.py b/pandas/tests/series/methods/test_values.py index cb1595e68264f..1e5cfd9f80947 100644 --- a/pandas/tests/series/methods/test_values.py +++ b/pandas/tests/series/methods/test_values.py @@ -19,7 +19,11 @@ class TestValues: ) def test_values_object_extension_dtypes(self, data): # https://github.com/pandas-dev/pandas/issues/23995 - result = Series(data).values + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result = Series(data).values expected = np.array(data.astype(object)) tm.assert_numpy_array_equal(result, expected) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 3f9d5bbe806bb..0b93457a65871 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1107,7 +1107,11 @@ def test_constructor_with_datetime_tz(self): assert "datetime64[ns, US/Eastern]" in str(s) # export - result = s.values + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result = s.values assert isinstance(result, np.ndarray) assert result.dtype == "datetime64[ns]" @@ -1234,7 +1238,11 @@ def test_construction_interval(self, interval_constructor): intervals = interval_constructor.from_breaks(np.arange(3), closed="right") result = Series(intervals) assert result.dtype == "interval[int64, right]" - tm.assert_index_equal(Index(result.values), Index(intervals)) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + tm.assert_index_equal(Index(result.values), Index(intervals)) @pytest.mark.parametrize( "data_constructor", [list, np.array], ids=["list", "ndarray[object]"] @@ -1270,17 +1278,31 @@ def test_construction_consistency(self): # Pre-2.0 dt64 values were treated as utc, which was inconsistent # with DatetimeIndex, which treats them as wall times, see GH#33401 - result = Series(ser.values, dtype=ser.dtype) - expected = Series(ser.values).dt.tz_localize(ser.dtype.tz) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + result = Series(ser.values, dtype=ser.dtype) + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): + expected = Series(ser.values).dt.tz_localize(ser.dtype.tz) tm.assert_series_equal(result, expected) - with tm.assert_produces_warning(None): + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): # one suggested alternative to the deprecated (changed in 2.0) usage middle = Series(ser.values).dt.tz_localize("UTC") result = middle.dt.tz_convert(ser.dtype.tz) tm.assert_series_equal(result, ser) - with tm.assert_produces_warning(None): + with tm.assert_produces_warning( + FutureWarning, + match="series.values will stop converting tz from dt64tz, interval to object and period to object", + ): # the other suggested alternative to the deprecated usage result = Series(ser.values.view("int64"), dtype=ser.dtype) tm.assert_series_equal(result, ser)