diff --git a/pandas/tests/indexes/period/test_formats.py b/pandas/tests/indexes/period/test_formats.py index 723777fa826c5..888d814ac7eea 100644 --- a/pandas/tests/indexes/period/test_formats.py +++ b/pandas/tests/indexes/period/test_formats.py @@ -56,6 +56,15 @@ def test_get_values_for_csv(): class TestPeriodIndexRendering: + def test_format_empty(self): + # GH#35712 + empty_idx = PeriodIndex([], freq="Y") + msg = r"PeriodIndex\.format is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + assert empty_idx.format() == [] + with tm.assert_produces_warning(FutureWarning, match=msg): + assert empty_idx.format(name=True) == [""] + def test_frame_repr(self): df = pd.DataFrame({"A": [1, 2, 3]}, index=pd.date_range("2000", periods=3)) result = repr(df) diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index 84b8c55f3f0fa..9ac105ac5fc1b 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -272,15 +272,6 @@ def test_map(self): exp = Index([x.ordinal for x in index]) tm.assert_index_equal(result, exp) - def test_format_empty(self): - # GH35712 - empty_idx = PeriodIndex([], freq="Y") - msg = r"PeriodIndex\.format is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - assert empty_idx.format() == [] - with tm.assert_produces_warning(FutureWarning, match=msg): - assert empty_idx.format(name=True) == [""] - def test_period_index_frequency_ME_error_message(self): msg = "Invalid frequency: 2ME" diff --git a/pandas/tests/indexes/test_index_new.py b/pandas/tests/indexes/test_index_new.py index b6de73266dc34..acb0f85027da2 100644 --- a/pandas/tests/indexes/test_index_new.py +++ b/pandas/tests/indexes/test_index_new.py @@ -350,6 +350,14 @@ def test_constructor_dtypes_to_timedelta(self, cast_index, vals): index = Index(vals) assert isinstance(index, TimedeltaIndex) + def test_pass_timedeltaindex_to_index(self): + rng = timedelta_range("1 days", "10 days") + idx = Index(rng, dtype=object) + + expected = Index(rng.to_pytimedelta(), dtype=object) + + tm.assert_numpy_array_equal(idx.values, expected.values) + class TestIndexConstructorUnwrapping: # Test passing different arraylike values to pd.Index diff --git a/pandas/tests/indexes/timedeltas/methods/test_astype.py b/pandas/tests/indexes/timedeltas/methods/test_astype.py index 4f5ece61fc30c..6aeb1fc23dae9 100644 --- a/pandas/tests/indexes/timedeltas/methods/test_astype.py +++ b/pandas/tests/indexes/timedeltas/methods/test_astype.py @@ -12,6 +12,7 @@ timedelta_range, ) import pandas._testing as tm +from pandas.core.arrays import TimedeltaArray class TestTimedeltaIndex: @@ -95,6 +96,56 @@ def test_astype_timedelta64(self): tm.assert_index_equal(result, idx) assert result is idx + def test_astype_to_td64d_raises(self, index_or_series): + # We don't support "D" reso + scalar = Timedelta(days=31) + td = index_or_series( + [scalar, scalar, scalar + timedelta(minutes=5, seconds=3), NaT], + dtype="m8[ns]", + ) + msg = ( + r"Cannot convert from timedelta64\[ns\] to timedelta64\[D\]. " + "Supported resolutions are 's', 'ms', 'us', 'ns'" + ) + with pytest.raises(ValueError, match=msg): + td.astype("timedelta64[D]") + + def test_astype_ms_to_s(self, index_or_series): + scalar = Timedelta(days=31) + td = index_or_series( + [scalar, scalar, scalar + timedelta(minutes=5, seconds=3), NaT], + dtype="m8[ns]", + ) + + exp_values = np.asarray(td).astype("m8[s]") + exp_tda = TimedeltaArray._simple_new(exp_values, dtype=exp_values.dtype) + expected = index_or_series(exp_tda) + assert expected.dtype == "m8[s]" + result = td.astype("timedelta64[s]") + tm.assert_equal(result, expected) + + def test_astype_freq_conversion(self): + # pre-2.0 td64 astype converted to float64. now for supported units + # (s, ms, us, ns) this converts to the requested dtype. + # This matches TDA and Series + tdi = timedelta_range("1 Day", periods=30) + + res = tdi.astype("m8[s]") + exp_values = np.asarray(tdi).astype("m8[s]") + exp_tda = TimedeltaArray._simple_new( + exp_values, dtype=exp_values.dtype, freq=tdi.freq + ) + expected = Index(exp_tda) + assert expected.dtype == "m8[s]" + tm.assert_index_equal(res, expected) + + # check this matches Series and TimedeltaArray + res = tdi._data.astype("m8[s]") + tm.assert_equal(res, expected._values) + + res = tdi.to_series().astype("m8[s]") + tm.assert_equal(res._values, expected._values._with_freq(None)) + @pytest.mark.parametrize("dtype", [float, "datetime64", "datetime64[ns]"]) def test_astype_raises(self, dtype): # GH 13149, GH 13209 diff --git a/pandas/tests/indexes/timedeltas/test_arithmetic.py b/pandas/tests/indexes/timedeltas/test_arithmetic.py new file mode 100644 index 0000000000000..a431e10dc18ab --- /dev/null +++ b/pandas/tests/indexes/timedeltas/test_arithmetic.py @@ -0,0 +1,51 @@ +# Arithmetic tests for TimedeltaIndex are generally about the result's `freq` attribute. +# Other cases can be shared in tests.arithmetic.test_timedelta64 +import numpy as np + +from pandas import ( + NaT, + Timedelta, + timedelta_range, +) +import pandas._testing as tm + + +class TestTimedeltaIndexArithmetic: + def test_arithmetic_zero_freq(self): + # GH#51575 don't get a .freq with freq.n = 0 + tdi = timedelta_range(0, periods=100, freq="ns") + result = tdi / 2 + assert result.freq is None + expected = tdi[:50].repeat(2) + tm.assert_index_equal(result, expected) + + result2 = tdi // 2 + assert result2.freq is None + expected2 = expected + tm.assert_index_equal(result2, expected2) + + result3 = tdi * 0 + assert result3.freq is None + expected3 = tdi[:1].repeat(100) + tm.assert_index_equal(result3, expected3) + + def test_tdi_division(self, index_or_series): + # doc example + + scalar = Timedelta(days=31) + td = index_or_series( + [scalar, scalar, scalar + Timedelta(minutes=5, seconds=3), NaT], + dtype="m8[ns]", + ) + + result = td / np.timedelta64(1, "D") + expected = index_or_series( + [31, 31, (31 * 86400 + 5 * 60 + 3) / 86400.0, np.nan] + ) + tm.assert_equal(result, expected) + + result = td / np.timedelta64(1, "s") + expected = index_or_series( + [31 * 86400, 31 * 86400, 31 * 86400 + 5 * 60 + 3, np.nan] + ) + tm.assert_equal(result, expected) diff --git a/pandas/tests/indexes/timedeltas/test_timedelta.py b/pandas/tests/indexes/timedeltas/test_timedelta.py index 6af4382912a01..3120066741ffa 100644 --- a/pandas/tests/indexes/timedeltas/test_timedelta.py +++ b/pandas/tests/indexes/timedeltas/test_timedelta.py @@ -1,24 +1,16 @@ -from datetime import timedelta - import numpy as np import pytest from pandas import ( Index, - NaT, Series, Timedelta, timedelta_range, ) import pandas._testing as tm -from pandas.core.arrays import TimedeltaArray class TestTimedeltaIndex: - @pytest.fixture - def index(self): - return tm.makeTimedeltaIndex(10) - def test_misc_coverage(self): rng = timedelta_range("1 day", periods=5) result = rng.groupby(rng.days) @@ -34,14 +26,6 @@ def test_map(self): exp = Index([f(x) for x in rng], dtype=np.int64) tm.assert_index_equal(result, exp) - def test_pass_TimedeltaIndex_to_index(self): - rng = timedelta_range("1 days", "10 days") - idx = Index(rng, dtype=object) - - expected = Index(rng.to_pytimedelta(), dtype=object) - - tm.assert_numpy_array_equal(idx.values, expected.values) - def test_fields(self): rng = timedelta_range("1 days, 10:11:12.100123456", periods=2, freq="s") tm.assert_index_equal(rng.days, Index([1, 1], dtype=np.int64)) @@ -75,80 +59,3 @@ def test_fields(self): # preserve name (GH15589) rng.name = "name" assert rng.days.name == "name" - - def test_freq_conversion_always_floating(self): - # pre-2.0 td64 astype converted to float64. now for supported units - # (s, ms, us, ns) this converts to the requested dtype. - # This matches TDA and Series - tdi = timedelta_range("1 Day", periods=30) - - res = tdi.astype("m8[s]") - exp_values = np.asarray(tdi).astype("m8[s]") - exp_tda = TimedeltaArray._simple_new( - exp_values, dtype=exp_values.dtype, freq=tdi.freq - ) - expected = Index(exp_tda) - assert expected.dtype == "m8[s]" - tm.assert_index_equal(res, expected) - - # check this matches Series and TimedeltaArray - res = tdi._data.astype("m8[s]") - tm.assert_equal(res, expected._values) - - res = tdi.to_series().astype("m8[s]") - tm.assert_equal(res._values, expected._values._with_freq(None)) - - def test_freq_conversion(self, index_or_series): - # doc example - - scalar = Timedelta(days=31) - td = index_or_series( - [scalar, scalar, scalar + timedelta(minutes=5, seconds=3), NaT], - dtype="m8[ns]", - ) - - result = td / np.timedelta64(1, "D") - expected = index_or_series( - [31, 31, (31 * 86400 + 5 * 60 + 3) / 86400.0, np.nan] - ) - tm.assert_equal(result, expected) - - # We don't support "D" reso, so we use the pre-2.0 behavior - # casting to float64 - msg = ( - r"Cannot convert from timedelta64\[ns\] to timedelta64\[D\]. " - "Supported resolutions are 's', 'ms', 'us', 'ns'" - ) - with pytest.raises(ValueError, match=msg): - td.astype("timedelta64[D]") - - result = td / np.timedelta64(1, "s") - expected = index_or_series( - [31 * 86400, 31 * 86400, 31 * 86400 + 5 * 60 + 3, np.nan] - ) - tm.assert_equal(result, expected) - - exp_values = np.asarray(td).astype("m8[s]") - exp_tda = TimedeltaArray._simple_new(exp_values, dtype=exp_values.dtype) - expected = index_or_series(exp_tda) - assert expected.dtype == "m8[s]" - result = td.astype("timedelta64[s]") - tm.assert_equal(result, expected) - - def test_arithmetic_zero_freq(self): - # GH#51575 don't get a .freq with freq.n = 0 - tdi = timedelta_range(0, periods=100, freq="ns") - result = tdi / 2 - assert result.freq is None - expected = tdi[:50].repeat(2) - tm.assert_index_equal(result, expected) - - result2 = tdi // 2 - assert result2.freq is None - expected2 = expected - tm.assert_index_equal(result2, expected2) - - result3 = tdi * 0 - assert result3.freq is None - expected3 = tdi[:1].repeat(100) - tm.assert_index_equal(result3, expected3) diff --git a/pandas/tests/scalar/timedelta/methods/__init__.py b/pandas/tests/scalar/timedelta/methods/__init__.py new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/pandas/tests/scalar/timedelta/methods/test_as_unit.py b/pandas/tests/scalar/timedelta/methods/test_as_unit.py new file mode 100644 index 0000000000000..8660141e5a537 --- /dev/null +++ b/pandas/tests/scalar/timedelta/methods/test_as_unit.py @@ -0,0 +1,80 @@ +import pytest + +from pandas._libs.tslibs.dtypes import NpyDatetimeUnit +from pandas.errors import OutOfBoundsTimedelta + +from pandas import Timedelta + + +class TestAsUnit: + def test_as_unit(self): + td = Timedelta(days=1) + + assert td.as_unit("ns") is td + + res = td.as_unit("us") + assert res._value == td._value // 1000 + assert res._creso == NpyDatetimeUnit.NPY_FR_us.value + + rt = res.as_unit("ns") + assert rt._value == td._value + assert rt._creso == td._creso + + res = td.as_unit("ms") + assert res._value == td._value // 1_000_000 + assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value + + rt = res.as_unit("ns") + assert rt._value == td._value + assert rt._creso == td._creso + + res = td.as_unit("s") + assert res._value == td._value // 1_000_000_000 + assert res._creso == NpyDatetimeUnit.NPY_FR_s.value + + rt = res.as_unit("ns") + assert rt._value == td._value + assert rt._creso == td._creso + + def test_as_unit_overflows(self): + # microsecond that would be just out of bounds for nano + us = 9223372800000000 + td = Timedelta._from_value_and_reso(us, NpyDatetimeUnit.NPY_FR_us.value) + + msg = "Cannot cast 106752 days 00:00:00 to unit='ns' without overflow" + with pytest.raises(OutOfBoundsTimedelta, match=msg): + td.as_unit("ns") + + res = td.as_unit("ms") + assert res._value == us // 1000 + assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value + + def test_as_unit_rounding(self): + td = Timedelta(microseconds=1500) + res = td.as_unit("ms") + + expected = Timedelta(milliseconds=1) + assert res == expected + + assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value + assert res._value == 1 + + with pytest.raises(ValueError, match="Cannot losslessly convert units"): + td.as_unit("ms", round_ok=False) + + def test_as_unit_non_nano(self): + # case where we are going neither to nor from nano + td = Timedelta(days=1).as_unit("ms") + assert td.days == 1 + assert td._value == 86_400_000 + assert td.components.days == 1 + assert td._d == 1 + assert td.total_seconds() == 86400 + + res = td.as_unit("us") + assert res._value == 86_400_000_000 + assert res.components.days == 1 + assert res.components.hours == 0 + assert res._d == 1 + assert res._h == 0 + assert res.total_seconds() == 86400 diff --git a/pandas/tests/scalar/timedelta/methods/test_round.py b/pandas/tests/scalar/timedelta/methods/test_round.py new file mode 100644 index 0000000000000..c68dfc7a16719 --- /dev/null +++ b/pandas/tests/scalar/timedelta/methods/test_round.py @@ -0,0 +1,185 @@ +from hypothesis import ( + given, + strategies as st, +) +import numpy as np +import pytest + +from pandas._libs import lib +from pandas._libs.tslibs import iNaT +from pandas.errors import OutOfBoundsTimedelta + +from pandas import Timedelta + + +class TestTimedeltaRound: + @pytest.mark.parametrize( + "freq,s1,s2", + [ + # This first case has s1, s2 being the same as t1,t2 below + ( + "ns", + Timedelta("1 days 02:34:56.789123456"), + Timedelta("-1 days 02:34:56.789123456"), + ), + ( + "us", + Timedelta("1 days 02:34:56.789123000"), + Timedelta("-1 days 02:34:56.789123000"), + ), + ( + "ms", + Timedelta("1 days 02:34:56.789000000"), + Timedelta("-1 days 02:34:56.789000000"), + ), + ("s", Timedelta("1 days 02:34:57"), Timedelta("-1 days 02:34:57")), + ("2s", Timedelta("1 days 02:34:56"), Timedelta("-1 days 02:34:56")), + ("5s", Timedelta("1 days 02:34:55"), Timedelta("-1 days 02:34:55")), + ("min", Timedelta("1 days 02:35:00"), Timedelta("-1 days 02:35:00")), + ("12min", Timedelta("1 days 02:36:00"), Timedelta("-1 days 02:36:00")), + ("h", Timedelta("1 days 03:00:00"), Timedelta("-1 days 03:00:00")), + ("d", Timedelta("1 days"), Timedelta("-1 days")), + ], + ) + def test_round(self, freq, s1, s2): + t1 = Timedelta("1 days 02:34:56.789123456") + t2 = Timedelta("-1 days 02:34:56.789123456") + + r1 = t1.round(freq) + assert r1 == s1 + r2 = t2.round(freq) + assert r2 == s2 + + def test_round_invalid(self): + t1 = Timedelta("1 days 02:34:56.789123456") + + for freq, msg in [ + ("Y", " is a non-fixed frequency"), + ("ME", " is a non-fixed frequency"), + ("foobar", "Invalid frequency: foobar"), + ]: + with pytest.raises(ValueError, match=msg): + t1.round(freq) + + def test_round_implementation_bounds(self): + # See also: analogous test for Timestamp + # GH#38964 + result = Timedelta.min.ceil("s") + expected = Timedelta.min + Timedelta(seconds=1) - Timedelta(145224193) + assert result == expected + + result = Timedelta.max.floor("s") + expected = Timedelta.max - Timedelta(854775807) + assert result == expected + + msg = ( + r"Cannot round -106752 days \+00:12:43.145224193 to freq=s without overflow" + ) + with pytest.raises(OutOfBoundsTimedelta, match=msg): + Timedelta.min.floor("s") + with pytest.raises(OutOfBoundsTimedelta, match=msg): + Timedelta.min.round("s") + + msg = "Cannot round 106751 days 23:47:16.854775807 to freq=s without overflow" + with pytest.raises(OutOfBoundsTimedelta, match=msg): + Timedelta.max.ceil("s") + with pytest.raises(OutOfBoundsTimedelta, match=msg): + Timedelta.max.round("s") + + @given(val=st.integers(min_value=iNaT + 1, max_value=lib.i8max)) + @pytest.mark.parametrize( + "method", [Timedelta.round, Timedelta.floor, Timedelta.ceil] + ) + def test_round_sanity(self, val, method): + cls = Timedelta + err_cls = OutOfBoundsTimedelta + + val = np.int64(val) + td = cls(val) + + def checker(ts, nanos, unit): + # First check that we do raise in cases where we should + if nanos == 1: + pass + else: + div, mod = divmod(ts._value, nanos) + diff = int(nanos - mod) + lb = ts._value - mod + assert lb <= ts._value # i.e. no overflows with python ints + ub = ts._value + diff + assert ub > ts._value # i.e. no overflows with python ints + + msg = "without overflow" + if mod == 0: + # We should never be raising in this + pass + elif method is cls.ceil: + if ub > cls.max._value: + with pytest.raises(err_cls, match=msg): + method(ts, unit) + return + elif method is cls.floor: + if lb < cls.min._value: + with pytest.raises(err_cls, match=msg): + method(ts, unit) + return + elif mod >= diff: + if ub > cls.max._value: + with pytest.raises(err_cls, match=msg): + method(ts, unit) + return + elif lb < cls.min._value: + with pytest.raises(err_cls, match=msg): + method(ts, unit) + return + + res = method(ts, unit) + + td = res - ts + diff = abs(td._value) + assert diff < nanos + assert res._value % nanos == 0 + + if method is cls.round: + assert diff <= nanos / 2 + elif method is cls.floor: + assert res <= ts + elif method is cls.ceil: + assert res >= ts + + nanos = 1 + checker(td, nanos, "ns") + + nanos = 1000 + checker(td, nanos, "us") + + nanos = 1_000_000 + checker(td, nanos, "ms") + + nanos = 1_000_000_000 + checker(td, nanos, "s") + + nanos = 60 * 1_000_000_000 + checker(td, nanos, "min") + + nanos = 60 * 60 * 1_000_000_000 + checker(td, nanos, "h") + + nanos = 24 * 60 * 60 * 1_000_000_000 + checker(td, nanos, "D") + + @pytest.mark.parametrize("unit", ["ns", "us", "ms", "s"]) + def test_round_non_nano(self, unit): + td = Timedelta("1 days 02:34:57").as_unit(unit) + + res = td.round("min") + assert res == Timedelta("1 days 02:35:00") + assert res._creso == td._creso + + res = td.floor("min") + assert res == Timedelta("1 days 02:34:00") + assert res._creso == td._creso + + res = td.ceil("min") + assert res == Timedelta("1 days 02:35:00") + assert res._creso == td._creso diff --git a/pandas/tests/scalar/timedelta/test_constructors.py b/pandas/tests/scalar/timedelta/test_constructors.py index 858ab29d79c5e..e45e0ac3c6a45 100644 --- a/pandas/tests/scalar/timedelta/test_constructors.py +++ b/pandas/tests/scalar/timedelta/test_constructors.py @@ -8,11 +8,166 @@ from pandas._libs.tslibs.dtypes import NpyDatetimeUnit from pandas import ( + Index, NaT, Timedelta, + TimedeltaIndex, offsets, to_timedelta, ) +import pandas._testing as tm + + +class TestTimedeltaConstructorUnitKeyword: + @pytest.mark.parametrize("unit", ["Y", "y", "M"]) + def test_unit_m_y_raises(self, unit): + msg = "Units 'M', 'Y', and 'y' are no longer supported" + + with pytest.raises(ValueError, match=msg): + Timedelta(10, unit) + + with pytest.raises(ValueError, match=msg): + to_timedelta(10, unit) + + with pytest.raises(ValueError, match=msg): + to_timedelta([1, 2], unit) + + @pytest.mark.parametrize( + "unit,unit_depr", + [ + ("h", "H"), + ("min", "T"), + ("s", "S"), + ("ms", "L"), + ("ns", "N"), + ("us", "U"), + ], + ) + def test_units_H_T_S_L_N_U_deprecated(self, unit, unit_depr): + # GH#52536 + msg = f"'{unit_depr}' is deprecated and will be removed in a future version." + + expected = Timedelta(1, unit=unit) + with tm.assert_produces_warning(FutureWarning, match=msg): + result = Timedelta(1, unit=unit_depr) + tm.assert_equal(result, expected) + + @pytest.mark.parametrize( + "unit, np_unit", + [(value, "W") for value in ["W", "w"]] + + [(value, "D") for value in ["D", "d", "days", "day", "Days", "Day"]] + + [ + (value, "m") + for value in [ + "m", + "minute", + "min", + "minutes", + "Minute", + "Min", + "Minutes", + ] + ] + + [ + (value, "s") + for value in [ + "s", + "seconds", + "sec", + "second", + "Seconds", + "Sec", + "Second", + ] + ] + + [ + (value, "ms") + for value in [ + "ms", + "milliseconds", + "millisecond", + "milli", + "millis", + "MS", + "Milliseconds", + "Millisecond", + "Milli", + "Millis", + ] + ] + + [ + (value, "us") + for value in [ + "us", + "microseconds", + "microsecond", + "micro", + "micros", + "u", + "US", + "Microseconds", + "Microsecond", + "Micro", + "Micros", + "U", + ] + ] + + [ + (value, "ns") + for value in [ + "ns", + "nanoseconds", + "nanosecond", + "nano", + "nanos", + "n", + "NS", + "Nanoseconds", + "Nanosecond", + "Nano", + "Nanos", + "N", + ] + ], + ) + @pytest.mark.parametrize("wrapper", [np.array, list, Index]) + def test_unit_parser(self, unit, np_unit, wrapper): + # validate all units, GH 6855, GH 21762 + # array-likes + expected = TimedeltaIndex( + [np.timedelta64(i, np_unit) for i in np.arange(5).tolist()], + dtype="m8[ns]", + ) + # TODO(2.0): the desired output dtype may have non-nano resolution + msg = f"'{unit}' is deprecated and will be removed in a future version." + + if (unit, np_unit) in (("u", "us"), ("U", "us"), ("n", "ns"), ("N", "ns")): + warn = FutureWarning + else: + warn = None + with tm.assert_produces_warning(warn, match=msg): + result = to_timedelta(wrapper(range(5)), unit=unit) + tm.assert_index_equal(result, expected) + result = TimedeltaIndex(wrapper(range(5)), unit=unit) + tm.assert_index_equal(result, expected) + + str_repr = [f"{x}{unit}" for x in np.arange(5)] + result = to_timedelta(wrapper(str_repr)) + tm.assert_index_equal(result, expected) + result = to_timedelta(wrapper(str_repr)) + tm.assert_index_equal(result, expected) + + # scalar + expected = Timedelta(np.timedelta64(2, np_unit).astype("timedelta64[ns]")) + result = to_timedelta(2, unit=unit) + assert result == expected + result = Timedelta(2, unit=unit) + assert result == expected + + result = to_timedelta(f"2{unit}") + assert result == expected + result = Timedelta(f"2{unit}") + assert result == expected def test_construct_from_kwargs_overflow(): diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index ef780a090c12d..ac605df935226 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -17,89 +17,13 @@ from pandas._libs.tslibs.dtypes import NpyDatetimeUnit from pandas.errors import OutOfBoundsTimedelta -import pandas as pd from pandas import ( Timedelta, - TimedeltaIndex, to_timedelta, ) import pandas._testing as tm -class TestAsUnit: - def test_as_unit(self): - td = Timedelta(days=1) - - assert td.as_unit("ns") is td - - res = td.as_unit("us") - assert res._value == td._value // 1000 - assert res._creso == NpyDatetimeUnit.NPY_FR_us.value - - rt = res.as_unit("ns") - assert rt._value == td._value - assert rt._creso == td._creso - - res = td.as_unit("ms") - assert res._value == td._value // 1_000_000 - assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value - - rt = res.as_unit("ns") - assert rt._value == td._value - assert rt._creso == td._creso - - res = td.as_unit("s") - assert res._value == td._value // 1_000_000_000 - assert res._creso == NpyDatetimeUnit.NPY_FR_s.value - - rt = res.as_unit("ns") - assert rt._value == td._value - assert rt._creso == td._creso - - def test_as_unit_overflows(self): - # microsecond that would be just out of bounds for nano - us = 9223372800000000 - td = Timedelta._from_value_and_reso(us, NpyDatetimeUnit.NPY_FR_us.value) - - msg = "Cannot cast 106752 days 00:00:00 to unit='ns' without overflow" - with pytest.raises(OutOfBoundsTimedelta, match=msg): - td.as_unit("ns") - - res = td.as_unit("ms") - assert res._value == us // 1000 - assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value - - def test_as_unit_rounding(self): - td = Timedelta(microseconds=1500) - res = td.as_unit("ms") - - expected = Timedelta(milliseconds=1) - assert res == expected - - assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value - assert res._value == 1 - - with pytest.raises(ValueError, match="Cannot losslessly convert units"): - td.as_unit("ms", round_ok=False) - - def test_as_unit_non_nano(self): - # case where we are going neither to nor from nano - td = Timedelta(days=1).as_unit("ms") - assert td.days == 1 - assert td._value == 86_400_000 - assert td.components.days == 1 - assert td._d == 1 - assert td.total_seconds() == 86400 - - res = td.as_unit("us") - assert res._value == 86_400_000_000 - assert res.components.days == 1 - assert res.components.hours == 0 - assert res._d == 1 - assert res._h == 0 - assert res.total_seconds() == 86400 - - class TestNonNano: @pytest.fixture(params=["s", "ms", "us"]) def unit_str(self, request): @@ -474,11 +398,13 @@ def check(value): assert tup.microseconds == 999 assert tup.nanoseconds == 0 + # TODO: this is a test of to_timedelta string parsing def test_iso_conversion(self): # GH #21877 expected = Timedelta(1, unit="s") assert to_timedelta("P0DT0H0M1S") == expected + # TODO: this is a test of to_timedelta returning NaT def test_nat_converters(self): result = to_timedelta("nat").to_numpy() assert result.dtype.kind == "M" @@ -488,136 +414,6 @@ def test_nat_converters(self): assert result.dtype.kind == "M" assert result.astype("int64") == iNaT - @pytest.mark.parametrize( - "unit, np_unit", - [(value, "W") for value in ["W", "w"]] - + [(value, "D") for value in ["D", "d", "days", "day", "Days", "Day"]] - + [ - (value, "m") - for value in [ - "m", - "minute", - "min", - "minutes", - "Minute", - "Min", - "Minutes", - ] - ] - + [ - (value, "s") - for value in [ - "s", - "seconds", - "sec", - "second", - "Seconds", - "Sec", - "Second", - ] - ] - + [ - (value, "ms") - for value in [ - "ms", - "milliseconds", - "millisecond", - "milli", - "millis", - "MS", - "Milliseconds", - "Millisecond", - "Milli", - "Millis", - ] - ] - + [ - (value, "us") - for value in [ - "us", - "microseconds", - "microsecond", - "micro", - "micros", - "u", - "US", - "Microseconds", - "Microsecond", - "Micro", - "Micros", - "U", - ] - ] - + [ - (value, "ns") - for value in [ - "ns", - "nanoseconds", - "nanosecond", - "nano", - "nanos", - "n", - "NS", - "Nanoseconds", - "Nanosecond", - "Nano", - "Nanos", - "N", - ] - ], - ) - @pytest.mark.parametrize("wrapper", [np.array, list, pd.Index]) - def test_unit_parser(self, unit, np_unit, wrapper): - # validate all units, GH 6855, GH 21762 - # array-likes - expected = TimedeltaIndex( - [np.timedelta64(i, np_unit) for i in np.arange(5).tolist()], - dtype="m8[ns]", - ) - # TODO(2.0): the desired output dtype may have non-nano resolution - msg = f"'{unit}' is deprecated and will be removed in a future version." - - if (unit, np_unit) in (("u", "us"), ("U", "us"), ("n", "ns"), ("N", "ns")): - warn = FutureWarning - else: - warn = None - with tm.assert_produces_warning(warn, match=msg): - result = to_timedelta(wrapper(range(5)), unit=unit) - tm.assert_index_equal(result, expected) - result = TimedeltaIndex(wrapper(range(5)), unit=unit) - tm.assert_index_equal(result, expected) - - str_repr = [f"{x}{unit}" for x in np.arange(5)] - result = to_timedelta(wrapper(str_repr)) - tm.assert_index_equal(result, expected) - result = to_timedelta(wrapper(str_repr)) - tm.assert_index_equal(result, expected) - - # scalar - expected = Timedelta(np.timedelta64(2, np_unit).astype("timedelta64[ns]")) - result = to_timedelta(2, unit=unit) - assert result == expected - result = Timedelta(2, unit=unit) - assert result == expected - - result = to_timedelta(f"2{unit}") - assert result == expected - result = Timedelta(f"2{unit}") - assert result == expected - - @pytest.mark.parametrize("unit", ["Y", "y", "M"]) - def test_unit_m_y_raises(self, unit): - msg = "Units 'M', 'Y', and 'y' are no longer supported" - - with pytest.raises(ValueError, match=msg): - Timedelta(10, unit) - - with pytest.raises(ValueError, match=msg): - to_timedelta(10, unit) - - with pytest.raises(ValueError, match=msg): - to_timedelta([1, 2], unit) - def test_numeric_conversions(self): assert Timedelta(0) == np.timedelta64(0, "ns") assert Timedelta(10) == np.timedelta64(10, "ns") @@ -649,177 +445,6 @@ def test_to_numpy_alias(self): with pytest.raises(ValueError, match=msg): td.to_numpy(copy=True) - @pytest.mark.parametrize( - "freq,s1,s2", - [ - # This first case has s1, s2 being the same as t1,t2 below - ( - "ns", - Timedelta("1 days 02:34:56.789123456"), - Timedelta("-1 days 02:34:56.789123456"), - ), - ( - "us", - Timedelta("1 days 02:34:56.789123000"), - Timedelta("-1 days 02:34:56.789123000"), - ), - ( - "ms", - Timedelta("1 days 02:34:56.789000000"), - Timedelta("-1 days 02:34:56.789000000"), - ), - ("s", Timedelta("1 days 02:34:57"), Timedelta("-1 days 02:34:57")), - ("2s", Timedelta("1 days 02:34:56"), Timedelta("-1 days 02:34:56")), - ("5s", Timedelta("1 days 02:34:55"), Timedelta("-1 days 02:34:55")), - ("min", Timedelta("1 days 02:35:00"), Timedelta("-1 days 02:35:00")), - ("12min", Timedelta("1 days 02:36:00"), Timedelta("-1 days 02:36:00")), - ("h", Timedelta("1 days 03:00:00"), Timedelta("-1 days 03:00:00")), - ("d", Timedelta("1 days"), Timedelta("-1 days")), - ], - ) - def test_round(self, freq, s1, s2): - t1 = Timedelta("1 days 02:34:56.789123456") - t2 = Timedelta("-1 days 02:34:56.789123456") - - r1 = t1.round(freq) - assert r1 == s1 - r2 = t2.round(freq) - assert r2 == s2 - - def test_round_invalid(self): - t1 = Timedelta("1 days 02:34:56.789123456") - - for freq, msg in [ - ("Y", " is a non-fixed frequency"), - ("ME", " is a non-fixed frequency"), - ("foobar", "Invalid frequency: foobar"), - ]: - with pytest.raises(ValueError, match=msg): - t1.round(freq) - - def test_round_implementation_bounds(self): - # See also: analogous test for Timestamp - # GH#38964 - result = Timedelta.min.ceil("s") - expected = Timedelta.min + Timedelta(seconds=1) - Timedelta(145224193) - assert result == expected - - result = Timedelta.max.floor("s") - expected = Timedelta.max - Timedelta(854775807) - assert result == expected - - msg = ( - r"Cannot round -106752 days \+00:12:43.145224193 to freq=s without overflow" - ) - with pytest.raises(OutOfBoundsTimedelta, match=msg): - Timedelta.min.floor("s") - with pytest.raises(OutOfBoundsTimedelta, match=msg): - Timedelta.min.round("s") - - msg = "Cannot round 106751 days 23:47:16.854775807 to freq=s without overflow" - with pytest.raises(OutOfBoundsTimedelta, match=msg): - Timedelta.max.ceil("s") - with pytest.raises(OutOfBoundsTimedelta, match=msg): - Timedelta.max.round("s") - - @given(val=st.integers(min_value=iNaT + 1, max_value=lib.i8max)) - @pytest.mark.parametrize( - "method", [Timedelta.round, Timedelta.floor, Timedelta.ceil] - ) - def test_round_sanity(self, val, method): - cls = Timedelta - err_cls = OutOfBoundsTimedelta - - val = np.int64(val) - td = cls(val) - - def checker(ts, nanos, unit): - # First check that we do raise in cases where we should - if nanos == 1: - pass - else: - div, mod = divmod(ts._value, nanos) - diff = int(nanos - mod) - lb = ts._value - mod - assert lb <= ts._value # i.e. no overflows with python ints - ub = ts._value + diff - assert ub > ts._value # i.e. no overflows with python ints - - msg = "without overflow" - if mod == 0: - # We should never be raising in this - pass - elif method is cls.ceil: - if ub > cls.max._value: - with pytest.raises(err_cls, match=msg): - method(ts, unit) - return - elif method is cls.floor: - if lb < cls.min._value: - with pytest.raises(err_cls, match=msg): - method(ts, unit) - return - elif mod >= diff: - if ub > cls.max._value: - with pytest.raises(err_cls, match=msg): - method(ts, unit) - return - elif lb < cls.min._value: - with pytest.raises(err_cls, match=msg): - method(ts, unit) - return - - res = method(ts, unit) - - td = res - ts - diff = abs(td._value) - assert diff < nanos - assert res._value % nanos == 0 - - if method is cls.round: - assert diff <= nanos / 2 - elif method is cls.floor: - assert res <= ts - elif method is cls.ceil: - assert res >= ts - - nanos = 1 - checker(td, nanos, "ns") - - nanos = 1000 - checker(td, nanos, "us") - - nanos = 1_000_000 - checker(td, nanos, "ms") - - nanos = 1_000_000_000 - checker(td, nanos, "s") - - nanos = 60 * 1_000_000_000 - checker(td, nanos, "min") - - nanos = 60 * 60 * 1_000_000_000 - checker(td, nanos, "h") - - nanos = 24 * 60 * 60 * 1_000_000_000 - checker(td, nanos, "D") - - @pytest.mark.parametrize("unit", ["ns", "us", "ms", "s"]) - def test_round_non_nano(self, unit): - td = Timedelta("1 days 02:34:57").as_unit(unit) - - res = td.round("min") - assert res == Timedelta("1 days 02:35:00") - assert res._creso == td._creso - - res = td.floor("min") - assert res == Timedelta("1 days 02:34:00") - assert res._creso == td._creso - - res = td.ceil("min") - assert res == Timedelta("1 days 02:35:00") - assert res._creso == td._creso - def test_identity(self): td = Timedelta(10, unit="d") assert isinstance(td, Timedelta) @@ -1038,24 +663,3 @@ def test_timedelta_attribute_precision(): result += td.nanoseconds expected = td._value assert result == expected - - -@pytest.mark.parametrize( - "unit,unit_depr", - [ - ("h", "H"), - ("min", "T"), - ("s", "S"), - ("ms", "L"), - ("ns", "N"), - ("us", "U"), - ], -) -def test_units_H_T_S_L_N_U_deprecated(unit, unit_depr): - # GH#52536 - msg = f"'{unit_depr}' is deprecated and will be removed in a future version." - - expected = Timedelta(1, unit=unit) - with tm.assert_produces_warning(FutureWarning, match=msg): - result = Timedelta(1, unit=unit_depr) - tm.assert_equal(result, expected)