@@ -146,20 +146,20 @@ def test_types_all() -> None:
146
146
147
147
def test_types_csv () -> None :
148
148
s = pd .Series (data = [1 , 2 , 3 ])
149
- csv_df : str = s .to_csv ()
149
+ check ( assert_type ( s .to_csv (), str ), str )
150
150
151
151
with ensure_clean () as path :
152
152
s .to_csv (path )
153
- s2 : pd .DataFrame = pd .read_csv ( path )
153
+ check ( assert_type ( pd . read_csv ( path ), pd .DataFrame ), pd .DataFrame )
154
154
155
155
with ensure_clean () as path :
156
156
s .to_csv (Path (path ))
157
- s3 : pd .DataFrame = pd . read_csv (Path (path ))
157
+ check ( assert_type ( pd .read_csv (Path (path )), pd . DataFrame ), pd . DataFrame )
158
158
159
159
# This keyword was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
160
160
with ensure_clean () as path :
161
161
s .to_csv (path , errors = "replace" )
162
- s4 : pd .DataFrame = pd .read_csv ( path )
162
+ check ( assert_type ( pd . read_csv ( path ), pd .DataFrame ), pd .DataFrame )
163
163
164
164
165
165
def test_types_copy () -> None :
@@ -177,7 +177,7 @@ def test_types_select() -> None:
177
177
lower = "2.0.99" ,
178
178
):
179
179
s [0 ]
180
- s [1 :]
180
+ check ( assert_type ( s [1 :], "pd.Series[int]" ), pd . Series , np . integer )
181
181
182
182
183
183
def test_types_iloc_iat () -> None :
@@ -230,11 +230,11 @@ def test_types_boolean_indexing() -> None:
230
230
def test_types_df_to_df_comparison () -> None :
231
231
s = pd .Series (data = {"col1" : [1 , 2 ]})
232
232
s2 = pd .Series (data = {"col1" : [3 , 2 ]})
233
- res_gt : pd .Series = s > s2
234
- res_ge : pd . Series = s >= s2
235
- res_lt : pd .Series = s < s2
236
- res_le : pd . Series = s <= s2
237
- res_e : pd . Series = s == s2
233
+ check ( assert_type ( s > s2 , " pd.Series[bool]" ), pd . Series , np . bool )
234
+ check ( assert_type ( s >= s2 , "pd.Series[bool]" ), pd . Series , np . bool )
235
+ check ( assert_type ( s < s2 , " pd.Series[bool]" ), pd . Series , np . bool )
236
+ check ( assert_type ( s <= s2 , "pd.Series[bool]" ), pd . Series , np . bool )
237
+ check ( assert_type ( s == s2 , "pd.Series[bool]" ), pd . Series , np . bool )
238
238
239
239
240
240
def test_types_head_tail () -> None :
@@ -310,7 +310,11 @@ def test_types_drop_multilevel() -> None:
310
310
codes = [[0 , 0 , 0 , 1 , 1 , 1 ], [0 , 1 , 2 , 0 , 1 , 2 ]],
311
311
)
312
312
s = pd .Series (data = [1 , 2 , 3 , 4 , 5 , 6 ], index = index )
313
- res : pd .Series = s .drop (labels = "first" , level = 1 )
313
+ check (
314
+ assert_type (s .drop (labels = "first" , level = 1 ), "pd.Series[int]" ),
315
+ pd .Series ,
316
+ np .integer ,
317
+ )
314
318
315
319
316
320
def test_types_drop_duplicates () -> None :
@@ -383,7 +387,11 @@ def test_types_sort_index() -> None:
383
387
# This was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
384
388
def test_types_sort_index_with_key () -> None :
385
389
s = pd .Series ([1 , 2 , 3 ], index = ["a" , "B" , "c" ])
386
- res : pd .Series = s .sort_index (key = lambda k : k .str .lower ())
390
+ check (
391
+ assert_type (s .sort_index (key = lambda k : k .str .lower ()), "pd.Series[int]" ),
392
+ pd .Series ,
393
+ np .integer ,
394
+ )
387
395
388
396
389
397
def test_types_sort_values () -> None :
@@ -413,7 +421,11 @@ def test_types_sort_values() -> None:
413
421
# This was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
414
422
def test_types_sort_values_with_key () -> None :
415
423
s = pd .Series ([1 , 2 , 3 ], index = [2 , 3 , 1 ])
416
- res : pd .Series = s .sort_values (key = lambda k : - k )
424
+ check (
425
+ assert_type (s .sort_values (key = lambda k : - k ), "pd.Series[int]" ),
426
+ pd .Series ,
427
+ np .integer ,
428
+ )
417
429
418
430
419
431
def test_types_shift () -> None :
@@ -441,18 +453,26 @@ def test_types_rank() -> None:
441
453
442
454
def test_types_mean () -> None :
443
455
s = pd .Series ([1 , 2 , 3 , np .nan ])
444
- f1 : float = s .mean ()
445
- s1 : pd .Series = s .groupby (level = 0 ).mean ()
446
- f2 : float = s .mean (skipna = False )
447
- f3 : float = s .mean (numeric_only = False )
456
+ check (assert_type (s .mean (), float ), float )
457
+ check (
458
+ assert_type (s .groupby (level = 0 ).mean (), "pd.Series[float]" ),
459
+ pd .Series ,
460
+ np .float64 ,
461
+ )
462
+ check (assert_type (s .mean (skipna = False ), float ), float )
463
+ check (assert_type (s .mean (numeric_only = False ), float ), float )
448
464
449
465
450
466
def test_types_median () -> None :
451
467
s = pd .Series ([1 , 2 , 3 , np .nan ])
452
- f1 : float = s .median ()
453
- s1 : pd .Series = s .groupby (level = 0 ).median ()
454
- f2 : float = s .median (skipna = False )
455
- f3 : float = s .median (numeric_only = False )
468
+ check (assert_type (s .median (), float ), float )
469
+ check (
470
+ assert_type (s .groupby (level = 0 ).median (), "pd.Series[float]" ),
471
+ pd .Series ,
472
+ np .float64 ,
473
+ )
474
+ check (assert_type (s .median (skipna = False ), float ), float )
475
+ check (assert_type (s .median (numeric_only = False ), float ), float )
456
476
457
477
458
478
def test_types_sum () -> None :
@@ -630,17 +650,25 @@ def test_types_element_wise_arithmetic() -> None:
630
650
s = pd .Series ([0 , 1 , - 10 ])
631
651
s2 = pd .Series ([7 , - 5 , 10 ])
632
652
633
- res_add1 : pd .Series = s + s2
634
- res_add2 : pd . Series = s .add (s2 , fill_value = 0 )
653
+ check ( assert_type ( s + s2 , " pd.Series[int]" ), pd . Series , np . integer )
654
+ check ( assert_type ( s .add (s2 , fill_value = 0 ), "pd.Series[int]" ), pd . Series , np . integer )
635
655
636
- res_sub : pd .Series = s - s2
637
- res_sub2 : pd .Series = s .sub (s2 , fill_value = 0 )
656
+ # TODO this one below should type pd.Series[int]
657
+ check (assert_type (s - s2 , pd .Series ), pd .Series , np .integer )
658
+ check (assert_type (s .sub (s2 , fill_value = 0 ), "pd.Series[int]" ), pd .Series , np .integer )
638
659
639
- res_mul : pd .Series = s * s2
640
- res_mul2 : pd .Series = s .mul (s2 , fill_value = 0 )
660
+ # TODO these two below should type pd.Series[int]
661
+ # check(assert_type(s * s2, "pd.Series[int]"), pd.Series, np.integer )
662
+ check (assert_type (s * s2 , pd .Series ), pd .Series , np .integer )
663
+ # check(assert_type(s.mul(s2, fill_value=0), "pd.Series[int]"), pd.Series, np.integer)
664
+ check (assert_type (s .mul (s2 , fill_value = 0 ), pd .Series ), pd .Series , np .integer )
641
665
642
- res_div : pd .Series = s / s2
643
- res_div2 : pd .Series = s .div (s2 , fill_value = 0 )
666
+ # TODO these two below should type pd.Series[float]
667
+ # check(assert_type(s / s2, "pd.Series[float]"), pd.Series, np.float64)
668
+ check (assert_type (s / s2 , pd .Series ), pd .Series , np .float64 )
669
+ check (
670
+ assert_type (s .div (s2 , fill_value = 0 ), "pd.Series[float]" ), pd .Series , np .float64
671
+ )
644
672
645
673
res_floordiv : pd .Series = s // s2
646
674
res_floordiv2 : pd .Series = s .floordiv (s2 , fill_value = 0 )
@@ -657,8 +685,8 @@ def test_types_element_wise_arithmetic() -> None:
657
685
def test_types_scalar_arithmetic () -> None :
658
686
s = pd .Series ([0 , 1 , - 10 ])
659
687
660
- res_add1 : pd .Series = s + 1
661
- res_add2 : pd . Series = s .add (1 , fill_value = 0 )
688
+ check ( assert_type ( s + 1 , " pd.Series[int]" ), pd . Series , np . integer )
689
+ check ( assert_type ( s .add (1 , fill_value = 0 ), "pd.Series[int]" ), pd . Series , np . integer )
662
690
663
691
res_sub : pd .Series = s - 1
664
692
res_sub2 : pd .Series = s .sub (1 , fill_value = 0 )
@@ -681,8 +709,8 @@ def test_types_scalar_arithmetic() -> None:
681
709
res_pow3 : pd .Series = s .pow (0.5 )
682
710
683
711
684
- # GH 103
685
712
def test_types_complex_arithmetic () -> None :
713
+ # GH 103
686
714
c = 1 + 1j
687
715
s = pd .Series ([1.0 , 2.0 , 3.0 ])
688
716
x = s + c
@@ -1111,8 +1139,8 @@ def test_types_getitem() -> None:
1111
1139
s = pd .Series ({"key" : [0 , 1 , 2 , 3 ]})
1112
1140
key : list [int ] = s ["key" ]
1113
1141
s2 = pd .Series ([0 , 1 , 2 , 3 ])
1114
- value : int = s2 [0 ]
1115
- s3 : pd .Series = s [: 2 ]
1142
+ check ( assert_type ( s2 [0 ], int ), np . integer )
1143
+ check ( assert_type ( s [: 2 ], pd .Series ), pd . Series )
1116
1144
1117
1145
1118
1146
def test_types_getitem_by_timestamp () -> None :
@@ -1123,9 +1151,9 @@ def test_types_getitem_by_timestamp() -> None:
1123
1151
1124
1152
def test_types_eq () -> None :
1125
1153
s1 = pd .Series ([1 , 2 , 3 ])
1126
- res1 : pd . Series = s1 == 1
1154
+ check ( assert_type ( s1 == 1 , "pd.Series[bool]" ), pd . Series , np . bool )
1127
1155
s2 = pd .Series ([1 , 2 , 4 ])
1128
- res2 : pd . Series = s1 == s2
1156
+ check ( assert_type ( s1 == s2 , "pd.Series[bool]" ), pd . Series , np . bool )
1129
1157
1130
1158
1131
1159
def test_types_rename_axis () -> None :
@@ -1183,6 +1211,7 @@ def add1(x: int) -> int:
1183
1211
s5 = pd .Series ([1 , 2 , 3 ]).rename ({1 : 10 })
1184
1212
check (assert_type (s5 , "pd.Series[int]" ), pd .Series , np .integer )
1185
1213
# inplace
1214
+ # TODO fix issue with inplace=True returning a Series, cf pandas #60942
1186
1215
s6 : None = pd .Series ([1 , 2 , 3 ]).rename ("A" , inplace = True )
1187
1216
1188
1217
if TYPE_CHECKING_INVALID_USAGE :
@@ -1192,7 +1221,7 @@ def add1(x: int) -> int:
1192
1221
def test_types_ne () -> None :
1193
1222
s1 = pd .Series ([1 , 2 , 3 ])
1194
1223
s2 = pd .Series ([1 , 2 , 4 ])
1195
- s3 : pd . Series = s1 != s2
1224
+ check ( assert_type ( s1 != s2 , "pd.Series[bool]" ), pd . Series , np . bool )
1196
1225
1197
1226
1198
1227
def test_types_bfill () -> None :
@@ -1261,7 +1290,7 @@ def test_types_ffill() -> None:
1261
1290
1262
1291
def test_types_as_type () -> None :
1263
1292
s1 = pd .Series ([1 , 2 , 8 , 9 ])
1264
- s2 : pd .Series = s1 . astype ( " int32" )
1293
+ check ( assert_type ( s1 . astype ( "int32" ), " pd.Series[int]" ), pd . Series , np . int32 )
1265
1294
1266
1295
1267
1296
def test_types_dot () -> None :
@@ -1414,13 +1443,19 @@ def test_cat_accessor() -> None:
1414
1443
1415
1444
1416
1445
def test_cat_ctor_values () -> None :
1417
- c1 = pd .Categorical (["a" , "b" , "a" ])
1446
+ check ( assert_type ( pd .Categorical (["a" , "b" , "a" ]), pd . Categorical ), pd . Categorical )
1418
1447
# GH 95
1419
- c2 = pd .Categorical (pd .Series (["a" , "b" , "a" ]))
1448
+ check (
1449
+ assert_type (pd .Categorical (pd .Series (["a" , "b" , "a" ])), pd .Categorical ),
1450
+ pd .Categorical ,
1451
+ )
1420
1452
s : Sequence = cast (Sequence , ["a" , "b" , "a" ])
1421
- c3 = pd .Categorical (s )
1453
+ check ( assert_type ( pd .Categorical (s ), pd . Categorical ), pd . Categorical )
1422
1454
# GH 107
1423
- c4 = pd .Categorical (np .array ([1 , 2 , 3 , 1 , 1 ]))
1455
+ check (
1456
+ assert_type (pd .Categorical (np .array ([1 , 2 , 3 , 1 , 1 ])), pd .Categorical ),
1457
+ pd .Categorical ,
1458
+ )
1424
1459
1425
1460
1426
1461
def test_iloc_getitem_ndarray () -> None :
@@ -1478,8 +1513,8 @@ def test_iloc_setitem_ndarray() -> None:
1478
1513
def test_types_iter () -> None :
1479
1514
s = pd .Series ([1 , 2 , 3 ], dtype = int )
1480
1515
iterable : Iterable [int ] = s
1481
- assert_type (iter (s ), Iterator [int ])
1482
- assert_type (next (iter (s )), int )
1516
+ check ( assert_type (iter (s ), Iterator [int ]), Iterator , int )
1517
+ check ( assert_type (next (iter (s )), int ), int )
1483
1518
1484
1519
1485
1520
def test_types_to_list () -> None :
@@ -2707,12 +2742,12 @@ def test_astype_bytes(cast_arg: BytesDtypeArg, target_type: type) -> None:
2707
2742
@pytest .mark .parametrize ("cast_arg, target_type" , ASTYPE_CATEGORICAL_ARGS , ids = repr )
2708
2743
def test_astype_categorical (cast_arg : CategoryDtypeArg , target_type : type ) -> None :
2709
2744
s = pd .Series (["a" , "b" ])
2710
- check (s .astype ("category" ), pd .Series , target_type )
2745
+ check (s .astype (cast_arg ), pd .Series , target_type )
2711
2746
2712
2747
if TYPE_CHECKING :
2713
2748
# pandas category
2714
2749
assert_type (s .astype (pd .CategoricalDtype ()), "pd.Series[pd.CategoricalDtype]" )
2715
- assert_type (s .astype ("category" ), "pd.Series[pd.CategoricalDtype]" )
2750
+ assert_type (s .astype (cast_arg ), "pd.Series[pd.CategoricalDtype]" )
2716
2751
# pyarrow dictionary
2717
2752
# assert_type(s.astype("dictionary[pyarrow]"), "pd.Series[Categorical]")
2718
2753
0 commit comments