Skip to content

Commit

Permalink
Series.values deprecation of converts and drops
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrocariellof committed May 29, 2024
1 parent ca6f9e3 commit ce1d8d9
Show file tree
Hide file tree
Showing 22 changed files with 239 additions and 63 deletions.
2 changes: 1 addition & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2372,4 +2372,4 @@ def external_values(values: ArrayLike) -> ArrayLike:

# TODO(CoW) we should also mark our ExtensionArrays as read-only

return values
return values
10 changes: 8 additions & 2 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -2329,10 +2329,16 @@ def test_dti_add_series(self, tz_naive_fixture, names):
tm.assert_series_equal(result2, expected)

expected = index + Timedelta(seconds=5)
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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)
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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)

Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/arrays/test_timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ 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]")
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/base/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,10 @@ def test_value_counts_datetime64(index_or_series, unit):

# with NaT
s = df["dt"].copy()
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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)
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,10 @@ 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)
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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},
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/copy_view/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ def test_astype_arrow_timestamp():
},
dtype="M8[ns]",
)
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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:
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,10 @@ def test_frame_setitem_datetime64_col_other_units(self, unit):
df[unit] = vals

assert df[unit].dtype == ex_vals.dtype
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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"])
Expand All @@ -323,7 +326,10 @@ 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
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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):
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/frame/methods/test_convert_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ def test_pyarrow_dtype_backend(self):
"g": pd.Series(pd.timedelta_range("1D", periods=3)),
}
)
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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(
{
Expand Down Expand Up @@ -177,7 +180,10 @@ 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"))
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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)
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/frame/methods/test_duplicated.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ def test_frame_datetime64_duplicated():
dates = date_range("2010-07-01", end="2010-08-05")

tst = DataFrame({"symbol": "AAA", "date": dates})
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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()

Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,20 @@ def test_consolidate_datetime64(self):
)

ser_starting = df.starting
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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")
Expand Down
88 changes: 70 additions & 18 deletions pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ 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)
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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
)
Expand All @@ -433,7 +436,10 @@ 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)
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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)
Expand All @@ -446,7 +452,10 @@ 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
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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)
Expand All @@ -458,7 +467,10 @@ 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
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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)
Expand All @@ -474,7 +486,10 @@ 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
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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)
Expand All @@ -488,7 +503,10 @@ 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
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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)
Expand All @@ -499,18 +517,27 @@ def test_date_query_with_non_date(self, engine, parser):
{"dates": date_range("1/1/2012", periods=n), "nondate": np.arange(n)}
)

with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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

with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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):
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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):
Expand Down Expand Up @@ -757,12 +784,18 @@ def test_check_tz_aware_index_query(self, tz_aware_fixture):
)
expected = DataFrame(index=df_index)
df = DataFrame(index=df_index)
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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)
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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)

Expand Down Expand Up @@ -809,9 +842,14 @@ 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)
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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
"(dates1 < 20130101) & (20130101 < dates3)",
engine=engine,
parser=parser,
)
expec = df[(df.dates1 < "20130101") & ("20130101" < df.dates3)]
tm.assert_frame_equal(res, expec)
Expand All @@ -824,9 +862,14 @@ 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
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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
"(dates1 < 20130101) & (20130101 < dates3)",
engine=engine,
parser=parser,
)
expec = df[(df.dates1 < "20130101") & ("20130101" < df.dates3)]
tm.assert_frame_equal(res, expec)
Expand All @@ -838,7 +881,10 @@ 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
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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
)
Expand All @@ -856,7 +902,10 @@ 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
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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
)
Expand All @@ -873,7 +922,10 @@ 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):
with tm.assert_produces_warning(FutureWarning, match="series.values will stop converting tz from dt64tz, interval to object and period to object"):
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):
Expand Down
Loading

0 comments on commit ce1d8d9

Please sign in to comment.