diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index 0d71fb0926df9..5456615f6f028 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -798,7 +798,7 @@ def test_std_timedelta64_skipna_false(self): "values", [["2022-01-01", "2022-01-02", pd.NaT, "2022-01-03"], 4 * [pd.NaT]] ) def test_std_datetime64_with_nat( - self, values, skipna, using_array_manager, request + self, values, skipna, using_array_manager, request, unit ): # GH#51335 if using_array_manager and ( @@ -808,13 +808,14 @@ def test_std_datetime64_with_nat( reason="GH#51446: Incorrect type inference on NaT in reduction result" ) request.applymarker(mark) - df = DataFrame({"a": to_datetime(values)}) + dti = to_datetime(values).as_unit(unit) + df = DataFrame({"a": dti}) result = df.std(skipna=skipna) if not skipna or all(value is pd.NaT for value in values): - expected = Series({"a": pd.NaT}, dtype="timedelta64[ns]") + expected = Series({"a": pd.NaT}, dtype=f"timedelta64[{unit}]") else: # 86400000000000ns == 1 day - expected = Series({"a": 86400000000000}, dtype="timedelta64[ns]") + expected = Series({"a": 86400000000000}, dtype=f"timedelta64[{unit}]") tm.assert_series_equal(result, expected) def test_sum_corner(self): diff --git a/pandas/tests/groupby/test_timegrouper.py b/pandas/tests/groupby/test_timegrouper.py index b8b34d365d051..a5ac5b09bfd34 100644 --- a/pandas/tests/groupby/test_timegrouper.py +++ b/pandas/tests/groupby/test_timegrouper.py @@ -537,7 +537,9 @@ def test_groupby_groups_datetimeindex2(self): for date in dates: result = grouped.get_group(date) data = [[df.loc[date, "A"], df.loc[date, "B"]]] - expected_index = DatetimeIndex([date], name="date", freq="D") + expected_index = DatetimeIndex( + [date], name="date", freq="D", dtype=index.dtype + ) expected = DataFrame(data, columns=list("AB"), index=expected_index) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/indexes/datetimes/methods/test_round.py b/pandas/tests/indexes/datetimes/methods/test_round.py index ad5dd37eacd7c..cde4a3a65804d 100644 --- a/pandas/tests/indexes/datetimes/methods/test_round.py +++ b/pandas/tests/indexes/datetimes/methods/test_round.py @@ -40,9 +40,9 @@ def test_round_invalid(self, freq, error_msg): with pytest.raises(ValueError, match=error_msg): dti.round(freq) - def test_round(self, tz_naive_fixture): + def test_round(self, tz_naive_fixture, unit): tz = tz_naive_fixture - rng = date_range(start="2016-01-01", periods=5, freq="30Min", tz=tz) + rng = date_range(start="2016-01-01", periods=5, freq="30Min", tz=tz, unit=unit) elt = rng[1] expected_rng = DatetimeIndex( @@ -53,10 +53,11 @@ def test_round(self, tz_naive_fixture): Timestamp("2016-01-01 02:00:00", tz=tz), Timestamp("2016-01-01 02:00:00", tz=tz), ] - ) + ).as_unit(unit) expected_elt = expected_rng[1] - tm.assert_index_equal(rng.round(freq="h"), expected_rng) + result = rng.round(freq="h") + tm.assert_index_equal(result, expected_rng) assert elt.round(freq="h") == expected_elt msg = INVALID_FREQ_ERR_MSG @@ -74,9 +75,9 @@ def test_round(self, tz_naive_fixture): def test_round2(self, tz_naive_fixture): tz = tz_naive_fixture # GH#14440 & GH#15578 - index = DatetimeIndex(["2016-10-17 12:00:00.0015"], tz=tz) + index = DatetimeIndex(["2016-10-17 12:00:00.0015"], tz=tz).as_unit("ns") result = index.round("ms") - expected = DatetimeIndex(["2016-10-17 12:00:00.002000"], tz=tz) + expected = DatetimeIndex(["2016-10-17 12:00:00.002000"], tz=tz).as_unit("ns") tm.assert_index_equal(result, expected) for freq in ["us", "ns"]: @@ -84,20 +85,21 @@ def test_round2(self, tz_naive_fixture): def test_round3(self, tz_naive_fixture): tz = tz_naive_fixture - index = DatetimeIndex(["2016-10-17 12:00:00.00149"], tz=tz) + index = DatetimeIndex(["2016-10-17 12:00:00.00149"], tz=tz).as_unit("ns") result = index.round("ms") - expected = DatetimeIndex(["2016-10-17 12:00:00.001000"], tz=tz) + expected = DatetimeIndex(["2016-10-17 12:00:00.001000"], tz=tz).as_unit("ns") tm.assert_index_equal(result, expected) def test_round4(self, tz_naive_fixture): - index = DatetimeIndex(["2016-10-17 12:00:00.001501031"]) + index = DatetimeIndex(["2016-10-17 12:00:00.001501031"], dtype="M8[ns]") result = index.round("10ns") - expected = DatetimeIndex(["2016-10-17 12:00:00.001501030"]) + expected = DatetimeIndex(["2016-10-17 12:00:00.001501030"], dtype="M8[ns]") tm.assert_index_equal(result, expected) + ts = "2016-10-17 12:00:00.001501031" + dti = DatetimeIndex([ts], dtype="M8[ns]") with tm.assert_produces_warning(False): - ts = "2016-10-17 12:00:00.001501031" - DatetimeIndex([ts]).round("1010ns") + dti.round("1010ns") def test_no_rounding_occurs(self, tz_naive_fixture): # GH 21262 @@ -112,9 +114,10 @@ def test_no_rounding_occurs(self, tz_naive_fixture): Timestamp("2016-01-01 00:06:00", tz=tz), Timestamp("2016-01-01 00:08:00", tz=tz), ] - ) + ).as_unit("ns") - tm.assert_index_equal(rng.round(freq="2min"), expected_rng) + result = rng.round(freq="2min") + tm.assert_index_equal(result, expected_rng) @pytest.mark.parametrize( "test_input, rounder, freq, expected", diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index 35c3604de3d63..4a0b192244dc8 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -707,10 +707,14 @@ def test_constructor_dtype(self): idx = DatetimeIndex( ["2013-01-01", "2013-01-02"], dtype="datetime64[ns, US/Eastern]" ) - expected = DatetimeIndex(["2013-01-01", "2013-01-02"]).tz_localize("US/Eastern") + expected = ( + DatetimeIndex(["2013-01-01", "2013-01-02"]) + .as_unit("ns") + .tz_localize("US/Eastern") + ) tm.assert_index_equal(idx, expected) - idx = DatetimeIndex(["2013-01-01", "2013-01-02"], tz="US/Eastern") + idx = DatetimeIndex(["2013-01-01", "2013-01-02"], tz="US/Eastern").as_unit("ns") tm.assert_index_equal(idx, expected) def test_constructor_dtype_tz_mismatch_raises(self): @@ -774,7 +778,7 @@ def test_constructor_start_end_with_tz(self, tz): result = date_range(freq="D", start=start, end=end, tz=tz) expected = DatetimeIndex( ["2013-01-01 06:00:00", "2013-01-02 06:00:00"], - tz="America/Los_Angeles", + dtype="M8[ns, America/Los_Angeles]", freq="D", ) 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 b7932715c3ac7..bfbcdcff51ee6 100644 --- a/pandas/tests/indexes/datetimes/test_indexing.py +++ b/pandas/tests/indexes/datetimes/test_indexing.py @@ -226,58 +226,54 @@ def test_take_nan_first_datetime(self): expected = DatetimeIndex([index[-1], index[0], index[1]]) tm.assert_index_equal(result, expected) - def test_take(self): + @pytest.mark.parametrize("tz", [None, "Asia/Tokyo"]) + def test_take(self, tz): # GH#10295 - 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" + idx = date_range("2011-01-01", "2011-01-31", freq="D", name="idx", tz=tz) + + result = idx.take([0]) + assert result == Timestamp("2011-01-01", tz=idx.tz) + + result = idx.take([0, 1, 2]) + expected = date_range( + "2011-01-01", "2011-01-03", freq="D", tz=idx.tz, name="idx" ) + tm.assert_index_equal(result, expected) + assert result.freq == expected.freq - for idx in [idx1, idx2]: - result = idx.take([0]) - assert result == Timestamp("2011-01-01", tz=idx.tz) + result = idx.take([0, 2, 4]) + expected = date_range( + "2011-01-01", "2011-01-05", freq="2D", tz=idx.tz, name="idx" + ) + tm.assert_index_equal(result, expected) + assert result.freq == expected.freq - result = idx.take([0, 1, 2]) - expected = date_range( - "2011-01-01", "2011-01-03", freq="D", tz=idx.tz, name="idx" - ) - tm.assert_index_equal(result, expected) - assert result.freq == expected.freq + result = idx.take([7, 4, 1]) + expected = date_range( + "2011-01-08", "2011-01-02", freq="-3D", tz=idx.tz, name="idx" + ) + tm.assert_index_equal(result, expected) + assert result.freq == expected.freq - result = idx.take([0, 2, 4]) - expected = date_range( - "2011-01-01", "2011-01-05", freq="2D", tz=idx.tz, name="idx" - ) - tm.assert_index_equal(result, expected) - assert result.freq == expected.freq + result = idx.take([3, 2, 5]) + expected = DatetimeIndex( + ["2011-01-04", "2011-01-03", "2011-01-06"], + dtype=idx.dtype, + freq=None, + name="idx", + ) + tm.assert_index_equal(result, expected) + assert result.freq is None - result = idx.take([7, 4, 1]) - expected = date_range( - "2011-01-08", "2011-01-02", freq="-3D", tz=idx.tz, name="idx" - ) - tm.assert_index_equal(result, expected) - assert result.freq == expected.freq - - result = idx.take([3, 2, 5]) - expected = DatetimeIndex( - ["2011-01-04", "2011-01-03", "2011-01-06"], - dtype=idx.dtype, - freq=None, - name="idx", - ) - tm.assert_index_equal(result, expected) - assert result.freq is None - - 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", - ) - tm.assert_index_equal(result, expected) - assert result.freq is None + result = idx.take([-3, 2, 5]) + expected = DatetimeIndex( + ["2011-01-29", "2011-01-03", "2011-01-06"], + dtype=idx.dtype, + freq=None, + name="idx", + ) + tm.assert_index_equal(result, expected) + assert result.freq is None def test_take_invalid_kwargs(self): idx = date_range("2011-01-01", "2011-01-31", freq="D", name="idx") diff --git a/pandas/tests/indexing/test_coercion.py b/pandas/tests/indexing/test_coercion.py index 03691ca318037..45a9c207f0acc 100644 --- a/pandas/tests/indexing/test_coercion.py +++ b/pandas/tests/indexing/test_coercion.py @@ -256,13 +256,13 @@ def test_insert_float_index( def test_insert_index_datetimes(self, fill_val, exp_dtype, insert_value): obj = pd.DatetimeIndex( ["2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04"], tz=fill_val.tz - ) + ).as_unit("ns") assert obj.dtype == exp_dtype exp = pd.DatetimeIndex( ["2011-01-01", fill_val.date(), "2011-01-02", "2011-01-03", "2011-01-04"], tz=fill_val.tz, - ) + ).as_unit("ns") self._assert_insert_conversion(obj, fill_val, exp, exp_dtype) if fill_val.tz: diff --git a/pandas/tests/indexing/test_datetime.py b/pandas/tests/indexing/test_datetime.py index 6510612ba6f87..af7533399ea74 100644 --- a/pandas/tests/indexing/test_datetime.py +++ b/pandas/tests/indexing/test_datetime.py @@ -57,7 +57,10 @@ def test_indexing_fast_xs(self): df = DataFrame({"a": date_range("2014-01-01", periods=10, tz="UTC")}) result = df.iloc[5] expected = Series( - [Timestamp("2014-01-06 00:00:00+0000", tz="UTC")], index=["a"], name=5 + [Timestamp("2014-01-06 00:00:00+0000", tz="UTC")], + index=["a"], + name=5, + dtype="M8[ns, UTC]", ) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/reshape/concat/test_datetimes.py b/pandas/tests/reshape/concat/test_datetimes.py index c061a393bb1a6..c00a2dc92a52b 100644 --- a/pandas/tests/reshape/concat/test_datetimes.py +++ b/pandas/tests/reshape/concat/test_datetimes.py @@ -52,18 +52,14 @@ def test_concat_datetime_timezone(self): df2 = DataFrame({"b": [1, 2, 3]}, index=idx2) result = concat([df1, df2], axis=1) - exp_idx = ( - DatetimeIndex( - [ - "2011-01-01 00:00:00+01:00", - "2011-01-01 01:00:00+01:00", - "2011-01-01 02:00:00+01:00", - ], - freq="h", - ) - .tz_convert("UTC") - .tz_convert("Europe/Paris") - .as_unit("ns") + exp_idx = DatetimeIndex( + [ + "2011-01-01 00:00:00+01:00", + "2011-01-01 01:00:00+01:00", + "2011-01-01 02:00:00+01:00", + ], + dtype="M8[ns, Europe/Paris]", + freq="h", ) expected = DataFrame( @@ -431,21 +427,25 @@ def test_concat_multiindex_with_tz(self): # GH 6606 df = DataFrame( { - "dt": [ - datetime(2014, 1, 1), - datetime(2014, 1, 2), - datetime(2014, 1, 3), - ], + "dt": DatetimeIndex( + [ + datetime(2014, 1, 1), + datetime(2014, 1, 2), + datetime(2014, 1, 3), + ], + dtype="M8[ns, US/Pacific]", + ), "b": ["A", "B", "C"], "c": [1, 2, 3], "d": [4, 5, 6], } ) - df["dt"] = df["dt"].apply(lambda d: Timestamp(d, tz="US/Pacific")) df = df.set_index(["dt", "b"]) exp_idx1 = DatetimeIndex( - ["2014-01-01", "2014-01-02", "2014-01-03"] * 2, tz="US/Pacific", name="dt" + ["2014-01-01", "2014-01-02", "2014-01-03"] * 2, + dtype="M8[ns, US/Pacific]", + name="dt", ) exp_idx2 = Index(["A", "B", "C"] * 2, name="b") exp_idx = MultiIndex.from_arrays([exp_idx1, exp_idx2]) diff --git a/pandas/tests/series/indexing/test_setitem.py b/pandas/tests/series/indexing/test_setitem.py index 168be838ec768..0f3577a214186 100644 --- a/pandas/tests/series/indexing/test_setitem.py +++ b/pandas/tests/series/indexing/test_setitem.py @@ -126,7 +126,8 @@ def test_setitem_with_tz_dst(self, indexer_sli): Timestamp("2016-11-06 00:00-04:00", tz=tz), Timestamp("2011-01-01 00:00-05:00", tz=tz), Timestamp("2016-11-06 01:00-05:00", tz=tz), - ] + ], + dtype=orig.dtype, ) # scalar @@ -138,6 +139,7 @@ def test_setitem_with_tz_dst(self, indexer_sli): vals = Series( [Timestamp("2011-01-01", tz=tz), Timestamp("2012-01-01", tz=tz)], index=[1, 2], + dtype=orig.dtype, ) assert vals.dtype == f"datetime64[ns, {tz}]" @@ -146,7 +148,8 @@ def test_setitem_with_tz_dst(self, indexer_sli): Timestamp("2016-11-06 00:00", tz=tz), Timestamp("2011-01-01 00:00", tz=tz), Timestamp("2012-01-01 00:00", tz=tz), - ] + ], + dtype=orig.dtype, ) ser = orig.copy() diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index f77bff049124b..972d403fff997 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1154,24 +1154,24 @@ def test_constructor_with_datetime_tz5(self): def test_constructor_with_datetime_tz4(self): # inference - s = Series( + ser = Series( [ Timestamp("2013-01-01 13:00:00-0800", tz="US/Pacific"), Timestamp("2013-01-02 14:00:00-0800", tz="US/Pacific"), ] ) - assert s.dtype == "datetime64[ns, US/Pacific]" - assert lib.infer_dtype(s, skipna=True) == "datetime64" + assert ser.dtype == "datetime64[ns, US/Pacific]" + assert lib.infer_dtype(ser, skipna=True) == "datetime64" def test_constructor_with_datetime_tz3(self): - s = Series( + ser = Series( [ Timestamp("2013-01-01 13:00:00-0800", tz="US/Pacific"), Timestamp("2013-01-02 14:00:00-0800", tz="US/Eastern"), ] ) - assert s.dtype == "object" - assert lib.infer_dtype(s, skipna=True) == "datetime" + assert ser.dtype == "object" + assert lib.infer_dtype(ser, skipna=True) == "datetime" def test_constructor_with_datetime_tz2(self): # with all NaT @@ -1587,7 +1587,7 @@ def test_NaT_scalar(self): def test_NaT_cast(self): # GH10747 result = Series([np.nan]).astype("M8[ns]") - expected = Series([NaT]) + expected = Series([NaT], dtype="M8[ns]") tm.assert_series_equal(result, expected) def test_constructor_name_hashable(self): diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 508e03f617376..8139fe52c7037 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -625,7 +625,7 @@ def test_to_datetime_mixed_date_and_string(self, format): # https://github.com/pandas-dev/pandas/issues/50108 d1 = date(2020, 1, 2) res = to_datetime(["2020-01-01", d1], format=format) - expected = DatetimeIndex(["2020-01-01", "2020-01-02"]) + expected = DatetimeIndex(["2020-01-01", "2020-01-02"], dtype="M8[ns]") tm.assert_index_equal(res, expected) @pytest.mark.parametrize( @@ -1348,7 +1348,9 @@ def test_to_datetime_utc_true_with_series_tzaware_string(self, cache): ], ) def test_to_datetime_utc_true_with_series_datetime_ns(self, cache, date, dtype): - expected = Series([Timestamp("2013-01-01 01:00:00", tz="UTC")]) + expected = Series( + [Timestamp("2013-01-01 01:00:00", tz="UTC")], dtype="M8[ns, UTC]" + ) result = to_datetime(Series([date], dtype=dtype), utc=True, cache=cache) tm.assert_series_equal(result, expected) @@ -1853,7 +1855,7 @@ class TestToDatetimeUnit: def test_to_datetime_month_or_year_unit_int(self, cache, unit, item, request): # GH#50870 Note we have separate tests that pd.Timestamp gets these right ts = Timestamp(item, unit=unit) - expected = DatetimeIndex([ts]) + expected = DatetimeIndex([ts], dtype="M8[ns]") result = to_datetime([item], unit=unit, cache=cache) tm.assert_index_equal(result, expected) @@ -1929,7 +1931,8 @@ def test_unit_array_mixed_nans(self, cache): result = to_datetime(values, unit="D", errors="coerce", cache=cache) expected = DatetimeIndex( - ["NaT", "1970-01-02", "1970-01-02", "NaT", "NaT", "NaT", "NaT", "NaT"] + ["NaT", "1970-01-02", "1970-01-02", "NaT", "NaT", "NaT", "NaT", "NaT"], + dtype="M8[ns]", ) tm.assert_index_equal(result, expected) @@ -1972,7 +1975,9 @@ def test_unit_consistency(self, cache, error): def test_unit_with_numeric(self, cache, errors, dtype): # GH 13180 # coercions from floats/ints are ok - expected = DatetimeIndex(["2015-06-19 05:33:20", "2015-05-27 22:33:20"]) + expected = DatetimeIndex( + ["2015-06-19 05:33:20", "2015-05-27 22:33:20"], dtype="M8[ns]" + ) arr = np.array([1.434692e18, 1.432766e18]).astype(dtype) result = to_datetime(arr, errors=errors, cache=cache) tm.assert_index_equal(result, expected) @@ -1995,7 +2000,7 @@ def test_unit_with_numeric(self, cache, errors, dtype): def test_unit_with_numeric_coerce(self, cache, exp, arr, warning): # but we want to make sure that we are coercing # if we have ints/strings - expected = DatetimeIndex(exp) + expected = DatetimeIndex(exp, dtype="M8[ns]") with tm.assert_produces_warning(warning, match="Could not infer format"): result = to_datetime(arr, errors="coerce", cache=cache) tm.assert_index_equal(result, expected) @@ -2044,7 +2049,7 @@ def test_unit_ignore_keeps_name(self, cache): def test_to_datetime_errors_ignore_utc_true(self): # GH#23758 result = to_datetime([1], unit="s", utc=True, errors="ignore") - expected = DatetimeIndex(["1970-01-01 00:00:01"], tz="UTC") + expected = DatetimeIndex(["1970-01-01 00:00:01"], dtype="M8[ns, UTC]") tm.assert_index_equal(result, expected) @pytest.mark.parametrize("dtype", [int, float]) @@ -2064,7 +2069,8 @@ def test_to_datetime_unit_with_nulls(self, null): result = to_datetime(ser, unit="s") expected = Series( [Timestamp("2013-06-09 02:42:28") + timedelta(seconds=t) for t in range(20)] - + [NaT] + + [NaT], + dtype="M8[ns]", ) tm.assert_series_equal(result, expected) @@ -2078,7 +2084,8 @@ def test_to_datetime_unit_fractional_seconds(self): Timestamp("2013-06-09 02:42:28") + timedelta(seconds=t) for t in np.arange(0, 2, 0.25) ] - + [NaT] + + [NaT], + dtype="M8[ns]", ) # GH20455 argument will incur floating point errors but no premature rounding result = result.round("ms") @@ -2087,7 +2094,8 @@ def test_to_datetime_unit_fractional_seconds(self): def test_to_datetime_unit_na_values(self): result = to_datetime([1, 2, "NaT", NaT, np.nan], unit="D") expected = DatetimeIndex( - [Timestamp("1970-01-02"), Timestamp("1970-01-03")] + ["NaT"] * 3 + [Timestamp("1970-01-02"), Timestamp("1970-01-03")] + ["NaT"] * 3, + dtype="M8[ns]", ) tm.assert_index_equal(result, expected) @@ -2101,7 +2109,8 @@ def test_to_datetime_unit_invalid(self, bad_val): def test_to_timestamp_unit_coerce(self, bad_val): # coerce we can process expected = DatetimeIndex( - [Timestamp("1970-01-02"), Timestamp("1970-01-03")] + ["NaT"] * 1 + [Timestamp("1970-01-02"), Timestamp("1970-01-03")] + ["NaT"] * 1, + dtype="M8[ns]", ) result = to_datetime([1, 2, bad_val], unit="D", errors="coerce") tm.assert_index_equal(result, expected) diff --git a/pandas/tests/tseries/offsets/test_offsets.py b/pandas/tests/tseries/offsets/test_offsets.py index 97566750a5225..ddf56e68b1611 100644 --- a/pandas/tests/tseries/offsets/test_offsets.py +++ b/pandas/tests/tseries/offsets/test_offsets.py @@ -504,7 +504,7 @@ def test_add_empty_datetimeindex(self, offset_types, tz_naive_fixture): # GH#12724, GH#30336 offset_s = _create_offset(offset_types) - dti = DatetimeIndex([], tz=tz_naive_fixture) + dti = DatetimeIndex([], tz=tz_naive_fixture).as_unit("ns") warn = None if isinstance(