From 3530b3d21c99ebb57aafb4780f5b91a46bdaf6db Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 25 Nov 2023 13:05:01 -0800 Subject: [PATCH] TST: parametrize over dt64 unit (#56165) * TST: parametrize over dt64 unit * TST: specify dt64 unit --- pandas/tests/arithmetic/test_datetime64.py | 6 +- pandas/tests/frame/indexing/test_setitem.py | 5 +- pandas/tests/frame/methods/test_quantile.py | 23 ++++-- pandas/tests/frame/test_reductions.py | 20 ++++-- .../methods/test_groupby_shift_diff.py | 5 +- pandas/tests/groupby/test_timegrouper.py | 24 +++++-- .../tests/groupby/transform/test_transform.py | 21 +++--- .../indexes/datetimes/methods/test_astype.py | 5 +- .../tests/indexes/datetimes/test_indexing.py | 72 +++++++++---------- pandas/tests/indexes/datetimes/test_setops.py | 18 +++-- .../tests/indexes/datetimes/test_timezones.py | 31 +++++--- .../tests/indexes/interval/test_indexing.py | 9 ++- .../indexes/interval/test_interval_range.py | 3 + .../period/methods/test_to_timestamp.py | 22 ++++-- .../tests/indexing/multiindex/test_partial.py | 8 ++- .../tests/indexing/multiindex/test_setitem.py | 9 ++- pandas/tests/io/xml/test_xml_dtypes.py | 5 +- .../tests/resample/test_resampler_grouper.py | 7 +- pandas/tests/reshape/merge/test_join.py | 2 +- pandas/tests/series/methods/test_quantile.py | 42 +++++------ 20 files changed, 205 insertions(+), 132 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index a63bfbf1835a9..9014ba4b6093e 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -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) @@ -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 diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index bc632209ff7e1..0130820fc3de6 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -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 @@ -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) diff --git a/pandas/tests/frame/methods/test_quantile.py b/pandas/tests/frame/methods/test_quantile.py index dcec68ab3530d..787b77a5c725a 100644 --- a/pandas/tests/frame/methods/test_quantile.py +++ b/pandas/tests/frame/methods/test_quantile.py @@ -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 @@ -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) diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index 20ad93e6dce4d..0d71fb0926df9 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -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"]), }, ), @@ -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]), }, ), @@ -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], }, @@ -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], }, @@ -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"), } diff --git a/pandas/tests/groupby/methods/test_groupby_shift_diff.py b/pandas/tests/groupby/methods/test_groupby_shift_diff.py index f2d40867af03a..0ce6a6462a5d8 100644 --- a/pandas/tests/groupby/methods/test_groupby_shift_diff.py +++ b/pandas/tests/groupby/methods/test_groupby_shift_diff.py @@ -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) diff --git a/pandas/tests/groupby/test_timegrouper.py b/pandas/tests/groupby/test_timegrouper.py index be02c7f79ba01..d1faab9cabfba 100644 --- a/pandas/tests/groupby/test_timegrouper.py +++ b/pandas/tests/groupby/test_timegrouper.py @@ -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}) @@ -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) @@ -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) @@ -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), @@ -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) diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py index cf122832d86b4..a6f160d92fb66 100644 --- a/pandas/tests/groupby/transform/test_transform.py +++ b/pandas/tests/groupby/transform/test_transform.py @@ -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"], @@ -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) @@ -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) diff --git a/pandas/tests/indexes/datetimes/methods/test_astype.py b/pandas/tests/indexes/datetimes/methods/test_astype.py index 9ee6250feeac6..c0bc6601769b1 100644 --- a/pandas/tests/indexes/datetimes/methods/test_astype.py +++ b/pandas/tests/indexes/datetimes/methods/test_astype.py @@ -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): @@ -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) diff --git a/pandas/tests/indexes/datetimes/test_indexing.py b/pandas/tests/indexes/datetimes/test_indexing.py index 73de33607ca0b..b7932715c3ac7 100644 --- a/pandas/tests/indexes/datetimes/test_indexing.py +++ b/pandas/tests/indexes/datetimes/test_indexing.py @@ -35,46 +35,43 @@ def test_getitem_slice_keeps_name(self): dr = date_range(st, et, freq="h", name="timebucket") assert dr[1:].name == dr.name - def test_getitem(self): - idx1 = date_range("2011-01-01", "2011-01-31", freq="D", name="idx") - idx2 = date_range( - "2011-01-01", "2011-01-31", freq="D", tz="Asia/Tokyo", name="idx" - ) + @pytest.mark.parametrize("tz", [None, "Asia/Tokyo"]) + def test_getitem(self, tz): + idx = date_range("2011-01-01", "2011-01-31", freq="D", tz=tz, name="idx") - for idx in [idx1, idx2]: - result = idx[0] - assert result == Timestamp("2011-01-01", tz=idx.tz) + result = idx[0] + assert result == Timestamp("2011-01-01", tz=idx.tz) - result = idx[0:5] - expected = date_range( - "2011-01-01", "2011-01-05", freq="D", tz=idx.tz, name="idx" - ) - tm.assert_index_equal(result, expected) - assert result.freq == expected.freq + result = idx[0:5] + expected = date_range( + "2011-01-01", "2011-01-05", freq="D", tz=idx.tz, name="idx" + ) + tm.assert_index_equal(result, expected) + assert result.freq == expected.freq - result = idx[0:10:2] - expected = date_range( - "2011-01-01", "2011-01-09", freq="2D", tz=idx.tz, name="idx" - ) - tm.assert_index_equal(result, expected) - assert result.freq == expected.freq + result = idx[0:10:2] + expected = date_range( + "2011-01-01", "2011-01-09", freq="2D", tz=idx.tz, name="idx" + ) + tm.assert_index_equal(result, expected) + assert result.freq == expected.freq - result = idx[-20:-5:3] - expected = date_range( - "2011-01-12", "2011-01-24", freq="3D", tz=idx.tz, name="idx" - ) - tm.assert_index_equal(result, expected) - assert result.freq == expected.freq + result = idx[-20:-5:3] + expected = date_range( + "2011-01-12", "2011-01-24", freq="3D", tz=idx.tz, name="idx" + ) + tm.assert_index_equal(result, expected) + assert result.freq == expected.freq - result = idx[4::-1] - expected = DatetimeIndex( - ["2011-01-05", "2011-01-04", "2011-01-03", "2011-01-02", "2011-01-01"], - freq="-1D", - tz=idx.tz, - name="idx", - ) - tm.assert_index_equal(result, expected) - assert result.freq == expected.freq + result = idx[4::-1] + expected = DatetimeIndex( + ["2011-01-05", "2011-01-04", "2011-01-03", "2011-01-02", "2011-01-01"], + dtype=idx.dtype, + freq="-1D", + name="idx", + ) + tm.assert_index_equal(result, expected) + assert result.freq == expected.freq @pytest.mark.parametrize("freq", ["B", "C"]) def test_dti_business_getitem(self, freq): @@ -264,8 +261,8 @@ def test_take(self): result = idx.take([3, 2, 5]) expected = DatetimeIndex( ["2011-01-04", "2011-01-03", "2011-01-06"], + dtype=idx.dtype, freq=None, - tz=idx.tz, name="idx", ) tm.assert_index_equal(result, expected) @@ -274,6 +271,7 @@ def test_take(self): result = idx.take([-3, 2, 5]) expected = DatetimeIndex( ["2011-01-29", "2011-01-03", "2011-01-06"], + dtype=idx.dtype, freq=None, tz=idx.tz, name="idx", @@ -314,7 +312,7 @@ def test_take2(self, tz): tz=tz, name="idx", ) - expected = DatetimeIndex(dates, freq=None, name="idx", tz=tz) + expected = DatetimeIndex(dates, freq=None, name="idx", dtype=idx.dtype) taken1 = idx.take([5, 6, 8, 12]) taken2 = idx[[5, 6, 8, 12]] diff --git a/pandas/tests/indexes/datetimes/test_setops.py b/pandas/tests/indexes/datetimes/test_setops.py index 78c23e47897cf..993f88db38ea6 100644 --- a/pandas/tests/indexes/datetimes/test_setops.py +++ b/pandas/tests/indexes/datetimes/test_setops.py @@ -249,19 +249,23 @@ def test_intersection(self, tz, sort): # non-monotonic base = DatetimeIndex( ["2011-01-05", "2011-01-04", "2011-01-02", "2011-01-03"], tz=tz, name="idx" - ) + ).as_unit("ns") rng2 = DatetimeIndex( ["2011-01-04", "2011-01-02", "2011-02-02", "2011-02-03"], tz=tz, name="idx" - ) - expected2 = DatetimeIndex(["2011-01-04", "2011-01-02"], tz=tz, name="idx") + ).as_unit("ns") + expected2 = DatetimeIndex( + ["2011-01-04", "2011-01-02"], tz=tz, name="idx" + ).as_unit("ns") rng3 = DatetimeIndex( ["2011-01-04", "2011-01-02", "2011-02-02", "2011-02-03"], tz=tz, name="other", - ) - expected3 = DatetimeIndex(["2011-01-04", "2011-01-02"], tz=tz, name=None) + ).as_unit("ns") + expected3 = DatetimeIndex( + ["2011-01-04", "2011-01-02"], tz=tz, name=None + ).as_unit("ns") # GH 7880 rng4 = date_range("7/1/2000", "7/31/2000", freq="D", tz=tz, name="idx") @@ -350,7 +354,7 @@ def test_difference_freq(self, sort): index = date_range("20160920", "20160925", freq="D") other = date_range("20160921", "20160924", freq="D") - expected = DatetimeIndex(["20160920", "20160925"], freq=None) + expected = DatetimeIndex(["20160920", "20160925"], dtype="M8[ns]", freq=None) idx_diff = index.difference(other, sort) tm.assert_index_equal(idx_diff, expected) tm.assert_attr_equal("freq", idx_diff, expected) @@ -359,7 +363,7 @@ def test_difference_freq(self, sort): # subset of the original range other = date_range("20160922", "20160925", freq="D") idx_diff = index.difference(other, sort) - expected = DatetimeIndex(["20160920", "20160921"], freq="D") + expected = DatetimeIndex(["20160920", "20160921"], dtype="M8[ns]", freq="D") tm.assert_index_equal(idx_diff, expected) tm.assert_attr_equal("freq", idx_diff, expected) diff --git a/pandas/tests/indexes/datetimes/test_timezones.py b/pandas/tests/indexes/datetimes/test_timezones.py index 379c727f4ed0f..daa5b346eb4ec 100644 --- a/pandas/tests/indexes/datetimes/test_timezones.py +++ b/pandas/tests/indexes/datetimes/test_timezones.py @@ -92,7 +92,7 @@ def test_drop_dst_boundary(self): "201710290245", "201710290300", ], - tz=tz, + dtype="M8[ns, Europe/Brussels]", freq=freq, ambiguous=[ True, @@ -112,10 +112,14 @@ def test_drop_dst_boundary(self): result = index.drop(index[0]) tm.assert_index_equal(result, expected) - def test_date_range_localize(self): - rng = date_range("3/11/2012 03:00", periods=15, freq="h", tz="US/Eastern") - rng2 = DatetimeIndex(["3/11/2012 03:00", "3/11/2012 04:00"], tz="US/Eastern") - rng3 = date_range("3/11/2012 03:00", periods=15, freq="h") + def test_date_range_localize(self, unit): + rng = date_range( + "3/11/2012 03:00", periods=15, freq="h", tz="US/Eastern", unit=unit + ) + rng2 = DatetimeIndex( + ["3/11/2012 03:00", "3/11/2012 04:00"], dtype=f"M8[{unit}, US/Eastern]" + ) + rng3 = date_range("3/11/2012 03:00", periods=15, freq="h", unit=unit) rng3 = rng3.tz_localize("US/Eastern") tm.assert_index_equal(rng._with_freq(None), rng3) @@ -129,10 +133,15 @@ def test_date_range_localize(self): assert val == exp # same UTC value tm.assert_index_equal(rng[:2], rng2) + def test_date_range_localize2(self, unit): # Right before the DST transition - rng = date_range("3/11/2012 00:00", periods=2, freq="h", tz="US/Eastern") + rng = date_range( + "3/11/2012 00:00", periods=2, freq="h", tz="US/Eastern", unit=unit + ) rng2 = DatetimeIndex( - ["3/11/2012 00:00", "3/11/2012 01:00"], tz="US/Eastern", freq="h" + ["3/11/2012 00:00", "3/11/2012 01:00"], + dtype=f"M8[{unit}, US/Eastern]", + freq="h", ) tm.assert_index_equal(rng, rng2) exp = Timestamp("3/11/2012 00:00", tz="US/Eastern") @@ -142,7 +151,9 @@ def test_date_range_localize(self): assert exp.hour == 1 assert rng[1] == exp - rng = date_range("3/11/2012 00:00", periods=10, freq="h", tz="US/Eastern") + rng = date_range( + "3/11/2012 00:00", periods=10, freq="h", tz="US/Eastern", unit=unit + ) assert rng[2].hour == 3 def test_timestamp_equality_different_timezones(self): @@ -231,10 +242,10 @@ def test_dti_convert_tz_aware_datetime_datetime(self, tz): dates = [datetime(2000, 1, 1), datetime(2000, 1, 2), datetime(2000, 1, 3)] dates_aware = [conversion.localize_pydatetime(x, tz) for x in dates] - result = DatetimeIndex(dates_aware) + result = DatetimeIndex(dates_aware).as_unit("ns") assert timezones.tz_compare(result.tz, tz) - converted = to_datetime(dates_aware, utc=True) + converted = to_datetime(dates_aware, utc=True).as_unit("ns") ex_vals = np.array([Timestamp(x).as_unit("ns")._value for x in dates_aware]) tm.assert_numpy_array_equal(converted.asi8, ex_vals) assert converted.tz is timezone.utc diff --git a/pandas/tests/indexes/interval/test_indexing.py b/pandas/tests/indexes/interval/test_indexing.py index 2007a793843c9..fd03047b2c127 100644 --- a/pandas/tests/indexes/interval/test_indexing.py +++ b/pandas/tests/indexes/interval/test_indexing.py @@ -363,15 +363,18 @@ def test_get_indexer_categorical_with_nans(self): def test_get_indexer_datetime(self): ii = IntervalIndex.from_breaks(date_range("2018-01-01", periods=4)) - result = ii.get_indexer(DatetimeIndex(["2018-01-02"])) + # TODO: with mismatched resolution get_indexer currently raises; + # this should probably coerce? + target = DatetimeIndex(["2018-01-02"], dtype="M8[ns]") + result = ii.get_indexer(target) expected = np.array([0], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) - result = ii.get_indexer(DatetimeIndex(["2018-01-02"]).astype(str)) + result = ii.get_indexer(target.astype(str)) tm.assert_numpy_array_equal(result, expected) # https://github.com/pandas-dev/pandas/issues/47772 - result = ii.get_indexer(DatetimeIndex(["2018-01-02"]).asi8) + result = ii.get_indexer(target.asi8) expected = np.array([-1], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) diff --git a/pandas/tests/indexes/interval/test_interval_range.py b/pandas/tests/indexes/interval/test_interval_range.py index 37606bda9efca..d4d4a09c44d13 100644 --- a/pandas/tests/indexes/interval/test_interval_range.py +++ b/pandas/tests/indexes/interval/test_interval_range.py @@ -184,6 +184,9 @@ def test_no_invalid_float_truncation(self, start, end, freq): def test_linspace_dst_transition(self, start, mid, end): # GH 20976: linspace behavior defined from start/end/periods # accounts for the hour gained/lost during DST transition + start = start.as_unit("ns") + mid = mid.as_unit("ns") + end = end.as_unit("ns") result = interval_range(start=start, end=end, periods=2) expected = IntervalIndex.from_breaks([start, mid, end]) tm.assert_index_equal(result, expected) diff --git a/pandas/tests/indexes/period/methods/test_to_timestamp.py b/pandas/tests/indexes/period/methods/test_to_timestamp.py index 7be2602135578..3867f9e3245dc 100644 --- a/pandas/tests/indexes/period/methods/test_to_timestamp.py +++ b/pandas/tests/indexes/period/methods/test_to_timestamp.py @@ -58,7 +58,9 @@ def test_to_timestamp_pi_nat(self): result = index.to_timestamp("D") expected = DatetimeIndex( - [NaT, datetime(2011, 1, 1), datetime(2011, 2, 1)], name="idx" + [NaT, datetime(2011, 1, 1), datetime(2011, 2, 1)], + dtype="M8[ns]", + name="idx", ) tm.assert_index_equal(result, expected) assert result.name == "idx" @@ -98,11 +100,15 @@ def test_to_timestamp_pi_mult(self): idx = PeriodIndex(["2011-01", "NaT", "2011-02"], freq="2M", name="idx") result = idx.to_timestamp() - expected = DatetimeIndex(["2011-01-01", "NaT", "2011-02-01"], name="idx") + expected = DatetimeIndex( + ["2011-01-01", "NaT", "2011-02-01"], dtype="M8[ns]", name="idx" + ) tm.assert_index_equal(result, expected) result = idx.to_timestamp(how="E") - expected = DatetimeIndex(["2011-02-28", "NaT", "2011-03-31"], name="idx") + expected = DatetimeIndex( + ["2011-02-28", "NaT", "2011-03-31"], dtype="M8[ns]", name="idx" + ) expected = expected + Timedelta(1, "D") - Timedelta(1, "ns") tm.assert_index_equal(result, expected) @@ -110,18 +116,22 @@ def test_to_timestamp_pi_combined(self): idx = period_range(start="2011", periods=2, freq="1D1h", name="idx") result = idx.to_timestamp() - expected = DatetimeIndex(["2011-01-01 00:00", "2011-01-02 01:00"], name="idx") + expected = DatetimeIndex( + ["2011-01-01 00:00", "2011-01-02 01:00"], dtype="M8[ns]", name="idx" + ) tm.assert_index_equal(result, expected) result = idx.to_timestamp(how="E") expected = DatetimeIndex( - ["2011-01-02 00:59:59", "2011-01-03 01:59:59"], name="idx" + ["2011-01-02 00:59:59", "2011-01-03 01:59:59"], name="idx", dtype="M8[ns]" ) expected = expected + Timedelta(1, "s") - Timedelta(1, "ns") tm.assert_index_equal(result, expected) result = idx.to_timestamp(how="E", freq="h") - expected = DatetimeIndex(["2011-01-02 00:00", "2011-01-03 01:00"], name="idx") + expected = DatetimeIndex( + ["2011-01-02 00:00", "2011-01-03 01:00"], dtype="M8[ns]", name="idx" + ) expected = expected + Timedelta(1, "h") - Timedelta(1, "ns") tm.assert_index_equal(result, expected) diff --git a/pandas/tests/indexing/multiindex/test_partial.py b/pandas/tests/indexing/multiindex/test_partial.py index 9cf11b4602cb2..7be3d8c657766 100644 --- a/pandas/tests/indexing/multiindex/test_partial.py +++ b/pandas/tests/indexing/multiindex/test_partial.py @@ -5,9 +5,9 @@ from pandas import ( DataFrame, + DatetimeIndex, MultiIndex, date_range, - to_datetime, ) import pandas._testing as tm @@ -219,7 +219,11 @@ def test_setitem_multiple_partial(self, multiindex_dataframe_random_data): @pytest.mark.parametrize( "indexer, exp_idx, exp_values", [ - (slice("2019-2", None), [to_datetime("2019-02-01")], [2, 3]), + ( + slice("2019-2", None), + DatetimeIndex(["2019-02-01"], dtype="M8[ns]"), + [2, 3], + ), ( slice(None, "2019-2"), date_range("2019", periods=2, freq="MS"), diff --git a/pandas/tests/indexing/multiindex/test_setitem.py b/pandas/tests/indexing/multiindex/test_setitem.py index e31b675efb69a..9870868a3e1e9 100644 --- a/pandas/tests/indexing/multiindex/test_setitem.py +++ b/pandas/tests/indexing/multiindex/test_setitem.py @@ -9,7 +9,6 @@ DataFrame, MultiIndex, Series, - Timestamp, date_range, isna, notna, @@ -91,11 +90,11 @@ def test_setitem_multiindex3(self): np.random.default_rng(2).random((12, 4)), index=idx, columns=cols ) - subidx = MultiIndex.from_tuples( - [("A", Timestamp("2015-01-01")), ("A", Timestamp("2015-02-01"))] + subidx = MultiIndex.from_arrays( + [["A", "A"], date_range("2015-01-01", "2015-02-01", freq="MS")] ) - subcols = MultiIndex.from_tuples( - [("foo", Timestamp("2016-01-01")), ("foo", Timestamp("2016-02-01"))] + subcols = MultiIndex.from_arrays( + [["foo", "foo"], date_range("2016-01-01", "2016-02-01", freq="MS")] ) vals = DataFrame( diff --git a/pandas/tests/io/xml/test_xml_dtypes.py b/pandas/tests/io/xml/test_xml_dtypes.py index fb24902efc0f5..a85576ff13f5c 100644 --- a/pandas/tests/io/xml/test_xml_dtypes.py +++ b/pandas/tests/io/xml/test_xml_dtypes.py @@ -9,6 +9,7 @@ from pandas import ( DataFrame, + DatetimeIndex, Series, to_datetime, ) @@ -146,7 +147,9 @@ def test_dtypes_with_names(parser): "Col1": ["square", "circle", "triangle"], "Col2": Series(["00360", "00360", "00180"]).astype("string"), "Col3": Series([4.0, float("nan"), 3.0]).astype("Int64"), - "Col4": to_datetime(["2020-01-01", "2021-01-01", "2022-01-01"]), + "Col4": DatetimeIndex( + ["2020-01-01", "2021-01-01", "2022-01-01"], dtype="M8[ns]" + ), } ) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 4afd3b477c3ee..3cf5201d573d4 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -118,12 +118,11 @@ def test_getitem_multiple(): df = DataFrame(data, index=date_range("2016-01-01", periods=2)) r = df.groupby("id").resample("1D") result = r["buyer"].count() + + exp_mi = pd.MultiIndex.from_arrays([[1, 2], df.index], names=("id", None)) expected = Series( [1, 1], - index=pd.MultiIndex.from_tuples( - [(1, Timestamp("2016-01-01")), (2, Timestamp("2016-01-02"))], - names=["id", None], - ), + index=exp_mi, name="buyer", ) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/reshape/merge/test_join.py b/pandas/tests/reshape/merge/test_join.py index a38e4ffe2eaf7..c4c83e2046b76 100644 --- a/pandas/tests/reshape/merge/test_join.py +++ b/pandas/tests/reshape/merge/test_join.py @@ -787,7 +787,7 @@ def test_join_datetime_string(self): index=[2, 4], columns=["x", "y", "z", "a"], ) - expected["x"] = expected["x"].dt.as_unit("ns") + expected["x"] = expected["x"].astype("M8[ns]") tm.assert_frame_equal(result, expected) def test_join_with_categorical_index(self): diff --git a/pandas/tests/series/methods/test_quantile.py b/pandas/tests/series/methods/test_quantile.py index 016635a50fdf4..fa0563271d7df 100644 --- a/pandas/tests/series/methods/test_quantile.py +++ b/pandas/tests/series/methods/test_quantile.py @@ -105,8 +105,8 @@ def test_quantile_interpolation_dtype(self): def test_quantile_nan(self): # GH 13098 - s = Series([1, 2, 3, 4, np.nan]) - result = s.quantile(0.5) + ser = Series([1, 2, 3, 4, np.nan]) + result = ser.quantile(0.5) expected = 2.5 assert result == expected @@ -114,14 +114,14 @@ def test_quantile_nan(self): s1 = Series([], dtype=object) cases = [s1, Series([np.nan, np.nan])] - for s in cases: - res = s.quantile(0.5) + for ser in cases: + res = ser.quantile(0.5) assert np.isnan(res) - res = s.quantile([0.5]) + res = ser.quantile([0.5]) tm.assert_series_equal(res, Series([np.nan], index=[0.5])) - res = s.quantile([0.2, 0.3]) + res = ser.quantile([0.2, 0.3]) tm.assert_series_equal(res, Series([np.nan, np.nan], index=[0.2, 0.3])) @pytest.mark.parametrize( @@ -160,11 +160,11 @@ def test_quantile_nan(self): ], ) def test_quantile_box(self, case): - s = Series(case, name="XXX") - res = s.quantile(0.5) + ser = Series(case, name="XXX") + res = ser.quantile(0.5) assert res == case[1] - res = s.quantile([0.5]) + res = ser.quantile([0.5]) exp = Series([case[1]], index=[0.5], name="XXX") tm.assert_series_equal(res, exp) @@ -190,35 +190,37 @@ def test_quantile_sparse(self, values, dtype): expected = Series(np.asarray(ser)).quantile([0.5]).astype("Sparse[float]") tm.assert_series_equal(result, expected) - def test_quantile_empty(self): + def test_quantile_empty_float64(self): # floats - s = Series([], dtype="float64") + ser = Series([], dtype="float64") - res = s.quantile(0.5) + res = ser.quantile(0.5) assert np.isnan(res) - res = s.quantile([0.5]) + res = ser.quantile([0.5]) exp = Series([np.nan], index=[0.5]) tm.assert_series_equal(res, exp) + def test_quantile_empty_int64(self): # int - s = Series([], dtype="int64") + ser = Series([], dtype="int64") - res = s.quantile(0.5) + res = ser.quantile(0.5) assert np.isnan(res) - res = s.quantile([0.5]) + res = ser.quantile([0.5]) exp = Series([np.nan], index=[0.5]) tm.assert_series_equal(res, exp) + def test_quantile_empty_dt64(self): # datetime - s = Series([], dtype="datetime64[ns]") + ser = Series([], dtype="datetime64[ns]") - res = s.quantile(0.5) + res = ser.quantile(0.5) assert res is pd.NaT - res = s.quantile([0.5]) - exp = Series([pd.NaT], index=[0.5]) + res = ser.quantile([0.5]) + exp = Series([pd.NaT], index=[0.5], dtype=ser.dtype) tm.assert_series_equal(res, exp) @pytest.mark.parametrize("dtype", [int, float, "Int64"])