Repository: pandas-dev/pandas
File: pandas/tests/tools/test_to_timedelta.py (Python)

Code before:
        result = to_timedelta(np.array([np.timedelta64(1, "s")]))
        expected = pd.Index(np.array([np.timedelta64(1, "s")]))
        tm.assert_index_equal(result, expected)

    def test_to_timedelta_series(self):
        # Series
        expected = Series(
            [timedelta(days=1), timedelta(days=1, seconds=1)], dtype="m8[us]"
        )

        msg = "'d' is deprecated and will be removed in a future version."
        with tm.assert_produces_warning(Pandas4Warning, match=msg):
            result = to_timedelta(Series(["1d", "1days 00:00:01"]))
        tm.assert_series_equal(result, expected)

    def test_to_timedelta_units(self):
        # with units
        result = TimedeltaIndex(
            [np.timedelta64(0, "ns"), np.timedelta64(10, "s").astype("m8[ns]")]
        )
        expected = to_timedelta([0, 10], unit="s").as_unit("ns")
        tm.assert_index_equal(result, expected)

    def test_to_timedelta_mixed_dtype(self):
        result = to_timedelta(np.array([0.5, 2]), unit="m")
        expected = TimedeltaIndex(
            ["0 days 00:00:30", "0 days 00:02:00"], dtype="timedelta64[ns]", freq=None
        )
        tm.assert_index_equal(result, expected)

    @pytest.mark.parametrize(
        "dtype, unit",
        [
            ["int64", "s"],
            ["int64", "m"],
            ["int64", "h"],
            ["timedelta64[s]", "s"],
            ["timedelta64[D]", "D"],
        ],
    )
    def test_to_timedelta_units_dtypes(self, dtype, unit):
        # arrays of various dtypes
        arr = np.array([1] * 5, dtype=dtype)
        result = to_timedelta(arr, unit=unit)
        exp_dtype = "m8[s]"
        expected = TimedeltaIndex([np.timedelta64(1, unit)] * 5, dtype=exp_dtype)
        tm.assert_index_equal(result, expected)

    def test_to_timedelta_oob_non_nano(self):
        arr = np.array([pd.NaT._value + 1], dtype="timedelta64[m]")


Review comment (suggestion):
```suggestion
        # https://github.com/pandas-dev/pandas/issues/64044
        result = to_timedelta(np.array([0.5, 2]), unit="m")
```

Code after:
        result = to_timedelta(np.array([np.timedelta64(1, "s")]))
        expected = pd.Index(np.array([np.timedelta64(1, "s")]))
        tm.assert_index_equal(result, expected)

    def test_to_timedelta_series(self):
        # Series
        expected = Series(
            [timedelta(days=1), timedelta(days=1, seconds=1)], dtype="m8[us]"
        )

        msg = "'d' is deprecated and will be removed in a future version."
        with tm.assert_produces_warning(Pandas4Warning, match=msg):
            result = to_timedelta(Series(["1d", "1days 00:00:01"]))
        tm.assert_series_equal(result, expected)

    def test_to_timedelta_units(self):
        # with units
        result = TimedeltaIndex(
            [np.timedelta64(0, "ns"), np.timedelta64(10, "s").astype("m8[ns]")]
        )
        expected = to_timedelta([0, 10], unit="s").as_unit("ns")
        tm.assert_index_equal(result, expected)

    def test_to_timedelta_mixed_dtype(self):
        # https://github.com/pandas-dev/pandas/issues/64044
        result = to_timedelta(np.array([0.5, 2]), unit="m")
        expected = TimedeltaIndex(
            ["0 days 00:00:30", "0 days 00:02:00"], dtype="timedelta64[ns]", freq=None
        )
        tm.assert_index_equal(result, expected)

    @pytest.mark.parametrize(
        "dtype, unit",
        [
            ["int64", "s"],
            ["int64", "m"],
            ["int64", "h"],
            ["timedelta64[s]", "s"],
            ["timedelta64[D]", "D"],
        ],
    )
    def test_to_timedelta_units_dtypes(self, dtype, unit):
        # arrays of various dtypes
        arr = np.array([1] * 5, dtype=dtype)
        result = to_timedelta(arr, unit=unit)
        exp_dtype = "m8[s]"
        expected = TimedeltaIndex([np.timedelta64(1, unit)] * 5, dtype=exp_dtype)
        tm.assert_index_equal(result, expected)

    def test_to_timedelta_oob_non_nano(self):
        arr = np.array([pd.NaT._value + 1], dtype="timedelta64[m]")