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