-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathtest_api_types.py
416 lines (339 loc) · 14.8 KB
/
test_api_types.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import numpy as np
import pandas as pd
from pandas.api.extensions import ExtensionDtype
from pandas.api.interchange import from_dataframe
import pandas.api.types as api
from typing_extensions import assert_type
from pandas._typing import DtypeObj
from tests import check
nparr = np.array([1, 2, 3])
arr = pd.Series([1, 2, 3])
obj = "True"
mapping = {"a": "a"}
dframe = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
dtylike = np.dtype(np.int32)
ind = pd.Index([1, 2.0])
def test_is_array_like() -> None:
check(assert_type(api.is_array_like(arr), bool), bool)
check(assert_type(api.is_array_like(nparr), bool), bool)
check(assert_type(api.is_array_like(dtylike), bool), bool)
check(
assert_type(api.is_array_like(dframe), bool),
bool,
)
check(assert_type(api.is_array_like(ind), bool), bool)
def test_is_bool() -> None:
check(assert_type(api.is_bool(obj), bool), bool)
check(assert_type(api.is_bool(nparr), bool), bool)
check(assert_type(api.is_bool(dtylike), bool), bool)
check(assert_type(api.is_bool(arr), bool), bool)
check(assert_type(api.is_bool(dframe), bool), bool)
check(assert_type(api.is_bool(ind), bool), bool)
def test_is_bool_dtype() -> None:
check(assert_type(api.is_bool_dtype(arr), bool), bool)
check(assert_type(api.is_bool_dtype(nparr), bool), bool)
check(assert_type(api.is_bool_dtype(dtylike), bool), bool)
check(
assert_type(api.is_bool_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_bool_dtype(ind), bool), bool)
check(assert_type(api.is_bool_dtype(ExtensionDtype), bool), bool)
def test_is_complex() -> None:
check(assert_type(api.is_complex(obj), bool), bool)
check(assert_type(api.is_complex(nparr), bool), bool)
check(assert_type(api.is_complex(dtylike), bool), bool)
check(assert_type(api.is_complex(arr), bool), bool)
check(
assert_type(api.is_complex(dframe), bool),
bool,
)
check(assert_type(api.is_complex(ind), bool), bool)
def test_is_complex_dtype() -> None:
check(assert_type(api.is_complex_dtype(arr), bool), bool)
check(assert_type(api.is_complex_dtype(nparr), bool), bool)
check(assert_type(api.is_complex_dtype(dtylike), bool), bool)
check(
assert_type(api.is_complex_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_complex_dtype(ind), bool), bool)
# check(assert_type(api.is_complex_dtype(ExtensionDtype), bool), bool) pandas GH 50923
def test_is_datetime64_any_dtype() -> None:
check(assert_type(api.is_datetime64_any_dtype(arr), bool), bool)
check(assert_type(api.is_datetime64_any_dtype(nparr), bool), bool)
check(assert_type(api.is_datetime64_any_dtype(dtylike), bool), bool)
check(
assert_type(api.is_datetime64_any_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_datetime64_any_dtype(ind), bool), bool)
# check(assert_type(api.is_datetime64_any_dtype(ExtensionDtype), bool), bool) pandas GH 50923
def test_is_datetime64_dtype() -> None:
check(assert_type(api.is_datetime64_dtype(arr), bool), bool)
check(assert_type(api.is_datetime64_dtype(nparr), bool), bool)
check(assert_type(api.is_datetime64_dtype(dtylike), bool), bool)
check(
assert_type(api.is_datetime64_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_datetime64_dtype(ind), bool), bool)
# check(assert_type(api.is_datetime64_dtype(ExtensionDtype), bool), bool) pandas GH 50923
def test_is_datetime64_ns_dtype() -> None:
check(assert_type(api.is_datetime64_ns_dtype(arr), bool), bool)
check(assert_type(api.is_datetime64_ns_dtype(nparr), bool), bool)
check(assert_type(api.is_datetime64_ns_dtype(dtylike), bool), bool)
check(
assert_type(api.is_datetime64_ns_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_datetime64_ns_dtype(ind), bool), bool)
check(assert_type(api.is_datetime64_ns_dtype(ExtensionDtype), bool), bool)
def test_is_dict_like() -> None:
check(assert_type(api.is_dict_like(mapping), bool), bool)
check(assert_type(api.is_dict_like(nparr), bool), bool)
check(assert_type(api.is_dict_like(dtylike), bool), bool)
check(assert_type(api.is_dict_like(arr), bool), bool)
check(
assert_type(api.is_dict_like(dframe), bool),
bool,
)
check(assert_type(api.is_dict_like(ind), bool), bool)
def test_is_dtype_equal() -> None:
check(assert_type(api.is_dtype_equal("i4", np.int8), bool), bool)
def test_is_extension_array_dtype() -> None:
check(assert_type(api.is_extension_array_dtype(arr), bool), bool)
check(assert_type(api.is_extension_array_dtype(nparr), bool), bool)
check(assert_type(api.is_extension_array_dtype(dtylike), bool), bool)
check(
assert_type(api.is_extension_array_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_extension_array_dtype(ind), bool), bool)
check(assert_type(api.is_extension_array_dtype(ExtensionDtype), bool), bool)
def test_is_file_like() -> None:
check(assert_type(api.is_file_like(obj), bool), bool)
check(assert_type(api.is_file_like(nparr), bool), bool)
check(assert_type(api.is_file_like(dtylike), bool), bool)
check(assert_type(api.is_file_like(arr), bool), bool)
check(
assert_type(api.is_file_like(dframe), bool),
bool,
)
check(assert_type(api.is_file_like(ind), bool), bool)
def test_is_float() -> None:
check(assert_type(api.is_float(obj), bool), bool)
check(assert_type(api.is_float(nparr), bool), bool)
check(assert_type(api.is_float(dtylike), bool), bool)
check(assert_type(api.is_float(arr), bool), bool)
check(assert_type(api.is_float(dframe), bool), bool)
check(assert_type(api.is_float(ind), bool), bool)
def test_is_float_dtype() -> None:
check(assert_type(api.is_float_dtype(arr), bool), bool)
check(assert_type(api.is_float_dtype(nparr), bool), bool)
check(assert_type(api.is_float_dtype(dtylike), bool), bool)
check(
assert_type(api.is_float_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_float_dtype(ind), bool), bool)
# check(assert_type(api.is_float_dtype(ExtensionDtype), bool), bool) pandas GH 50923
def test_is_hashable() -> None:
check(assert_type(api.is_hashable(obj), bool), bool)
check(assert_type(api.is_hashable(nparr), bool), bool)
check(assert_type(api.is_hashable(dtylike), bool), bool)
check(assert_type(api.is_hashable(arr), bool), bool)
check(
assert_type(api.is_hashable(dframe), bool),
bool,
)
check(assert_type(api.is_hashable(ind), bool), bool)
def test_is_integer() -> None:
check(assert_type(api.is_integer(obj), bool), bool)
check(assert_type(api.is_integer(nparr), bool), bool)
check(assert_type(api.is_integer(dtylike), bool), bool)
check(assert_type(api.is_integer(arr), bool), bool)
check(
assert_type(api.is_integer(dframe), bool),
bool,
)
check(assert_type(api.is_integer(ind), bool), bool)
def test_is_integer_dtype() -> None:
check(assert_type(api.is_integer_dtype(arr), bool), bool)
check(assert_type(api.is_integer_dtype(nparr), bool), bool)
check(assert_type(api.is_integer_dtype(dtylike), bool), bool)
check(
assert_type(api.is_integer_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_integer_dtype(ind), bool), bool)
# check(assert_type(api.is_integer_dtype(ExtensionDtype), bool), bool) pandas GH 50923
def test_is_iterator() -> None:
check(assert_type(api.is_iterator(obj), bool), bool)
check(assert_type(api.is_iterator(nparr), bool), bool)
check(assert_type(api.is_iterator(dtylike), bool), bool)
check(assert_type(api.is_iterator(arr), bool), bool)
check(
assert_type(api.is_iterator(dframe), bool),
bool,
)
check(assert_type(api.is_iterator(ind), bool), bool)
def test_is_list_like() -> None:
check(assert_type(api.is_list_like(obj), bool), bool)
check(assert_type(api.is_list_like(nparr), bool), bool)
check(assert_type(api.is_list_like(dtylike), bool), bool)
check(assert_type(api.is_list_like(arr), bool), bool)
check(
assert_type(api.is_list_like(dframe), bool),
bool,
)
check(assert_type(api.is_list_like(ind), bool), bool)
def test_is_named_tuple() -> None:
check(assert_type(api.is_named_tuple(obj), bool), bool)
check(assert_type(api.is_named_tuple(nparr), bool), bool)
check(assert_type(api.is_named_tuple(dtylike), bool), bool)
check(assert_type(api.is_named_tuple(arr), bool), bool)
check(
assert_type(api.is_named_tuple(dframe), bool),
bool,
)
check(assert_type(api.is_named_tuple(ind), bool), bool)
def test_is_number() -> None:
check(assert_type(api.is_number(obj), bool), bool)
check(assert_type(api.is_number(nparr), bool), bool)
check(assert_type(api.is_number(dtylike), bool), bool)
check(assert_type(api.is_number(arr), bool), bool)
check(assert_type(api.is_number(dframe), bool), bool)
check(assert_type(api.is_number(ind), bool), bool)
def test_is_numeric_dtype() -> None:
check(assert_type(api.is_numeric_dtype(arr), bool), bool)
check(assert_type(api.is_numeric_dtype(nparr), bool), bool)
check(assert_type(api.is_numeric_dtype(dtylike), bool), bool)
check(
assert_type(api.is_numeric_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_numeric_dtype(ind), bool), bool)
# check(assert_type(api.is_numeric_dtype(ExtensionDtype), bool), bool) pandas GH 50923
def test_is_object_dtype() -> None:
check(assert_type(api.is_object_dtype(arr), bool), bool)
check(assert_type(api.is_object_dtype(nparr), bool), bool)
check(assert_type(api.is_object_dtype(dtylike), bool), bool)
check(
assert_type(api.is_object_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_object_dtype(ind), bool), bool)
# check(assert_type(api.is_object_dtype(ExtensionDtype), bool), bool) pandas GH 50923
def test_is_re() -> None:
check(assert_type(api.is_re(obj), bool), bool)
check(assert_type(api.is_re(nparr), bool), bool)
check(assert_type(api.is_re(dtylike), bool), bool)
check(assert_type(api.is_re(arr), bool), bool)
check(assert_type(api.is_re(dframe), bool), bool)
check(assert_type(api.is_re(ind), bool), bool)
def test_is_re_compilable() -> None:
check(assert_type(api.is_re_compilable(obj), bool), bool)
check(assert_type(api.is_re_compilable(nparr), bool), bool)
check(assert_type(api.is_re_compilable(dtylike), bool), bool)
check(assert_type(api.is_re_compilable(arr), bool), bool)
check(
assert_type(api.is_re_compilable(dframe), bool),
bool,
)
check(assert_type(api.is_re_compilable(ind), bool), bool)
def test_is_scalar() -> None:
check(assert_type(api.is_scalar(obj), bool), bool)
check(assert_type(api.is_scalar(nparr), bool), bool)
check(assert_type(api.is_scalar(dtylike), bool), bool)
check(assert_type(api.is_scalar(arr), bool), bool)
check(assert_type(api.is_scalar(dframe), bool), bool)
check(assert_type(api.is_scalar(ind), bool), bool)
def test_is_signed_integer_dtype() -> None:
check(assert_type(api.is_signed_integer_dtype(arr), bool), bool)
check(assert_type(api.is_signed_integer_dtype(nparr), bool), bool)
check(assert_type(api.is_signed_integer_dtype(dtylike), bool), bool)
check(
assert_type(api.is_signed_integer_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_signed_integer_dtype(ind), bool), bool)
# check(assert_type(api.is_signed_integer_dtype(ExtensionDtype), bool), bool) pandas GH 50923
def test_is_string_dtype() -> None:
check(assert_type(api.is_string_dtype(arr), bool), bool)
check(assert_type(api.is_string_dtype(nparr), bool), bool)
check(assert_type(api.is_string_dtype(dtylike), bool), bool)
check(
assert_type(api.is_string_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_string_dtype(ind), bool), bool)
check(assert_type(api.is_string_dtype(ExtensionDtype), bool), bool)
def test_is_timedelta64_dtype() -> None:
check(assert_type(api.is_timedelta64_dtype(arr), bool), bool)
check(assert_type(api.is_timedelta64_dtype(nparr), bool), bool)
check(assert_type(api.is_timedelta64_dtype(dtylike), bool), bool)
check(
assert_type(api.is_timedelta64_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_timedelta64_dtype(ind), bool), bool)
# check(assert_type(api.is_timedelta64_dtype(ExtensionDtype), bool), bool) pandas GH 50923
def test_is_timedelta64_ns_dtype() -> None:
check(assert_type(api.is_timedelta64_ns_dtype(arr), bool), bool)
check(assert_type(api.is_timedelta64_ns_dtype(nparr), bool), bool)
check(assert_type(api.is_timedelta64_ns_dtype(dtylike), bool), bool)
check(
assert_type(api.is_timedelta64_ns_dtype(dframe), bool),
bool,
)
check(assert_type(api.is_timedelta64_ns_dtype(ind), bool), bool)
check(assert_type(api.is_timedelta64_ns_dtype(ExtensionDtype), bool), bool)
def test_is_unsigned_integer_dtype() -> None:
check(assert_type(api.is_unsigned_integer_dtype(arr), bool), bool)
check(assert_type(api.is_unsigned_integer_dtype(nparr), bool), bool)
check(assert_type(api.is_unsigned_integer_dtype(dtylike), bool), bool)
check(
assert_type(
api.is_unsigned_integer_dtype(dframe),
bool,
),
bool,
)
check(assert_type(api.is_unsigned_integer_dtype(ind), bool), bool)
# check(assert_type(api.is_unsigned_integer_dtype(ExtensionDtype), bool), bool) pandas GH 50923
def test_pandas_dtype() -> None:
check(assert_type(api.pandas_dtype(arr), DtypeObj), type(np.dtype("i8")))
def test_infer_dtype() -> None:
check(assert_type(api.infer_dtype([1, 2, 3]), str), str)
def test_union_categoricals() -> None:
to_union = [pd.Categorical([1, 2, 3]), pd.Categorical([3, 4, 5])]
check(assert_type(api.union_categoricals(to_union), pd.Categorical), pd.Categorical)
def test_check_extension_dtypes() -> None:
# GH 315
def check_ext_dtype(etype: type[ExtensionDtype]):
assert issubclass(etype, ExtensionDtype)
check_ext_dtype(pd.Int64Dtype)
check_ext_dtype(pd.Int8Dtype)
check_ext_dtype(pd.Int16Dtype)
check_ext_dtype(pd.Int32Dtype)
check_ext_dtype(pd.Int64Dtype)
check_ext_dtype(pd.UInt8Dtype)
check_ext_dtype(pd.UInt16Dtype)
check_ext_dtype(pd.UInt32Dtype)
check_ext_dtype(pd.UInt64Dtype)
check_ext_dtype(pd.BooleanDtype)
check_ext_dtype(pd.StringDtype)
check_ext_dtype(pd.CategoricalDtype)
check_ext_dtype(pd.DatetimeTZDtype)
check_ext_dtype(pd.IntervalDtype)
check_ext_dtype(pd.PeriodDtype)
check_ext_dtype(pd.SparseDtype)
check_ext_dtype(pd.Float32Dtype)
check_ext_dtype(pd.Float64Dtype)
def test_from_dataframe() -> None:
# GH 712
check(
assert_type(from_dataframe(dframe), pd.DataFrame),
pd.DataFrame,
)