Skip to content

Commit b5bb1d3

Browse files
committed
Index[complex]
1 parent fee1de5 commit b5bb1d3

File tree

3 files changed

+106
-8
lines changed

3 files changed

+106
-8
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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)

tests/indexes/arithmetic/test_truediv.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ def test_truediv_paths(tmp_path: Path) -> None:
114114

115115
check(assert_type(folders / fpath, pd.Index), pd.Index, Path)
116116

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]
117+
check(assert_type(tmp_path / fpaths, pd.Index), pd.Index, Path)
119118

120119

121120
def test_truediv_path(tmp_path: Path) -> None:

tests/series/arithmetic/complex/test_truediv.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import numpy as np
22
from numpy import typing as npt # noqa: F401
33
import pandas as pd
4+
import pytest
45
from typing_extensions import assert_type
56

67
from tests import check
78

8-
left = pd.Series([1j, 2j, 3j]) # left operand
99

10+
@pytest.fixture
11+
def left() -> "pd.Series[complex]":
12+
"""Left operand"""
13+
lo = pd.Series([1j, 2j, 3j])
14+
return check(assert_type(lo, "pd.Series[complex]"), pd.Series, np.complexfloating)
1015

11-
def test_truediv_py_scalar() -> None:
16+
17+
def test_truediv_py_scalar(left: "pd.Series[complex]") -> None:
1218
"""Test pd.Series[complex] / Python native scalars"""
1319
b, i, f, c = True, 1, 1.0, 1j
1420

@@ -83,7 +89,7 @@ def test_truediv_py_scalar() -> None:
8389
)
8490

8591

86-
def test_truediv_py_sequence() -> None:
92+
def test_truediv_py_sequence(left: "pd.Series[complex]") -> None:
8793
"""Test pd.Series[complex] / Python native sequences"""
8894
b, i, f, c = [True, False, True], [2, 3, 5], [1.0, 2.0, 3.0], [1j, 1j, 4j]
8995

@@ -158,7 +164,7 @@ def test_truediv_py_sequence() -> None:
158164
)
159165

160166

161-
def test_truediv_numpy_array() -> None:
167+
def test_truediv_numpy_array(left: "pd.Series[complex]") -> None:
162168
"""Test pd.Series[complex] / numpy arrays"""
163169
b = np.array([True, False, True], np.bool_)
164170
i = np.array([2, 3, 5], np.int64)
@@ -249,7 +255,7 @@ def test_truediv_numpy_array() -> None:
249255
)
250256

251257

252-
def test_truediv_pd_index() -> None:
258+
def test_truediv_pd_index(left: "pd.Series[complex]") -> None:
253259
"""Test pd.Series[complex] / pandas Indexes"""
254260
b = pd.Index([True, False, True])
255261
i = pd.Index([2, 3, 5])
@@ -327,7 +333,7 @@ def test_truediv_pd_index() -> None:
327333
)
328334

329335

330-
def test_truediv_pd_series() -> None:
336+
def test_truediv_pd_series(left: "pd.Series[complex]") -> None:
331337
"""Test pd.Series[complex] / pandas Series"""
332338
b = pd.Series([True, False, True])
333339
i = pd.Series([2, 3, 5])

0 commit comments

Comments
 (0)