Skip to content

Commit

Permalink
TST: parametrize over dt64 unit (#56165)
Browse files Browse the repository at this point in the history
* TST: parametrize over dt64 unit

* TST: specify dt64 unit
  • Loading branch information
jbrockmendel authored Nov 25, 2023
1 parent aeed91c commit 3530b3d
Show file tree
Hide file tree
Showing 20 changed files with 205 additions and 132 deletions.
6 changes: 3 additions & 3 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ def test_dt64arr_add_sub_td64_nat(self, box_with_array, tz_naive_fixture):

dti = date_range("1994-04-01", periods=9, tz=tz, freq="QS")
other = np.timedelta64("NaT")
expected = DatetimeIndex(["NaT"] * 9, tz=tz)
expected = DatetimeIndex(["NaT"] * 9, tz=tz).as_unit("ns")

obj = tm.box_expected(dti, box_with_array)
expected = tm.box_expected(expected, box_with_array)
Expand Down Expand Up @@ -1590,13 +1590,13 @@ class TestDatetime64OverflowHandling:

def test_dt64_overflow_masking(self, box_with_array):
# GH#25317
left = Series([Timestamp("1969-12-31")])
left = Series([Timestamp("1969-12-31")], dtype="M8[ns]")
right = Series([NaT])

left = tm.box_expected(left, box_with_array)
right = tm.box_expected(right, box_with_array)

expected = TimedeltaIndex([NaT])
expected = TimedeltaIndex([NaT], dtype="m8[ns]")
expected = tm.box_expected(expected, box_with_array)

result = left - right
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,8 @@ def test_setitem_frame_dup_cols_dtype(self):

def test_frame_setitem_empty_dataframe(self):
# GH#28871
df = DataFrame({"date": [datetime(2000, 1, 1)]}).set_index("date")
dti = DatetimeIndex(["2000-01-01"], dtype="M8[ns]", name="date")
df = DataFrame({"date": dti}).set_index("date")
df = df[0:0].copy()

df["3010"] = None
Expand All @@ -1363,6 +1364,6 @@ def test_frame_setitem_empty_dataframe(self):
expected = DataFrame(
[],
columns=["3010", "2010"],
index=Index([], dtype="datetime64[ns]", name="date"),
index=dti[:0],
)
tm.assert_frame_equal(df, expected)
23 changes: 16 additions & 7 deletions pandas/tests/frame/methods/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,23 +623,23 @@ def test_quantile_nan(self, interp_method, request, using_array_manager):
exp = DataFrame({"a": [3.0, 4.0], "b": [np.nan, np.nan]}, index=[0.5, 0.75])
tm.assert_frame_equal(res, exp)

def test_quantile_nat(self, interp_method, request, using_array_manager):
def test_quantile_nat(self, interp_method, request, using_array_manager, unit):
interpolation, method = interp_method
if method == "table" and using_array_manager:
request.applymarker(pytest.mark.xfail(reason="Axis name incorrectly set."))
# full NaT column
df = DataFrame({"a": [pd.NaT, pd.NaT, pd.NaT]})
df = DataFrame({"a": [pd.NaT, pd.NaT, pd.NaT]}, dtype=f"M8[{unit}]")

res = df.quantile(
0.5, numeric_only=False, interpolation=interpolation, method=method
)
exp = Series([pd.NaT], index=["a"], name=0.5)
exp = Series([pd.NaT], index=["a"], name=0.5, dtype=f"M8[{unit}]")
tm.assert_series_equal(res, exp)

res = df.quantile(
[0.5], numeric_only=False, interpolation=interpolation, method=method
)
exp = DataFrame({"a": [pd.NaT]}, index=[0.5])
exp = DataFrame({"a": [pd.NaT]}, index=[0.5], dtype=f"M8[{unit}]")
tm.assert_frame_equal(res, exp)

# mixed non-null / full null column
Expand All @@ -651,20 +651,29 @@ def test_quantile_nat(self, interp_method, request, using_array_manager):
Timestamp("2012-01-03"),
],
"b": [pd.NaT, pd.NaT, pd.NaT],
}
},
dtype=f"M8[{unit}]",
)

res = df.quantile(
0.5, numeric_only=False, interpolation=interpolation, method=method
)
exp = Series([Timestamp("2012-01-02"), pd.NaT], index=["a", "b"], name=0.5)
exp = Series(
[Timestamp("2012-01-02"), pd.NaT],
index=["a", "b"],
name=0.5,
dtype=f"M8[{unit}]",
)
tm.assert_series_equal(res, exp)

res = df.quantile(
[0.5], numeric_only=False, interpolation=interpolation, method=method
)
exp = DataFrame(
[[Timestamp("2012-01-02"), pd.NaT]], index=[0.5], columns=["a", "b"]
[[Timestamp("2012-01-02"), pd.NaT]],
index=[0.5],
columns=["a", "b"],
dtype=f"M8[{unit}]",
)
tm.assert_frame_equal(res, exp)

Expand Down
20 changes: 14 additions & 6 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ def test_sem(self, datetime_frame):
"C": [1.0],
"D": ["a"],
"E": Categorical(["a"], categories=["a"]),
"F": to_datetime(["2000-1-2"]),
"F": pd.DatetimeIndex(["2000-01-02"], dtype="M8[ns]"),
"G": to_timedelta(["1 days"]),
},
),
Expand All @@ -610,7 +610,7 @@ def test_sem(self, datetime_frame):
"C": [np.nan],
"D": np.array([np.nan], dtype=object),
"E": Categorical([np.nan], categories=["a"]),
"F": [pd.NaT],
"F": pd.DatetimeIndex([pd.NaT], dtype="M8[ns]"),
"G": to_timedelta([pd.NaT]),
},
),
Expand All @@ -621,7 +621,9 @@ def test_sem(self, datetime_frame):
"I": [8, 9, np.nan, np.nan],
"J": [1, np.nan, np.nan, np.nan],
"K": Categorical(["a", np.nan, np.nan, np.nan], categories=["a"]),
"L": to_datetime(["2000-1-2", "NaT", "NaT", "NaT"]),
"L": pd.DatetimeIndex(
["2000-01-02", "NaT", "NaT", "NaT"], dtype="M8[ns]"
),
"M": to_timedelta(["1 days", "nan", "nan", "nan"]),
"N": [0, 1, 2, 3],
},
Expand All @@ -633,7 +635,9 @@ def test_sem(self, datetime_frame):
"I": [8, 9, np.nan, np.nan],
"J": [1, np.nan, np.nan, np.nan],
"K": Categorical([np.nan, "a", np.nan, np.nan], categories=["a"]),
"L": to_datetime(["NaT", "2000-1-2", "NaT", "NaT"]),
"L": pd.DatetimeIndex(
["NaT", "2000-01-02", "NaT", "NaT"], dtype="M8[ns]"
),
"M": to_timedelta(["nan", "1 days", "nan", "nan"]),
"N": [0, 1, 2, 3],
},
Expand All @@ -648,13 +652,17 @@ def test_mode_dropna(self, dropna, expected):
"C": [1, np.nan, np.nan, np.nan],
"D": [np.nan, np.nan, "a", np.nan],
"E": Categorical([np.nan, np.nan, "a", np.nan]),
"F": to_datetime(["NaT", "2000-1-2", "NaT", "NaT"]),
"F": pd.DatetimeIndex(
["NaT", "2000-01-02", "NaT", "NaT"], dtype="M8[ns]"
),
"G": to_timedelta(["1 days", "nan", "nan", "nan"]),
"H": [8, 8, 9, 9],
"I": [9, 9, 8, 8],
"J": [1, 1, np.nan, np.nan],
"K": Categorical(["a", np.nan, "a", np.nan]),
"L": to_datetime(["2000-1-2", "2000-1-2", "NaT", "NaT"]),
"L": pd.DatetimeIndex(
["2000-01-02", "2000-01-02", "NaT", "NaT"], dtype="M8[ns]"
),
"M": to_timedelta(["1 days", "nan", "1 days", "nan"]),
"N": np.arange(4, dtype="int64"),
}
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/groupby/methods/test_groupby_shift_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ def test_group_diff_real_frame(any_real_numpy_dtype):
[Timedelta("5 days"), Timedelta("6 days"), Timedelta("7 days")],
],
)
def test_group_diff_datetimelike(data):
def test_group_diff_datetimelike(data, unit):
df = DataFrame({"a": [1, 2, 2], "b": data})
df["b"] = df["b"].dt.as_unit(unit)
result = df.groupby("a")["b"].diff()
expected = Series([NaT, NaT, Timedelta("1 days")], name="b")
expected = Series([NaT, NaT, Timedelta("1 days")], name="b").dt.as_unit(unit)
tm.assert_series_equal(result, expected)


Expand Down
24 changes: 18 additions & 6 deletions pandas/tests/groupby/test_timegrouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,17 @@ def test_groupby_with_timegrouper(self):
for df in [df_original, df_reordered]:
df = df.set_index(["Date"])

exp_dti = date_range(
"20130901",
"20131205",
freq="5D",
name="Date",
inclusive="left",
unit=df.index.unit,
)
expected = DataFrame(
{"Buyer": 0, "Quantity": 0},
index=date_range(
"20130901", "20131205", freq="5D", name="Date", inclusive="left"
),
index=exp_dti,
)
# Cast to object to avoid implicit cast when setting entry to "CarlCarlCarl"
expected = expected.astype({"Buyer": object})
Expand Down Expand Up @@ -514,6 +520,7 @@ def test_groupby_groups_datetimeindex(self):
groups = grouped.groups
assert isinstance(next(iter(groups.keys())), datetime)

def test_groupby_groups_datetimeindex2(self):
# GH#11442
index = date_range("2015/01/01", periods=5, name="date")
df = DataFrame({"A": [5, 6, 7, 8, 9], "B": [1, 2, 3, 4, 5]}, index=index)
Expand Down Expand Up @@ -876,7 +883,9 @@ def test_groupby_apply_timegrouper_with_nat_dict_returns(

res = gb["Quantity"].apply(lambda x: {"foo": len(x)})

dti = date_range("2013-09-01", "2013-10-01", freq="5D", name="Date")
df = gb.obj
unit = df["Date"]._values.unit
dti = date_range("2013-09-01", "2013-10-01", freq="5D", name="Date", unit=unit)
mi = MultiIndex.from_arrays([dti, ["foo"] * len(dti)])
expected = Series([3, 0, 0, 0, 0, 0, 2], index=mi, name="Quantity")
tm.assert_series_equal(res, expected)
Expand All @@ -890,7 +899,9 @@ def test_groupby_apply_timegrouper_with_nat_scalar_returns(

res = gb["Quantity"].apply(lambda x: x.iloc[0] if len(x) else np.nan)

dti = date_range("2013-09-01", "2013-10-01", freq="5D", name="Date")
df = gb.obj
unit = df["Date"]._values.unit
dti = date_range("2013-09-01", "2013-10-01", freq="5D", name="Date", unit=unit)
expected = Series(
[18, np.nan, np.nan, np.nan, np.nan, np.nan, 5],
index=dti._with_freq(None),
Expand Down Expand Up @@ -919,9 +930,10 @@ def test_groupby_apply_timegrouper_with_nat_apply_squeeze(
with tm.assert_produces_warning(FutureWarning, match=msg):
res = gb.apply(lambda x: x["Quantity"] * 2)

dti = Index([Timestamp("2013-12-31")], dtype=df["Date"].dtype, name="Date")
expected = DataFrame(
[[36, 6, 6, 10, 2]],
index=Index([Timestamp("2013-12-31")], name="Date"),
index=dti,
columns=Index([0, 1, 5, 2, 3], name="Quantity"),
)
tm.assert_frame_equal(res, expected)
Expand Down
21 changes: 13 additions & 8 deletions pandas/tests/groupby/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,15 @@ def test_transform_fast2():
)
result = df.groupby("grouping").transform("first")

dates = [
Timestamp("2014-1-1"),
Timestamp("2014-1-2"),
Timestamp("2014-1-2"),
Timestamp("2014-1-4"),
]
dates = pd.Index(
[
Timestamp("2014-1-1"),
Timestamp("2014-1-2"),
Timestamp("2014-1-2"),
Timestamp("2014-1-4"),
],
dtype="M8[ns]",
)
expected = DataFrame(
{"f": [1.1, 2.1, 2.1, 4.5], "d": dates, "i": [1, 2, 2, 4]},
columns=["f", "i", "d"],
Expand Down Expand Up @@ -532,7 +535,7 @@ def test_series_fast_transform_date():
Timestamp("2014-1-2"),
Timestamp("2014-1-4"),
]
expected = Series(dates, name="d")
expected = Series(dates, name="d", dtype="M8[ns]")
tm.assert_series_equal(result, expected)


Expand Down Expand Up @@ -1204,7 +1207,9 @@ def test_groupby_transform_with_datetimes(func, values):

result = stocks.groupby(stocks["week_id"])["price"].transform(func)

expected = Series(data=pd.to_datetime(values), index=dates, name="price")
expected = Series(
data=pd.to_datetime(values).as_unit("ns"), index=dates, name="price"
)

tm.assert_series_equal(result, expected)

Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexes/datetimes/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def test_integer_index_astype_datetime(self, tz, dtype):
# GH 20997, 20964, 24559
val = [Timestamp("2018-01-01", tz=tz).as_unit("ns")._value]
result = Index(val, name="idx").astype(dtype)
expected = DatetimeIndex(["2018-01-01"], tz=tz, name="idx")
expected = DatetimeIndex(["2018-01-01"], tz=tz, name="idx").as_unit("ns")
tm.assert_index_equal(result, expected)

def test_dti_astype_period(self):
Expand All @@ -312,8 +312,9 @@ class TestAstype:
def test_astype_category(self, tz):
obj = date_range("2000", periods=2, tz=tz, name="idx")
result = obj.astype("category")
dti = DatetimeIndex(["2000-01-01", "2000-01-02"], tz=tz).as_unit("ns")
expected = pd.CategoricalIndex(
[Timestamp("2000-01-01", tz=tz), Timestamp("2000-01-02", tz=tz)],
dti,
name="idx",
)
tm.assert_index_equal(result, expected)
Expand Down
Loading

0 comments on commit 3530b3d

Please sign in to comment.