|
| 1 | +from datetime import timedelta |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from numpy import typing as npt # noqa: F401 |
| 5 | +import pandas as pd |
| 6 | +import pytest |
| 7 | +from typing_extensions import ( |
| 8 | + Never, |
| 9 | + assert_type, |
| 10 | +) |
| 11 | + |
| 12 | +from tests import ( |
| 13 | + PD_LTE_23, |
| 14 | + TYPE_CHECKING_INVALID_USAGE, |
| 15 | + check, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def left() -> "pd.Index[pd.Timedelta]": |
| 21 | + """left operand""" |
| 22 | + # pandas-dev/pandas#62524 |
| 23 | + lo = pd.Index([1]) * [timedelta(seconds=1)] # left operand |
| 24 | + return check(assert_type(lo, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 25 | + |
| 26 | + |
| 27 | +def test_mul_py_scalar(left: "pd.Index[pd.Timedelta]") -> None: |
| 28 | + """Test pd.Series[pd.Timedelta] * Python native scalars""" |
| 29 | + b, i, f, c = True, 1, 1.0, 1j |
| 30 | + |
| 31 | + # pandas-dev/pandas#62316 |
| 32 | + if PD_LTE_23: |
| 33 | + check(assert_type(left * b, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 34 | + check(assert_type(left * i, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 35 | + check(assert_type(left * f, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 36 | + if TYPE_CHECKING_INVALID_USAGE: |
| 37 | + _0 = left * c # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 38 | + |
| 39 | + if PD_LTE_23: |
| 40 | + check(assert_type(b * left, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 41 | + check(assert_type(i * left, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 42 | + check(assert_type(f * left, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 43 | + if TYPE_CHECKING_INVALID_USAGE: |
| 44 | + _1 = c * left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 45 | + |
| 46 | + |
| 47 | +def test_mul_py_sequence(left: "pd.Index[pd.Timedelta]") -> None: |
| 48 | + """Test pd.Series[pd.Timedelta] * Python native sequences""" |
| 49 | + b, i, f, c = [True], [2], [1.5], [1.7j] |
| 50 | + |
| 51 | + # pandas-dev/pandas#62316 |
| 52 | + if PD_LTE_23: |
| 53 | + check(assert_type(left * b, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 54 | + check(assert_type(left * i, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 55 | + check(assert_type(left * f, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 56 | + if TYPE_CHECKING_INVALID_USAGE: |
| 57 | + _0 = left * c # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 58 | + |
| 59 | + if PD_LTE_23: |
| 60 | + check(assert_type(b * left, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 61 | + check(assert_type(i * left, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 62 | + check(assert_type(f * left, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 63 | + if TYPE_CHECKING_INVALID_USAGE: |
| 64 | + _1 = c * left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 65 | + |
| 66 | + |
| 67 | +def test_mul_numpy_array(left: "pd.Index[pd.Timedelta]") -> None: |
| 68 | + """Test pd.Series[pd.Timedelta] * numpy arrays""" |
| 69 | + b = np.array([True], np.bool_) |
| 70 | + i = np.array([2], np.int64) |
| 71 | + f = np.array([1.5], np.float64) |
| 72 | + c = np.array([1.7j], np.complex128) |
| 73 | + |
| 74 | + # pandas-dev/pandas#62316 |
| 75 | + if PD_LTE_23: |
| 76 | + check(assert_type(left * b, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 77 | + check(assert_type(left * i, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 78 | + check(assert_type(left * f, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 79 | + if TYPE_CHECKING_INVALID_USAGE: |
| 80 | + assert_type(left * c, Never) |
| 81 | + |
| 82 | + # `numpy` typing gives the corresponding `ndarray`s in the static type |
| 83 | + # checking, where our `__rmul__` cannot override. At runtime, they return |
| 84 | + # `Series` with the correct element type. |
| 85 | + if PD_LTE_23: |
| 86 | + check(assert_type(b * left, "npt.NDArray[np.bool_]"), pd.Index, timedelta) |
| 87 | + check(assert_type(i * left, "npt.NDArray[np.int64]"), pd.Index, timedelta) |
| 88 | + check(assert_type(f * left, "npt.NDArray[np.float64]"), pd.Index, timedelta) |
| 89 | + if TYPE_CHECKING_INVALID_USAGE: |
| 90 | + # We made it Never, but numpy takes over |
| 91 | + assert_type(c * left, "npt.NDArray[np.complex128]") |
| 92 | + |
| 93 | + |
| 94 | +def test_mul_pd_index(left: "pd.Index[pd.Timedelta]") -> None: |
| 95 | + """Test pd.Series[pd.Timedelta] * pandas Indexes""" |
| 96 | + b = pd.Index([True]) |
| 97 | + i = pd.Index([2]) |
| 98 | + f = pd.Index([1.5]) |
| 99 | + c = pd.Index([1.7j]) |
| 100 | + |
| 101 | + # pandas-dev/pandas#62316 |
| 102 | + if PD_LTE_23: |
| 103 | + check(assert_type(left * b, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 104 | + check(assert_type(left * i, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 105 | + check(assert_type(left * f, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 106 | + if TYPE_CHECKING_INVALID_USAGE: |
| 107 | + _0 = left * c # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 108 | + |
| 109 | + if PD_LTE_23: |
| 110 | + check(assert_type(b * left, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 111 | + check(assert_type(i * left, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 112 | + check(assert_type(f * left, "pd.Index[pd.Timedelta]"), pd.Index, timedelta) |
| 113 | + if TYPE_CHECKING_INVALID_USAGE: |
| 114 | + _1 = c * left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
0 commit comments