|
| 1 | +from datetime import ( |
| 2 | + datetime, |
| 3 | + timedelta, |
| 4 | +) |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pandas as pd |
| 9 | +import pytest |
| 10 | +from typing_extensions import ( |
| 11 | + Never, |
| 12 | + assert_type, |
| 13 | +) |
| 14 | + |
| 15 | +from tests import ( |
| 16 | + TYPE_CHECKING_INVALID_USAGE, |
| 17 | + check, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def left() -> "pd.Index[float]": |
| 23 | + """Left operand""" |
| 24 | + lo = pd.Index([1.2, 2.4, 3.6]) |
| 25 | + return check(assert_type(lo, "pd.Index[float]"), pd.Index, np.floating) |
| 26 | + |
| 27 | + |
| 28 | +def test_floordiv_py_scalar(left: "pd.Index[float]") -> None: |
| 29 | + """Test pd.Index[float] // Python native scalars""" |
| 30 | + b, i, f, c = True, 1, 1.0, 1j |
| 31 | + s, d = datetime(2025, 9, 27), timedelta(seconds=1) |
| 32 | + |
| 33 | + check(assert_type(left // b, "pd.Index[float]"), pd.Index, np.floating) |
| 34 | + check(assert_type(left // i, "pd.Index[float]"), pd.Index, np.floating) |
| 35 | + check(assert_type(left // f, "pd.Index[float]"), pd.Index, np.floating) |
| 36 | + if TYPE_CHECKING_INVALID_USAGE: |
| 37 | + _03 = left // c # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 38 | + _04 = left // s # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 39 | + _05 = left // d # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 40 | + |
| 41 | + check(assert_type(b // left, "pd.Index[float]"), pd.Index, np.floating) |
| 42 | + check(assert_type(i // left, "pd.Index[float]"), pd.Index, np.floating) |
| 43 | + check(assert_type(f // left, "pd.Index[float]"), pd.Index, np.floating) |
| 44 | + if TYPE_CHECKING_INVALID_USAGE: |
| 45 | + _13 = c // left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 46 | + _14 = s // left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 47 | + check(assert_type(d // left, pd.TimedeltaIndex), pd.TimedeltaIndex, pd.Timedelta) |
| 48 | + |
| 49 | + |
| 50 | +def test_floordiv_py_sequence(left: "pd.Index[float]") -> None: |
| 51 | + """Test pd.Index[float] // Python native sequences""" |
| 52 | + b, i, f, c = [True, False, True], [2, 3, 5], [1.0, 2.0, 3.0], [1j, 1j, 4j] |
| 53 | + s = [datetime(2025, 10, 27 + d) for d in range(3)] |
| 54 | + d = [timedelta(seconds=s) for s in range(3)] |
| 55 | + |
| 56 | + check(assert_type(left // b, "pd.Index[float]"), pd.Index, np.floating) |
| 57 | + check(assert_type(left // i, "pd.Index[float]"), pd.Index, np.floating) |
| 58 | + check(assert_type(left // f, "pd.Index[float]"), pd.Index, np.floating) |
| 59 | + if TYPE_CHECKING_INVALID_USAGE: |
| 60 | + _03 = left // c # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 61 | + _04 = left // s # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 62 | + _05 = left // d # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 63 | + |
| 64 | + check(assert_type(b // left, "pd.Index[float]"), pd.Index, np.floating) |
| 65 | + check(assert_type(i // left, "pd.Index[float]"), pd.Index, np.floating) |
| 66 | + check(assert_type(f // left, "pd.Index[float]"), pd.Index, np.floating) |
| 67 | + if TYPE_CHECKING_INVALID_USAGE: |
| 68 | + _13 = c // left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 69 | + _14 = s // left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 70 | + _15 = d // left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 71 | + |
| 72 | + |
| 73 | +def test_floordiv_numpy_array(left: "pd.Index[float]") -> None: |
| 74 | + """Test pd.Index[float] // numpy arrays""" |
| 75 | + b = np.array([True, False, True], np.bool_) |
| 76 | + i = np.array([2, 3, 5], np.int64) |
| 77 | + f = np.array([1.0, 2.0, 3.0], np.float64) |
| 78 | + c = np.array([1.1j, 2.2j, 4.1j], np.complex128) |
| 79 | + s = np.array( |
| 80 | + [np.datetime64(f"2025-10-{d:02d}") for d in (23, 24, 25)], np.datetime64 |
| 81 | + ) |
| 82 | + d = np.array([np.timedelta64(s + 1, "s") for s in range(3)], np.timedelta64) |
| 83 | + |
| 84 | + check(assert_type(left // b, "pd.Index[float]"), pd.Index, np.floating) |
| 85 | + check(assert_type(left // i, "pd.Index[float]"), pd.Index, np.floating) |
| 86 | + check(assert_type(left // f, "pd.Index[float]"), pd.Index, np.floating) |
| 87 | + if TYPE_CHECKING_INVALID_USAGE: |
| 88 | + assert_type(left // c, Never) |
| 89 | + assert_type(left // s, Never) |
| 90 | + assert_type(left // d, Never) |
| 91 | + |
| 92 | + # `numpy` typing gives the corresponding `ndarray`s in the static type |
| 93 | + # checking, where our `__rfloordiv__` cannot override. At runtime, they lead to |
| 94 | + # errors or pd.Index. |
| 95 | + check(b // left, pd.Index, np.floating) |
| 96 | + check(i // left, pd.Index, np.floating) |
| 97 | + check(f // left, pd.Index, np.floating) |
| 98 | + if TYPE_CHECKING_INVALID_USAGE: |
| 99 | + assert_type(c // left, Any) |
| 100 | + assert_type(s // left, Any) |
| 101 | + check( |
| 102 | + assert_type(d // left, "np.typing.NDArray[np.int64]"), |
| 103 | + pd.TimedeltaIndex, |
| 104 | + pd.Timedelta, |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +def test_floordiv_pd_index(left: "pd.Index[float]") -> None: |
| 109 | + """Test pd.Index[float] // pandas Indexes""" |
| 110 | + b = pd.Index([True, False, True]) |
| 111 | + i = pd.Index([2, 3, 5]) |
| 112 | + f = pd.Index([1.0, 2.0, 3.0]) |
| 113 | + c = pd.Index([1.1j, 2.2j, 4.1j]) |
| 114 | + s = pd.Index([datetime(2025, 10, d) for d in (27, 28, 29)]) |
| 115 | + d = pd.Index([timedelta(seconds=s + 1) for s in range(3)]) |
| 116 | + |
| 117 | + check(assert_type(left // b, "pd.Index[float]"), pd.Index, np.floating) |
| 118 | + check(assert_type(left // i, "pd.Index[float]"), pd.Index, np.floating) |
| 119 | + check(assert_type(left // f, "pd.Index[float]"), pd.Index, np.floating) |
| 120 | + if TYPE_CHECKING_INVALID_USAGE: |
| 121 | + _03 = left // c # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 122 | + _04 = left // s # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 123 | + _05 = left // d # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 124 | + |
| 125 | + check(assert_type(b // left, "pd.Index[float]"), pd.Index, np.floating) |
| 126 | + check(assert_type(i // left, "pd.Index[float]"), pd.Index, np.floating) |
| 127 | + check(assert_type(f // left, "pd.Index[float]"), pd.Index, np.floating) |
| 128 | + if TYPE_CHECKING_INVALID_USAGE: |
| 129 | + _13 = c // left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 130 | + _14 = s // left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 131 | + check(assert_type(d // left, pd.TimedeltaIndex), pd.TimedeltaIndex, pd.Timedelta) |
0 commit comments