|
| 1 | +import numpy as np |
| 2 | +from numpy import typing as npt # noqa: F401 |
| 3 | +import pandas as pd |
| 4 | +import pytest |
| 5 | +from typing_extensions import assert_type |
| 6 | + |
| 7 | +from tests import ( |
| 8 | + TYPE_CHECKING_INVALID_USAGE, |
| 9 | + check, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def left() -> "pd.Index[float]": |
| 15 | + """Left operand""" |
| 16 | + lo = pd.Index([1.0, 2.0, 3.0]) |
| 17 | + return check(assert_type(lo, "pd.Index[float]"), pd.Index, np.floating) |
| 18 | + |
| 19 | + |
| 20 | +def test_truediv_py_scalar(left: "pd.Index[float]") -> None: |
| 21 | + """Test pd.Index[float] / Python native scalars""" |
| 22 | + b, i, f, c = True, 1, 1.0, 1j |
| 23 | + |
| 24 | + check(assert_type(left / b, "pd.Index[float]"), pd.Index, np.floating) |
| 25 | + check(assert_type(left / i, "pd.Index[float]"), pd.Index, np.floating) |
| 26 | + check(assert_type(left / f, "pd.Index[float]"), pd.Index, np.floating) |
| 27 | + check(assert_type(left / c, "pd.Index[complex]"), pd.Index, np.complexfloating) |
| 28 | + |
| 29 | + check(assert_type(b / left, "pd.Index[float]"), pd.Index, np.floating) |
| 30 | + check(assert_type(i / left, "pd.Index[float]"), pd.Index, np.floating) |
| 31 | + check(assert_type(f / left, "pd.Index[float]"), pd.Index, np.floating) |
| 32 | + check(assert_type(c / left, "pd.Index[complex]"), pd.Index, np.complexfloating) |
| 33 | + |
| 34 | + |
| 35 | +def test_truediv_py_sequence(left: "pd.Index[float]") -> None: |
| 36 | + """Test pd.Index[float] / Python native sequences""" |
| 37 | + b, i, f, c = [True, False, True], [2, 3, 5], [1.0, 2.0, 3.0], [1j, 1j, 4j] |
| 38 | + |
| 39 | + check(assert_type(left / b, "pd.Index[float]"), pd.Index, np.floating) |
| 40 | + check(assert_type(left / i, "pd.Index[float]"), pd.Index, np.floating) |
| 41 | + check(assert_type(left / f, "pd.Index[float]"), pd.Index, np.floating) |
| 42 | + check(assert_type(left / c, "pd.Index[complex]"), pd.Index, np.complexfloating) |
| 43 | + |
| 44 | + check(assert_type(b / left, "pd.Index[float]"), pd.Index, np.floating) |
| 45 | + check(assert_type(i / left, "pd.Index[float]"), pd.Index, np.floating) |
| 46 | + check(assert_type(f / left, "pd.Index[float]"), pd.Index, np.floating) |
| 47 | + check(assert_type(c / left, "pd.Index[complex]"), pd.Index, np.complexfloating) |
| 48 | + |
| 49 | + |
| 50 | +def test_truediv_numpy_array(left: "pd.Index[float]") -> None: |
| 51 | + """Test pd.Index[float] / numpy arrays""" |
| 52 | + b = np.array([True, False, True], np.bool_) |
| 53 | + i = np.array([2, 3, 5], np.int64) |
| 54 | + f = np.array([1.0, 2.0, 3.0], np.float64) |
| 55 | + c = np.array([1.1j, 2.2j, 4.1j], np.complex128) |
| 56 | + |
| 57 | + check(assert_type(left / b, "pd.Index[float]"), pd.Index, np.floating) |
| 58 | + check(assert_type(left / i, "pd.Index[float]"), pd.Index, np.floating) |
| 59 | + check(assert_type(left / f, "pd.Index[float]"), pd.Index, np.floating) |
| 60 | + check(assert_type(left / c, "pd.Index[complex]"), pd.Index, np.complexfloating) |
| 61 | + |
| 62 | + # `numpy` typing gives the corresponding `ndarray`s in the static type |
| 63 | + # checking, where our `__rtruediv__` cannot override. At runtime, they return |
| 64 | + # `Series` with the correct element type. |
| 65 | + check(assert_type(b / left, "npt.NDArray[np.float64]"), pd.Index, np.floating) |
| 66 | + check(assert_type(i / left, "npt.NDArray[np.float64]"), pd.Index, np.floating) |
| 67 | + check(assert_type(f / left, "npt.NDArray[np.float64]"), pd.Index, np.floating) |
| 68 | + check( |
| 69 | + assert_type(c / left, "npt.NDArray[np.complex128]"), |
| 70 | + pd.Index, |
| 71 | + np.complexfloating, |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +def test_truediv_pd_scalar(left: "pd.Index[float]") -> None: |
| 76 | + """Test pd.Index[float] / pandas scalars""" |
| 77 | + s, d = pd.Timestamp(2025, 9, 24), pd.Timedelta(seconds=1) |
| 78 | + |
| 79 | + if TYPE_CHECKING_INVALID_USAGE: |
| 80 | + _00 = left / s # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 81 | + _01 = left / d # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 82 | + |
| 83 | + if TYPE_CHECKING_INVALID_USAGE: |
| 84 | + _10 = s / left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 85 | + check(assert_type(d / left, "pd.Index[pd.Timedelta]"), pd.Index, pd.Timedelta) |
| 86 | + |
| 87 | + |
| 88 | +def test_truediv_pd_index(left: "pd.Index[float]") -> None: |
| 89 | + """Test pd.Index[float] / pandas Indexes""" |
| 90 | + b = pd.Index([True, False, True]) |
| 91 | + i = pd.Index([2, 3, 5]) |
| 92 | + f = pd.Index([1.0, 2.0, 3.0]) |
| 93 | + c = pd.Index([1.1j, 2.2j, 4.1j]) |
| 94 | + |
| 95 | + check(assert_type(left / b, "pd.Index[float]"), pd.Index, np.floating) |
| 96 | + check(assert_type(left / i, "pd.Index[float]"), pd.Index, np.floating) |
| 97 | + check(assert_type(left / f, "pd.Index[float]"), pd.Index, np.floating) |
| 98 | + check(assert_type(left / c, "pd.Index[complex]"), pd.Index, np.complexfloating) |
| 99 | + |
| 100 | + check(assert_type(b / left, "pd.Index[float]"), pd.Index, np.floating) |
| 101 | + check(assert_type(i / left, "pd.Index[float]"), pd.Index, np.floating) |
| 102 | + check(assert_type(f / left, "pd.Index[float]"), pd.Index, np.floating) |
| 103 | + check(assert_type(c / left, "pd.Index[complex]"), pd.Index, np.complexfloating) |
0 commit comments