forked from pandas-dev/pandas-stubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_indexes.py
182 lines (140 loc) · 6.61 KB
/
test_indexes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from __future__ import annotations
from typing import Union
import numpy as np
from numpy import typing as npt
import pandas as pd
from pandas.core.indexes.numeric import NumericIndex
import pytest
from typing_extensions import assert_type
from tests import check
def test_index_unique() -> None:
df = pd.DataFrame({"x": [1, 2, 3, 4]}, index=pd.Index([1, 2, 3, 2]))
ind = df.index
check(assert_type(ind, pd.Index), pd.Index)
i2 = ind.unique()
check(assert_type(i2, pd.Index), pd.Index)
def test_index_isin() -> None:
ind = pd.Index([1, 2, 3, 4, 5])
isin = ind.isin([2, 4])
check(assert_type(isin, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
def test_index_astype() -> None:
indi = pd.Index([1, 2, 3])
inds = pd.Index(["a", "b", "c"])
indc = indi.astype(inds.dtype)
check(assert_type(indc, pd.Index), pd.Index)
mi = pd.MultiIndex.from_product([["a", "b"], ["c", "d"]], names=["ab", "cd"])
mia = mi.astype(object) # object is only valid parameter for MultiIndex.astype()
check(assert_type(mia, pd.MultiIndex), pd.MultiIndex)
check(
assert_type(mi.to_frame(name=[3, 7], allow_duplicates=True), pd.DataFrame),
pd.DataFrame,
)
def test_multiindex_get_level_values() -> None:
mi = pd.MultiIndex.from_product([["a", "b"], ["c", "d"]], names=["ab", "cd"])
i1 = mi.get_level_values("ab")
check(assert_type(i1, pd.Index), pd.Index)
def test_index_tolist() -> None:
i1 = pd.Index([1, 2, 3])
check(assert_type(i1.tolist(), list), list, int)
check(assert_type(i1.to_list(), list), list, int)
def test_column_getitem() -> None:
# https://github.com/microsoft/python-type-stubs/issues/199#issuecomment-1132806594
df = pd.DataFrame([[1, 2, 3]], columns=["a", "b", "c"])
column = df.columns[0]
check(assert_type(df[column], pd.Series), pd.Series, int)
def test_column_contains() -> None:
# https://github.com/microsoft/python-type-stubs/issues/199
df = pd.DataFrame({"A": [1, 2], "B": ["c", "d"], "E": [3, 4]})
collist = [column for column in df.columns]
collist2 = [column for column in df.columns[df.columns.str.contains("A|B")]]
length = len(df.columns[df.columns.str.contains("A|B")])
def test_difference_none() -> None:
# https://github.com/pandas-dev/pandas-stubs/issues/17
ind = pd.Index([1, 2, 3])
check(assert_type(ind.difference([1, None]), pd.Index), pd.Index)
# GH 253
check(assert_type(ind.difference([1]), pd.Index), pd.Index)
def test_str_split() -> None:
# GH 194
ind = pd.Index(["a-b", "c-d"])
check(assert_type(ind.str.split("-"), pd.Index), pd.Index)
check(assert_type(ind.str.split("-", expand=True), pd.MultiIndex), pd.MultiIndex)
def test_index_dropna():
idx = pd.Index([1, 2])
check(assert_type(idx.dropna(how="all"), pd.Index), pd.Index)
check(assert_type(idx.dropna(how="any"), pd.Index), pd.Index)
midx = pd.MultiIndex.from_arrays([[1, 2], [3, 4]])
check(assert_type(midx.dropna(how="all"), pd.MultiIndex), pd.MultiIndex)
check(assert_type(midx.dropna(how="any"), pd.MultiIndex), pd.MultiIndex)
def test_index_neg():
# GH 253
idx = pd.Index([1, 2])
check(assert_type(-idx, pd.Index), pd.Index)
def test_types_to_numpy() -> None:
idx = pd.Index([1, 2])
check(assert_type(idx.to_numpy(), np.ndarray), np.ndarray)
check(assert_type(idx.to_numpy(dtype="int", copy=True), np.ndarray), np.ndarray)
check(assert_type(idx.to_numpy(na_value=0), np.ndarray), np.ndarray)
def test_index_arithmetic() -> None:
# GH 287
idx = pd.Index([1, 2.2, 3], dtype=float)
check(assert_type(idx + 3, NumericIndex), NumericIndex)
check(assert_type(idx - 3, NumericIndex), NumericIndex)
check(assert_type(idx * 3, NumericIndex), NumericIndex)
check(assert_type(idx / 3, NumericIndex), NumericIndex)
check(assert_type(idx // 3, NumericIndex), NumericIndex)
check(assert_type(3 + idx, NumericIndex), NumericIndex)
check(assert_type(3 - idx, NumericIndex), NumericIndex)
check(assert_type(3 * idx, NumericIndex), NumericIndex)
check(assert_type(3 / idx, NumericIndex), NumericIndex)
check(assert_type(3 // idx, NumericIndex), NumericIndex)
def test_index_relops() -> None:
# GH 265
data = pd.date_range("2022-01-01", "2022-01-31", freq="D")
x = pd.Timestamp("2022-01-17")
idx = pd.Index(data, name="date")
check(assert_type(data[x <= idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x < idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x >= idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x > idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[idx < x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[idx >= x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[idx > x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[idx <= x], pd.DatetimeIndex), pd.DatetimeIndex)
dt_idx = pd.DatetimeIndex(data, name="date")
check(assert_type(data[x <= dt_idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x >= dt_idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x < dt_idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[x > dt_idx], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[dt_idx <= x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[dt_idx >= x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[dt_idx < x], pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(data[dt_idx > x], pd.DatetimeIndex), pd.DatetimeIndex)
ind = pd.Index([1, 2, 3])
check(assert_type(ind <= 2, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
check(assert_type(ind >= 2, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
check(assert_type(ind < 2, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
check(assert_type(ind > 2, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
def test_range_index_union():
with pytest.warns(FutureWarning, match="pandas.Int64Index"):
check(
assert_type(
pd.RangeIndex(0, 10).union(pd.RangeIndex(10, 20)),
Union[pd.Index, pd.Int64Index, pd.RangeIndex],
),
pd.RangeIndex,
)
check(
assert_type(
pd.RangeIndex(0, 10).union([11, 12, 13]),
Union[pd.Index, pd.Int64Index, pd.RangeIndex],
),
pd.Int64Index,
)
check(
assert_type(
pd.RangeIndex(0, 10).union(["a", "b", "c"]),
Union[pd.Index, pd.Int64Index, pd.RangeIndex],
),
pd.Index,
)