diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index 81cd504119c38..e74a8d6dcdc9a 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -1019,6 +1019,17 @@ def iat(x): # ----------------------------------------------------------------------------- +_UNITS = ["s", "ms", "us", "ns"] + + +def get_finest_unit(left: str, right: str): + """ + Find the higher of two datetime64 units. + """ + if _UNITS.index(left) >= _UNITS.index(right): + return left + return right + def shares_memory(left, right) -> bool: """ @@ -1121,6 +1132,7 @@ def shares_memory(left, right) -> bool: "getitem", "get_locales", "getMixedTypeDict", + "get_finest_unit", "get_obj", "get_op_from_name", "getPeriodData", diff --git a/pandas/conftest.py b/pandas/conftest.py index efe263a41afe1..35a0d3b89400f 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -1303,6 +1303,9 @@ def unit(request): return request.param +unit2 = unit + + # ---------------------------------------------------------------- # Dtypes # ---------------------------------------------------------------- diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 928b5c5a24479..72c97f4fab46d 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -1696,8 +1696,8 @@ def test_dt64_series_arith_overflow(self): tm.assert_series_equal(res, -expected) def test_datetimeindex_sub_timestamp_overflow(self): - dtimax = pd.to_datetime(["2021-12-28 17:19", Timestamp.max]) - dtimin = pd.to_datetime(["2021-12-28 17:19", Timestamp.min]) + dtimax = pd.to_datetime(["2021-12-28 17:19", Timestamp.max]).as_unit("ns") + dtimin = pd.to_datetime(["2021-12-28 17:19", Timestamp.min]).as_unit("ns") tsneg = Timestamp("1950-01-01").as_unit("ns") ts_neg_variants = [ @@ -1735,11 +1735,11 @@ def test_datetimeindex_sub_timestamp_overflow(self): def test_datetimeindex_sub_datetimeindex_overflow(self): # GH#22492, GH#22508 - dtimax = pd.to_datetime(["2021-12-28 17:19", Timestamp.max]) - dtimin = pd.to_datetime(["2021-12-28 17:19", Timestamp.min]) + dtimax = pd.to_datetime(["2021-12-28 17:19", Timestamp.max]).as_unit("ns") + dtimin = pd.to_datetime(["2021-12-28 17:19", Timestamp.min]).as_unit("ns") - ts_neg = pd.to_datetime(["1950-01-01", "1950-01-01"]) - ts_pos = pd.to_datetime(["1980-01-01", "1980-01-01"]) + ts_neg = pd.to_datetime(["1950-01-01", "1950-01-01"]).as_unit("ns") + ts_pos = pd.to_datetime(["1980-01-01", "1980-01-01"]).as_unit("ns") # General tests expected = Timestamp.max._value - ts_pos[1]._value @@ -1815,12 +1815,16 @@ def test_operators_datetimelike(self): td1 + dt1 dt1 + td1 - def test_dt64ser_sub_datetime_dtype(self): + def test_dt64ser_sub_datetime_dtype(self, unit): ts = Timestamp(datetime(1993, 1, 7, 13, 30, 00)) dt = datetime(1993, 6, 22, 13, 30) - ser = Series([ts]) - result = pd.to_timedelta(np.abs(ser - dt)) - assert result.dtype == "timedelta64[ns]" + ser = Series([ts], dtype=f"M8[{unit}]") + result = ser - dt + + # the expected unit is the max of `unit` and the unit imputed to `dt`, + # which is "us" + exp_unit = tm.get_finest_unit(unit, "us") + assert result.dtype == f"timedelta64[{exp_unit}]" # ------------------------------------------------------------- # TODO: This next block of tests came from tests.series.test_operators, @@ -1899,10 +1903,8 @@ def test_sub_datetime_compat(self, unit): # see GH#14088 ser = Series([datetime(2016, 8, 23, 12, tzinfo=pytz.utc), NaT]).dt.as_unit(unit) dt = datetime(2016, 8, 22, 12, tzinfo=pytz.utc) - exp_unit = unit - if unit in ["s", "ms"]: - # The datetime object has "us" so we upcast - exp_unit = "us" + # The datetime object has "us" so we upcast lower units + exp_unit = tm.get_finest_unit(unit, "us") exp = Series([Timedelta("1 days"), NaT]).dt.as_unit(exp_unit) result = ser - dt tm.assert_series_equal(result, exp) diff --git a/pandas/tests/arrays/test_datetimes.py b/pandas/tests/arrays/test_datetimes.py index 9b1562b24d11b..761bc81c3763c 100644 --- a/pandas/tests/arrays/test_datetimes.py +++ b/pandas/tests/arrays/test_datetimes.py @@ -15,10 +15,7 @@ import numpy as np import pytest -from pandas._libs.tslibs import ( - npy_unit_to_abbrev, - tz_compare, -) +from pandas._libs.tslibs import tz_compare from pandas.core.dtypes.dtypes import DatetimeTZDtype @@ -235,8 +232,7 @@ def test_add_timedeltalike_scalar_mismatched_reso(self, dta_dti, scalar): dta, dti = dta_dti td = pd.Timedelta(scalar) - exp_reso = max(dta._creso, td._creso) - exp_unit = npy_unit_to_abbrev(exp_reso) + exp_unit = tm.get_finest_unit(dta.unit, td.unit) expected = (dti + td)._data.as_unit(exp_unit) result = dta + scalar diff --git a/pandas/tests/frame/methods/test_reset_index.py b/pandas/tests/frame/methods/test_reset_index.py index 339e19254fd10..37a0d589cb3b7 100644 --- a/pandas/tests/frame/methods/test_reset_index.py +++ b/pandas/tests/frame/methods/test_reset_index.py @@ -76,19 +76,12 @@ def test_reset_index_tz(self, tz_aware_fixture): expected = DataFrame( { - "idx": [ - datetime(2011, 1, 1), - datetime(2011, 1, 2), - datetime(2011, 1, 3), - datetime(2011, 1, 4), - datetime(2011, 1, 5), - ], + "idx": idx, "a": range(5), "b": ["A", "B", "C", "D", "E"], }, columns=["idx", "a", "b"], ) - expected["idx"] = expected["idx"].apply(lambda d: Timestamp(d, tz=tz)) tm.assert_frame_equal(df.reset_index(), expected) @pytest.mark.parametrize("tz", ["US/Eastern", "dateutil/US/Eastern"]) diff --git a/pandas/tests/indexes/datetimes/methods/test_delete.py b/pandas/tests/indexes/datetimes/methods/test_delete.py index 620e41bf2d43a..0fdd60c14474e 100644 --- a/pandas/tests/indexes/datetimes/methods/test_delete.py +++ b/pandas/tests/indexes/datetimes/methods/test_delete.py @@ -9,19 +9,25 @@ class TestDelete: - def test_delete(self): - idx = date_range(start="2000-01-01", periods=5, freq="ME", name="idx") + def test_delete(self, unit): + idx = date_range( + start="2000-01-01", periods=5, freq="ME", name="idx", unit=unit + ) # preserve freq - expected_0 = date_range(start="2000-02-01", periods=4, freq="ME", name="idx") - expected_4 = date_range(start="2000-01-01", periods=4, freq="ME", name="idx") + expected_0 = date_range( + start="2000-02-01", periods=4, freq="ME", name="idx", unit=unit + ) + expected_4 = date_range( + start="2000-01-01", periods=4, freq="ME", name="idx", unit=unit + ) # reset freq to None expected_1 = DatetimeIndex( ["2000-01-31", "2000-03-31", "2000-04-30", "2000-05-31"], freq=None, name="idx", - ) + ).as_unit(unit) cases = { 0: expected_0, @@ -64,12 +70,18 @@ def test_delete2(self, tz): assert result.freqstr == "h" assert result.tz == expected.tz - def test_delete_slice(self): - idx = date_range(start="2000-01-01", periods=10, freq="D", name="idx") + def test_delete_slice(self, unit): + idx = date_range( + start="2000-01-01", periods=10, freq="D", name="idx", unit=unit + ) # preserve freq - expected_0_2 = date_range(start="2000-01-04", periods=7, freq="D", name="idx") - expected_7_9 = date_range(start="2000-01-01", periods=7, freq="D", name="idx") + expected_0_2 = date_range( + start="2000-01-04", periods=7, freq="D", name="idx", unit=unit + ) + expected_7_9 = date_range( + start="2000-01-01", periods=7, freq="D", name="idx", unit=unit + ) # reset freq to None expected_3_5 = DatetimeIndex( @@ -84,7 +96,7 @@ def test_delete_slice(self): ], freq=None, name="idx", - ) + ).as_unit(unit) cases = { (0, 1, 2): expected_0_2, diff --git a/pandas/tests/indexes/datetimes/methods/test_insert.py b/pandas/tests/indexes/datetimes/methods/test_insert.py index 4ef162913b622..ebfe490e0e067 100644 --- a/pandas/tests/indexes/datetimes/methods/test_insert.py +++ b/pandas/tests/indexes/datetimes/methods/test_insert.py @@ -52,13 +52,15 @@ def test_insert_empty_preserves_freq(self, tz_naive_fixture): result = dti.insert(0, item) assert result.freq is None - def test_insert(self): - idx = DatetimeIndex(["2000-01-04", "2000-01-01", "2000-01-02"], name="idx") + def test_insert(self, unit): + idx = DatetimeIndex( + ["2000-01-04", "2000-01-01", "2000-01-02"], name="idx" + ).as_unit(unit) result = idx.insert(2, datetime(2000, 1, 5)) exp = DatetimeIndex( ["2000-01-04", "2000-01-01", "2000-01-05", "2000-01-02"], name="idx" - ) + ).as_unit(unit) tm.assert_index_equal(result, exp) # insertion of non-datetime should coerce to object index @@ -76,31 +78,32 @@ def test_insert(self): tm.assert_index_equal(result, expected) assert result.name == expected.name - idx = date_range("1/1/2000", periods=3, freq="ME", name="idx") + def test_insert2(self, unit): + idx = date_range("1/1/2000", periods=3, freq="ME", name="idx", unit=unit) # preserve freq expected_0 = DatetimeIndex( ["1999-12-31", "2000-01-31", "2000-02-29", "2000-03-31"], name="idx", freq="ME", - ) + ).as_unit(unit) expected_3 = DatetimeIndex( ["2000-01-31", "2000-02-29", "2000-03-31", "2000-04-30"], name="idx", freq="ME", - ) + ).as_unit(unit) # reset freq to None expected_1_nofreq = DatetimeIndex( ["2000-01-31", "2000-01-31", "2000-02-29", "2000-03-31"], name="idx", freq=None, - ) + ).as_unit(unit) expected_3_nofreq = DatetimeIndex( ["2000-01-31", "2000-02-29", "2000-03-31", "2000-01-02"], name="idx", freq=None, - ) + ).as_unit(unit) cases = [ (0, datetime(1999, 12, 31), expected_0), @@ -116,22 +119,28 @@ def test_insert(self): assert result.name == expected.name assert result.freq == expected.freq + def test_insert3(self, unit): + idx = date_range("1/1/2000", periods=3, freq="ME", name="idx", unit=unit) + # reset freq to None result = idx.insert(3, datetime(2000, 1, 2)) expected = DatetimeIndex( ["2000-01-31", "2000-02-29", "2000-03-31", "2000-01-02"], name="idx", freq=None, - ) + ).as_unit(unit) tm.assert_index_equal(result, expected) assert result.name == expected.name assert result.freq is None + def test_insert4(self, unit): for tz in ["US/Pacific", "Asia/Singapore"]: - idx = date_range("1/1/2000 09:00", periods=6, freq="h", tz=tz, name="idx") + idx = date_range( + "1/1/2000 09:00", periods=6, freq="h", tz=tz, name="idx", unit=unit + ) # preserve freq expected = date_range( - "1/1/2000 09:00", periods=7, freq="h", tz=tz, name="idx" + "1/1/2000 09:00", periods=7, freq="h", tz=tz, name="idx", unit=unit ) for d in [ Timestamp("2000-01-01 15:00", tz=tz), @@ -156,7 +165,7 @@ def test_insert(self): name="idx", tz=tz, freq=None, - ) + ).as_unit(unit) # reset freq to None for d in [ Timestamp("2000-01-01 10:00", tz=tz), diff --git a/pandas/tests/indexes/datetimes/methods/test_repeat.py b/pandas/tests/indexes/datetimes/methods/test_repeat.py index c18109a23b6e8..cf4749aedd5a7 100644 --- a/pandas/tests/indexes/datetimes/methods/test_repeat.py +++ b/pandas/tests/indexes/datetimes/methods/test_repeat.py @@ -11,13 +11,14 @@ class TestRepeat: def test_repeat_range(self, tz_naive_fixture): - tz = tz_naive_fixture rng = date_range("1/1/2000", "1/1/2001") result = rng.repeat(5) assert result.freq is None assert len(result) == 5 * len(rng) + def test_repeat_range2(self, tz_naive_fixture): + tz = tz_naive_fixture index = date_range("2001-01-01", periods=2, freq="D", tz=tz) exp = DatetimeIndex( ["2001-01-01", "2001-01-01", "2001-01-02", "2001-01-02"], tz=tz @@ -26,6 +27,8 @@ def test_repeat_range(self, tz_naive_fixture): tm.assert_index_equal(res, exp) assert res.freq is None + def test_repeat_range3(self, tz_naive_fixture): + tz = tz_naive_fixture index = date_range("2001-01-01", periods=2, freq="2D", tz=tz) exp = DatetimeIndex( ["2001-01-01", "2001-01-01", "2001-01-03", "2001-01-03"], tz=tz @@ -34,6 +37,8 @@ def test_repeat_range(self, tz_naive_fixture): tm.assert_index_equal(res, exp) assert res.freq is None + def test_repeat_range4(self, tz_naive_fixture): + tz = tz_naive_fixture index = DatetimeIndex(["2001-01-01", "NaT", "2003-01-01"], tz=tz) exp = DatetimeIndex( [ diff --git a/pandas/tests/indexes/datetimes/methods/test_round.py b/pandas/tests/indexes/datetimes/methods/test_round.py index 6c9fb8ded0650..f9a3cdf2cf26e 100644 --- a/pandas/tests/indexes/datetimes/methods/test_round.py +++ b/pandas/tests/indexes/datetimes/methods/test_round.py @@ -71,6 +71,8 @@ def test_round(self, tz_naive_fixture): with pytest.raises(ValueError, match=msg): elt.round(freq="ME") + 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) result = index.round("ms") @@ -80,11 +82,14 @@ def test_round(self, tz_naive_fixture): for freq in ["us", "ns"]: tm.assert_index_equal(index, index.round(freq)) + def test_round3(self, tz_naive_fixture): + tz = tz_naive_fixture index = DatetimeIndex(["2016-10-17 12:00:00.00149"], tz=tz) result = index.round("ms") expected = DatetimeIndex(["2016-10-17 12:00:00.001000"], tz=tz) tm.assert_index_equal(result, expected) + def test_round4(self, tz_naive_fixture): index = DatetimeIndex(["2016-10-17 12:00:00.001501031"]) result = index.round("10ns") expected = DatetimeIndex(["2016-10-17 12:00:00.001501030"]) diff --git a/pandas/tests/indexes/datetimes/methods/test_tz_localize.py b/pandas/tests/indexes/datetimes/methods/test_tz_localize.py index ca71dde3e6c9e..9cd0d2df48ac8 100644 --- a/pandas/tests/indexes/datetimes/methods/test_tz_localize.py +++ b/pandas/tests/indexes/datetimes/methods/test_tz_localize.py @@ -26,6 +26,17 @@ ZoneInfo = None # type: ignore[misc, assignment] +easts = [pytz.timezone("US/Eastern"), gettz("US/Eastern")] +if ZoneInfo is not None: + try: + tz = ZoneInfo("US/Eastern") + except KeyError: + # no tzdata + pass + else: + easts.append(tz) + + class TestTZLocalize: def test_tz_localize_invalidates_freq(self): # we only preserve freq in unambiguous cases @@ -77,16 +88,6 @@ def test_dti_tz_localize_nonexistent_raise_coerce(self): expected = dti.tz_convert("US/Eastern") tm.assert_index_equal(result, expected) - easts = [pytz.timezone("US/Eastern"), gettz("US/Eastern")] - if ZoneInfo is not None: - try: - tz = ZoneInfo("US/Eastern") - except KeyError: - # no tzdata - pass - else: - easts.append(tz) - @pytest.mark.parametrize("tz", easts) def test_dti_tz_localize_ambiguous_infer(self, tz): # November 6, 2011, fall back, repeat 2 AM hour @@ -95,6 +96,8 @@ def test_dti_tz_localize_ambiguous_infer(self, tz): with pytest.raises(pytz.AmbiguousTimeError, match="Cannot infer dst time"): dr.tz_localize(tz) + @pytest.mark.parametrize("tz", easts) + def test_dti_tz_localize_ambiguous_infer2(self, tz): # With repeated hours, we can infer the transition dr = date_range(datetime(2011, 11, 6, 0), periods=5, freq=offsets.Hour(), tz=tz) times = [ @@ -105,18 +108,21 @@ def test_dti_tz_localize_ambiguous_infer(self, tz): "11/06/2011 03:00", ] di = DatetimeIndex(times) - localized = di.tz_localize(tz, ambiguous="infer") + result = di.tz_localize(tz, ambiguous="infer") expected = dr._with_freq(None) - tm.assert_index_equal(expected, localized) - tm.assert_index_equal(expected, DatetimeIndex(times, tz=tz, ambiguous="infer")) + tm.assert_index_equal(result, expected) + result2 = DatetimeIndex(times, tz=tz, ambiguous="infer") + tm.assert_index_equal(result2, expected) + @pytest.mark.parametrize("tz", easts) + def test_dti_tz_localize_ambiguous_infer3(self, tz): # When there is no dst transition, nothing special happens dr = date_range(datetime(2011, 6, 1, 0), periods=10, freq=offsets.Hour()) localized = dr.tz_localize(tz) localized_infer = dr.tz_localize(tz, ambiguous="infer") tm.assert_index_equal(localized, localized_infer) - @pytest.mark.parametrize("tz", [pytz.timezone("US/Eastern"), gettz("US/Eastern")]) + @pytest.mark.parametrize("tz", easts) def test_dti_tz_localize_ambiguous_times(self, tz): # March 13, 2011, spring forward, skip from 2 AM to 3 AM dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3, freq=offsets.Hour()) @@ -237,7 +243,7 @@ def test_dti_tz_localize_tzlocal(self): dti2 = dti.tz_localize(None) tm.assert_numpy_array_equal(dti2.asi8 - offset, dti.asi8) - @pytest.mark.parametrize("tz", [pytz.timezone("US/Eastern"), gettz("US/Eastern")]) + @pytest.mark.parametrize("tz", easts) def test_dti_tz_localize_ambiguous_nat(self, tz): times = [ "11/06/2011 00:00", @@ -262,7 +268,7 @@ def test_dti_tz_localize_ambiguous_nat(self, tz): # right is datetime64[ns, tzfile('/usr/share/zoneinfo/US/Eastern')] tm.assert_numpy_array_equal(di_test.values, localized.values) - @pytest.mark.parametrize("tz", [pytz.timezone("US/Eastern"), gettz("US/Eastern")]) + @pytest.mark.parametrize("tz", easts) def test_dti_tz_localize_ambiguous_flags(self, tz): # November 6, 2011, fall back, repeat 2 AM hour diff --git a/pandas/tests/indexes/datetimes/test_formats.py b/pandas/tests/indexes/datetimes/test_formats.py index 4bf8df986801b..b52eed8c509c6 100644 --- a/pandas/tests/indexes/datetimes/test_formats.py +++ b/pandas/tests/indexes/datetimes/test_formats.py @@ -14,6 +14,11 @@ import pandas._testing as tm +@pytest.fixture(params=["s", "ms", "us", "ns"]) +def unit(request): + return request.param + + def test_get_values_for_csv(): index = pd.date_range(freq="1D", periods=3, start="2017-01-01") @@ -116,14 +121,13 @@ def test_dti_repr_short(self): ), ], ) - def test_dti_repr_time_midnight(self, dates, freq, expected_repr): + def test_dti_repr_time_midnight(self, dates, freq, expected_repr, unit): # GH53634 - dti = DatetimeIndex(dates, freq) + dti = DatetimeIndex(dates, freq).as_unit(unit) actual_repr = repr(dti) - assert actual_repr == expected_repr + assert actual_repr == expected_repr.replace("[ns]", f"[{unit}]") - @pytest.mark.parametrize("method", ["__repr__", "__str__"]) - def test_dti_representation(self, method): + def test_dti_representation(self, unit): idxs = [] idxs.append(DatetimeIndex([], freq="D")) idxs.append(DatetimeIndex(["2011-01-01"], freq="D")) @@ -174,11 +178,16 @@ def test_dti_representation(self, method): ) with pd.option_context("display.width", 300): - for indx, expected in zip(idxs, exp): - result = getattr(indx, method)() + for index, expected in zip(idxs, exp): + index = index.as_unit(unit) + expected = expected.replace("[ns", f"[{unit}") + result = repr(index) + assert result == expected + result = str(index) assert result == expected - def test_dti_representation_to_series(self): + # TODO: this is a Series.__repr__ test + def test_dti_representation_to_series(self, unit): idx1 = DatetimeIndex([], freq="D") idx2 = DatetimeIndex(["2011-01-01"], freq="D") idx3 = DatetimeIndex(["2011-01-01", "2011-01-02"], freq="D") @@ -231,8 +240,9 @@ def test_dti_representation_to_series(self): [idx1, idx2, idx3, idx4, idx5, idx6, idx7], [exp1, exp2, exp3, exp4, exp5, exp6, exp7], ): - result = repr(Series(idx)) - assert result == expected + ser = Series(idx.as_unit(unit)) + result = repr(ser) + assert result == expected.replace("[ns", f"[{unit}") def test_dti_summary(self): # GH#9116 diff --git a/pandas/tests/reshape/concat/test_datetimes.py b/pandas/tests/reshape/concat/test_datetimes.py index fe8e243919d05..22e1d75255b3f 100644 --- a/pandas/tests/reshape/concat/test_datetimes.py +++ b/pandas/tests/reshape/concat/test_datetimes.py @@ -19,22 +19,6 @@ ) import pandas._testing as tm -UNITS = ["s", "ms", "us", "ns"] - - -@pytest.fixture(params=UNITS) -def unit(request): - return request.param - - -unit2 = unit - - -def _get_finer_unit(unit, unit2): - if UNITS.index(unit) >= UNITS.index(unit2): - return unit - return unit2 - class TestDatetimeConcat: def test_concat_datetime64_block(self): @@ -333,7 +317,7 @@ def test_concat_tz_series3(self, unit, unit2): second[0] = second[0].dt.tz_localize("UTC") result = concat([first, second]) - exp_unit = _get_finer_unit(unit, unit2) + exp_unit = tm.get_finest_unit(unit, unit2) assert result[0].dtype == f"datetime64[{exp_unit}, UTC]" def test_concat_tz_series4(self, unit, unit2): @@ -345,7 +329,7 @@ def test_concat_tz_series4(self, unit, unit2): second[0] = second[0].dt.tz_localize("Europe/London") result = concat([first, second]) - exp_unit = _get_finer_unit(unit, unit2) + exp_unit = tm.get_finest_unit(unit, unit2) assert result[0].dtype == f"datetime64[{exp_unit}, Europe/London]" def test_concat_tz_series5(self, unit, unit2): @@ -359,7 +343,7 @@ def test_concat_tz_series5(self, unit, unit2): second[0] = second[0].dt.tz_localize("Europe/London") result = concat([first, second]) - exp_unit = _get_finer_unit(unit, unit2) + exp_unit = tm.get_finest_unit(unit, unit2) assert result[0].dtype == f"datetime64[{exp_unit}, Europe/London]" def test_concat_tz_series6(self, unit, unit2): @@ -373,7 +357,7 @@ def test_concat_tz_series6(self, unit, unit2): second[0] = second[0].dt.tz_localize("Europe/London") result = concat([first, second]) - exp_unit = _get_finer_unit(unit, unit2) + exp_unit = tm.get_finest_unit(unit, unit2) assert result[0].dtype == f"datetime64[{exp_unit}, Europe/London]" def test_concat_tz_series_tzlocal(self): diff --git a/pandas/tests/series/methods/test_astype.py b/pandas/tests/series/methods/test_astype.py index a1a74f9986ada..7d4ad99deab38 100644 --- a/pandas/tests/series/methods/test_astype.py +++ b/pandas/tests/series/methods/test_astype.py @@ -220,8 +220,8 @@ def test_astype_dt64tz_to_str(self): ) tm.assert_series_equal(result, expected) - def test_astype_datetime(self): - ser = Series(iNaT, dtype="M8[ns]", index=range(5)) + def test_astype_datetime(self, unit): + ser = Series(iNaT, dtype=f"M8[{unit}]", index=range(5)) ser = ser.astype("O") assert ser.dtype == np.object_ @@ -231,10 +231,12 @@ def test_astype_datetime(self): ser = ser.astype("O") assert ser.dtype == np.object_ - ser = Series([datetime(2001, 1, 2, 0, 0) for i in range(3)]) + ser = Series( + [datetime(2001, 1, 2, 0, 0) for i in range(3)], dtype=f"M8[{unit}]" + ) ser[1] = np.nan - assert ser.dtype == "M8[ns]" + assert ser.dtype == f"M8[{unit}]" ser = ser.astype("O") assert ser.dtype == np.object_ diff --git a/pandas/tests/series/methods/test_combine_first.py b/pandas/tests/series/methods/test_combine_first.py index 89b6f9b01bc66..47659308cfcad 100644 --- a/pandas/tests/series/methods/test_combine_first.py +++ b/pandas/tests/series/methods/test_combine_first.py @@ -69,14 +69,14 @@ def test_combine_first(self): ser.index = ser.index.astype("O") tm.assert_series_equal(ser, result) - def test_combine_first_dt64(self): - s0 = to_datetime(Series(["2010", np.nan])) - s1 = to_datetime(Series([np.nan, "2011"])) + def test_combine_first_dt64(self, unit): + s0 = to_datetime(Series(["2010", np.nan])).dt.as_unit(unit) + s1 = to_datetime(Series([np.nan, "2011"])).dt.as_unit(unit) rs = s0.combine_first(s1) - xp = to_datetime(Series(["2010", "2011"])) + xp = to_datetime(Series(["2010", "2011"])).dt.as_unit(unit) tm.assert_series_equal(rs, xp) - s0 = to_datetime(Series(["2010", np.nan])) + s0 = to_datetime(Series(["2010", np.nan])).dt.as_unit(unit) s1 = Series([np.nan, "2011"]) rs = s0.combine_first(s1) diff --git a/pandas/tests/series/methods/test_convert_dtypes.py b/pandas/tests/series/methods/test_convert_dtypes.py index f621604faae4b..0cd39140938c4 100644 --- a/pandas/tests/series/methods/test_convert_dtypes.py +++ b/pandas/tests/series/methods/test_convert_dtypes.py @@ -3,6 +3,8 @@ import numpy as np import pytest +from pandas._libs import lib + import pandas as pd import pandas._testing as tm @@ -125,7 +127,25 @@ ), (["a", "b"], pd.CategoricalDtype(), pd.CategoricalDtype(), {}), ( - pd.to_datetime(["2020-01-14 10:00", "2020-01-15 11:11"]), + pd.to_datetime(["2020-01-14 10:00", "2020-01-15 11:11"]).as_unit("s"), + pd.DatetimeTZDtype(tz="UTC"), + pd.DatetimeTZDtype(tz="UTC"), + {}, + ), + ( + pd.to_datetime(["2020-01-14 10:00", "2020-01-15 11:11"]).as_unit("ms"), + pd.DatetimeTZDtype(tz="UTC"), + pd.DatetimeTZDtype(tz="UTC"), + {}, + ), + ( + pd.to_datetime(["2020-01-14 10:00", "2020-01-15 11:11"]).as_unit("us"), + pd.DatetimeTZDtype(tz="UTC"), + pd.DatetimeTZDtype(tz="UTC"), + {}, + ), + ( + pd.to_datetime(["2020-01-14 10:00", "2020-01-15 11:11"]).as_unit("ns"), pd.DatetimeTZDtype(tz="UTC"), pd.DatetimeTZDtype(tz="UTC"), {}, @@ -170,7 +190,7 @@ def test_convert_dtypes( data, maindtype, expected_default, expected_other = test_cases if ( hasattr(data, "dtype") - and data.dtype == "M8[ns]" + and lib.is_np_dtype(data.dtype, "M") and isinstance(maindtype, pd.DatetimeTZDtype) ): # this astype is deprecated in favor of tz_localize diff --git a/pandas/tests/series/methods/test_dropna.py b/pandas/tests/series/methods/test_dropna.py index 1a7c27929d405..d03fcac24003e 100644 --- a/pandas/tests/series/methods/test_dropna.py +++ b/pandas/tests/series/methods/test_dropna.py @@ -68,7 +68,7 @@ def test_dropna_period_dtype(self): tm.assert_series_equal(result, expected) - def test_datetime64_tz_dropna(self): + def test_datetime64_tz_dropna(self, unit): # DatetimeLikeBlock ser = Series( [ @@ -76,20 +76,23 @@ def test_datetime64_tz_dropna(self): NaT, Timestamp("2011-01-03 10:00"), NaT, - ] + ], + dtype=f"M8[{unit}]", ) result = ser.dropna() expected = Series( - [Timestamp("2011-01-01 10:00"), Timestamp("2011-01-03 10:00")], index=[0, 2] + [Timestamp("2011-01-01 10:00"), Timestamp("2011-01-03 10:00")], + index=[0, 2], + dtype=f"M8[{unit}]", ) tm.assert_series_equal(result, expected) # DatetimeTZBlock idx = DatetimeIndex( ["2011-01-01 10:00", NaT, "2011-01-03 10:00", NaT], tz="Asia/Tokyo" - ) + ).as_unit(unit) ser = Series(idx) - assert ser.dtype == "datetime64[ns, Asia/Tokyo]" + assert ser.dtype == f"datetime64[{unit}, Asia/Tokyo]" result = ser.dropna() expected = Series( [ @@ -97,8 +100,9 @@ def test_datetime64_tz_dropna(self): Timestamp("2011-01-03 10:00", tz="Asia/Tokyo"), ], index=[0, 2], + dtype=f"datetime64[{unit}, Asia/Tokyo]", ) - assert result.dtype == "datetime64[ns, Asia/Tokyo]" + assert result.dtype == f"datetime64[{unit}, Asia/Tokyo]" tm.assert_series_equal(result, expected) @pytest.mark.parametrize("val", [1, 1.5]) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 224f219abf512..ae07cb00211a2 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -98,10 +98,7 @@ def test_to_datetime_format(self, cache, index_or_series, format, expected): values = index_or_series(["1/1/2000", "1/2/2000", "1/3/2000"]) result = to_datetime(values, format=format, cache=cache) expected = index_or_series(expected) - if isinstance(expected, Series): - tm.assert_series_equal(result, expected) - else: - tm.assert_index_equal(result, expected) + tm.assert_equal(result, expected) @pytest.mark.parametrize( "arg, expected, format", diff --git a/pandas/tests/tseries/offsets/test_business_hour.py b/pandas/tests/tseries/offsets/test_business_hour.py index 2ecea6df16162..28e660f677e28 100644 --- a/pandas/tests/tseries/offsets/test_business_hour.py +++ b/pandas/tests/tseries/offsets/test_business_hour.py @@ -993,8 +993,7 @@ def test_bday_ignores_timedeltas(self, unit, td_unit): off = BDay(offset=td) t1 = idx + off - exp_reso = max(td._creso, idx._data._creso) - exp_unit = {7: "s", 8: "ms", 9: "us", 10: "ns"}[exp_reso] + exp_unit = tm.get_finest_unit(td.unit, idx.unit) expected = DatetimeIndex( [