|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pandas as pd |
| 6 | +import pytest |
| 7 | +from typing_extensions import assert_type |
| 8 | + |
| 9 | +from tests import ( |
| 10 | + PD_LTE_23, |
| 11 | + TYPE_CHECKING_INVALID_USAGE, |
| 12 | + check, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def left_i() -> pd.Index: |
| 18 | + """Left operand""" |
| 19 | + lo = pd.MultiIndex.from_arrays([[1, 2, 3]]).levels[0] |
| 20 | + return check(assert_type(lo, pd.Index), pd.Index, np.integer) |
| 21 | + |
| 22 | + |
| 23 | +def test_truediv_py_scalar(left_i: pd.Index) -> None: |
| 24 | + """Test pd.Index[Any] (int) / Python native scalars""" |
| 25 | + b, i, f, c = True, 1, 1.0, 1j |
| 26 | + |
| 27 | + check(assert_type(left_i / b, pd.Index), pd.Index) |
| 28 | + check(assert_type(left_i / i, pd.Index), pd.Index) |
| 29 | + check(assert_type(left_i / f, pd.Index), pd.Index) |
| 30 | + check(assert_type(left_i / c, pd.Index), pd.Index) |
| 31 | + |
| 32 | + check(assert_type(b / left_i, pd.Index), pd.Index) |
| 33 | + check(assert_type(i / left_i, pd.Index), pd.Index) |
| 34 | + check(assert_type(f / left_i, pd.Index), pd.Index) |
| 35 | + check(assert_type(c / left_i, pd.Index), pd.Index) |
| 36 | + |
| 37 | + |
| 38 | +def test_truediv_py_sequence(left_i: pd.Index) -> None: |
| 39 | + """Test pd.Index[Any] (int) / Python native sequences""" |
| 40 | + b, i, f, c = [True, False, True], [2, 3, 5], [1.0, 2.0, 3.0], [1j, 1j, 4j] |
| 41 | + |
| 42 | + check(assert_type(left_i / b, pd.Index), pd.Index) |
| 43 | + check(assert_type(left_i / i, pd.Index), pd.Index) |
| 44 | + check(assert_type(left_i / f, pd.Index), pd.Index) |
| 45 | + check(assert_type(left_i / c, pd.Index), pd.Index) |
| 46 | + |
| 47 | + check(assert_type(b / left_i, pd.Index), pd.Index) |
| 48 | + check(assert_type(i / left_i, pd.Index), pd.Index) |
| 49 | + check(assert_type(f / left_i, pd.Index), pd.Index) |
| 50 | + check(assert_type(c / left_i, pd.Index), pd.Index) |
| 51 | + |
| 52 | + |
| 53 | +def test_truediv_numpy_array(left_i: pd.Index) -> None: |
| 54 | + """Test pd.Index[Any] (int) / numpy arrays""" |
| 55 | + b = np.array([True, False, True], np.bool_) |
| 56 | + i = np.array([2, 3, 5], np.int64) |
| 57 | + f = np.array([1.0, 2.0, 3.0], np.float64) |
| 58 | + c = np.array([1.1j, 2.2j, 4.1j], np.complex64) |
| 59 | + |
| 60 | + check(assert_type(left_i / b, pd.Index), pd.Index) |
| 61 | + check(assert_type(left_i / i, pd.Index), pd.Index) |
| 62 | + check(assert_type(left_i / f, pd.Index), pd.Index) |
| 63 | + check(assert_type(left_i / c, pd.Index), pd.Index) |
| 64 | + |
| 65 | + # `numpy` typing gives the corresponding `ndarray`s in the static type |
| 66 | + # checking, where our `__rtruediv__` cannot override. At runtime, they return |
| 67 | + # `Index`. |
| 68 | + # microsoft/pyright#10924 |
| 69 | + check( |
| 70 | + assert_type(b / left_i, Any), # pyright: ignore[reportAssertTypeFailure] |
| 71 | + pd.Index, |
| 72 | + ) |
| 73 | + check( |
| 74 | + assert_type(i / left_i, Any), # pyright: ignore[reportAssertTypeFailure] |
| 75 | + pd.Index, |
| 76 | + ) |
| 77 | + check( |
| 78 | + assert_type(f / left_i, Any), # pyright: ignore[reportAssertTypeFailure] |
| 79 | + pd.Index, |
| 80 | + ) |
| 81 | + check( |
| 82 | + assert_type(c / left_i, Any), # pyright: ignore[reportAssertTypeFailure] |
| 83 | + pd.Index, |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +def test_truediv_pd_index(left_i: pd.Index) -> None: |
| 88 | + """Test pd.Index[Any] (int) / pandas Indexes""" |
| 89 | + a = pd.MultiIndex.from_tuples([(1,), (2,), (3,)]).levels[0] |
| 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_i / a, pd.Index), pd.Index) |
| 96 | + check(assert_type(left_i / b, pd.Index), pd.Index) |
| 97 | + check(assert_type(left_i / i, pd.Index), pd.Index) |
| 98 | + check(assert_type(left_i / f, pd.Index), pd.Index) |
| 99 | + check(assert_type(left_i / c, pd.Index), pd.Index) |
| 100 | + |
| 101 | + check(assert_type(a / left_i, pd.Index), pd.Index) |
| 102 | + check(assert_type(b / left_i, pd.Index), pd.Index) |
| 103 | + check(assert_type(i / left_i, pd.Index), pd.Index) |
| 104 | + check(assert_type(f / left_i, pd.Index), pd.Index) |
| 105 | + check(assert_type(c / left_i, pd.Index), pd.Index) |
| 106 | + |
| 107 | + |
| 108 | +def test_truediv_paths(tmp_path: Path) -> None: |
| 109 | + """Test pd.Index of paths / path object. |
| 110 | +
|
| 111 | + Also GH 682.""" |
| 112 | + fpath = Path("a.png") |
| 113 | + folders, fpaths = pd.Index([tmp_path, tmp_path]), pd.Index([fpath, fpath]) |
| 114 | + |
| 115 | + check(assert_type(folders / fpath, pd.Index), pd.Index, Path) |
| 116 | + |
| 117 | + # mypy thinks it's `Path`, in contrast to Index.__rtruediv__(self, other: Path) -> Index: ... |
| 118 | + check(assert_type(tmp_path / fpaths, pd.Index), pd.Index, Path) # type: ignore[assert-type] |
| 119 | + |
| 120 | + |
| 121 | +def test_truediv_path(tmp_path: Path) -> None: |
| 122 | + """Test pd.Index / path object. |
| 123 | +
|
| 124 | + Also GH 682.""" |
| 125 | + fnames = pd.Index(["a.png", "b.gz", "c.txt"]) |
| 126 | + |
| 127 | + if PD_LTE_23: |
| 128 | + # Bug in 3.0 https://github.com/pandas-dev/pandas/issues/61940 (pyarrow.lib.ArrowInvalid) |
| 129 | + check(assert_type(fnames / tmp_path, pd.Index), pd.Index, Path) |
| 130 | + check(assert_type(tmp_path / fnames, pd.Index), pd.Index, Path) |
| 131 | + |
| 132 | + |
| 133 | +def test_truediv_str_py_str(left_i: pd.Index) -> None: |
| 134 | + """Test pd.Index[Any] (int) / Python str""" |
| 135 | + s = "abc" |
| 136 | + |
| 137 | + if TYPE_CHECKING_INVALID_USAGE: |
| 138 | + _0 = left_i / s # type: ignore[operator] # pyright:ignore[reportOperatorIssue] |
| 139 | + _1 = s / left_i # type: ignore[operator] # pyright:ignore[reportOperatorIssue] |
0 commit comments