@@ -145,20 +145,20 @@ def test_types_all() -> None:
145
145
146
146
def test_types_csv () -> None :
147
147
s = pd .Series (data = [1 , 2 , 3 ])
148
- csv_df : str = s .to_csv ()
148
+ check ( assert_type ( s .to_csv (), str ), str )
149
149
150
150
with ensure_clean () as path :
151
151
s .to_csv (path )
152
- s2 : pd .DataFrame = pd .read_csv ( path )
152
+ check ( assert_type ( pd . read_csv ( path ), pd .DataFrame ), pd .DataFrame )
153
153
154
154
with ensure_clean () as path :
155
155
s .to_csv (Path (path ))
156
- s3 : pd .DataFrame = pd . read_csv (Path (path ))
156
+ check ( assert_type ( pd .read_csv (Path (path )), pd . DataFrame ), pd . DataFrame )
157
157
158
158
# This keyword was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
159
159
with ensure_clean () as path :
160
160
s .to_csv (path , errors = "replace" )
161
- s4 : pd .DataFrame = pd .read_csv ( path )
161
+ check ( assert_type ( pd . read_csv ( path ), pd .DataFrame ), pd .DataFrame )
162
162
163
163
164
164
def test_types_copy () -> None :
@@ -229,11 +229,11 @@ def test_types_boolean_indexing() -> None:
229
229
def test_types_df_to_df_comparison () -> None :
230
230
s = pd .Series (data = {"col1" : [1 , 2 ]})
231
231
s2 = pd .Series (data = {"col1" : [3 , 2 ]})
232
- res_gt : pd .Series = s > s2
233
- res_ge : pd . Series = s >= s2
234
- res_lt : pd .Series = s < s2
235
- res_le : pd . Series = s <= s2
236
- res_e : pd . Series = s == s2
232
+ check ( assert_type ( s > s2 , " pd.Series[bool]" ), pd . Series , np . bool_ )
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
237
238
238
239
239
def test_types_head_tail () -> None :
@@ -309,7 +309,11 @@ def test_types_drop_multilevel() -> None:
309
309
codes = [[0 , 0 , 0 , 1 , 1 , 1 ], [0 , 1 , 2 , 0 , 1 , 2 ]],
310
310
)
311
311
s = pd .Series (data = [1 , 2 , 3 , 4 , 5 , 6 ], index = index )
312
- res : pd .Series = s .drop (labels = "first" , level = 1 )
312
+ check (
313
+ assert_type (s .drop (labels = "first" , level = 1 ), "pd.Series[int]" ),
314
+ pd .Series ,
315
+ np .int64 ,
316
+ )
313
317
314
318
315
319
def test_types_drop_duplicates () -> None :
@@ -382,7 +386,11 @@ def test_types_sort_index() -> None:
382
386
# This was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
383
387
def test_types_sort_index_with_key () -> None :
384
388
s = pd .Series ([1 , 2 , 3 ], index = ["a" , "B" , "c" ])
385
- res : pd .Series = s .sort_index (key = lambda k : k .str .lower ())
389
+ check (
390
+ assert_type (s .sort_index (key = lambda k : k .str .lower ()), "pd.Series[int]" ),
391
+ pd .Series ,
392
+ np .int64 ,
393
+ )
386
394
387
395
388
396
def test_types_sort_values () -> None :
@@ -412,7 +420,11 @@ def test_types_sort_values() -> None:
412
420
# This was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
413
421
def test_types_sort_values_with_key () -> None :
414
422
s = pd .Series ([1 , 2 , 3 ], index = [2 , 3 , 1 ])
415
- res : pd .Series = s .sort_values (key = lambda k : - k )
423
+ check (
424
+ assert_type (s .sort_values (key = lambda k : - k ), "pd.Series[int]" ),
425
+ pd .Series ,
426
+ np .integer ,
427
+ )
416
428
417
429
418
430
def test_types_shift () -> None :
@@ -435,18 +447,32 @@ def test_types_rank() -> None:
435
447
436
448
def test_types_mean () -> None :
437
449
s = pd .Series ([1 , 2 , 3 , np .nan ])
438
- f1 : float = s .mean ()
439
- s1 : pd .Series = s .groupby (level = 0 ).mean ()
440
- f2 : float = s .mean (skipna = False )
441
- f3 : float = s .mean (numeric_only = False )
450
+ check (assert_type (s .mean (), float ), float )
451
+ check (
452
+ assert_type (
453
+ s .groupby (level = 0 ).mean (), # pyright: ignore[reportAssertTypeFailure]
454
+ "pd.Series[float]" ,
455
+ ),
456
+ pd .Series ,
457
+ float ,
458
+ )
459
+ check (assert_type (s .mean (skipna = False ), float ), float )
460
+ check (assert_type (s .mean (numeric_only = False ), float ), float )
442
461
443
462
444
463
def test_types_median () -> None :
445
464
s = pd .Series ([1 , 2 , 3 , np .nan ])
446
- f1 : float = s .median ()
447
- s1 : pd .Series = s .groupby (level = 0 ).median ()
448
- f2 : float = s .median (skipna = False )
449
- f3 : float = s .median (numeric_only = False )
465
+ check (assert_type (s .median (), float ), float )
466
+ check (
467
+ assert_type (
468
+ s .groupby (level = 0 ).median (), # pyright: ignore[reportAssertTypeFailure]
469
+ "pd.Series[float]" ,
470
+ ),
471
+ pd .Series ,
472
+ float ,
473
+ )
474
+ check (assert_type (s .median (skipna = False ), float ), float )
475
+ check (assert_type (s .median (numeric_only = False ), float ), float )
450
476
451
477
452
478
def test_types_sum () -> None :
@@ -624,63 +650,79 @@ def test_types_element_wise_arithmetic() -> None:
624
650
s = pd .Series ([0 , 1 , - 10 ])
625
651
s2 = pd .Series ([7 , - 5 , 10 ])
626
652
627
- res_add1 : pd .Series = s + s2
628
- 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 )
629
655
630
- res_sub : pd .Series = s - s2
631
- res_sub2 : pd . Series = s .sub (s2 , fill_value = 0 )
656
+ check ( assert_type ( s - s2 , pd .Series ), pd . Series , np . integer )
657
+ check ( assert_type ( s .sub (s2 , fill_value = 0 ), "pd.Series[int]" ), pd . Series , np . integer )
632
658
633
- res_mul : pd .Series = s * s2
634
- res_mul2 : pd . Series = s .mul (s2 , fill_value = 0 )
659
+ check ( assert_type ( s * s2 , pd .Series ), pd . Series , np . integer )
660
+ check ( assert_type ( s .mul (s2 , fill_value = 0 ), pd . Series ), pd . Series , np . integer )
635
661
636
- res_div : pd .Series = s / s2
637
- res_div2 : pd .Series = s .div (s2 , fill_value = 0 )
662
+ check (assert_type (s / s2 , pd .Series ), pd .Series , np .float64 )
663
+ check (
664
+ assert_type (s .div (s2 , fill_value = 0 ), "pd.Series[float]" ), pd .Series , np .float64
665
+ )
638
666
639
- res_floordiv : pd .Series = s // s2
640
- res_floordiv2 : pd .Series = s .floordiv (s2 , fill_value = 0 )
667
+ check (assert_type (s // s2 , "pd.Series[int]" ), pd .Series , np .integer )
668
+ check (
669
+ assert_type (s .floordiv (s2 , fill_value = 0 ), "pd.Series[int]" ),
670
+ pd .Series ,
671
+ np .integer ,
672
+ )
641
673
642
- res_mod : pd .Series = s % s2
643
- res_mod2 : pd . Series = s .mod (s2 , fill_value = 0 )
674
+ check ( assert_type ( s % s2 , " pd.Series[int]" ), pd . Series , np . integer )
675
+ check ( assert_type ( s .mod (s2 , fill_value = 0 ), "pd.Series[int]" ), pd . Series , np . integer )
644
676
645
- res_pow : pd .Series = s ** s2 .abs ()
646
- res_pow2 : pd .Series = s .pow (s2 .abs (), fill_value = 0 )
677
+ check (assert_type (s ** s2 .abs (), "pd.Series[int]" ), pd .Series , np .integer )
678
+ check (
679
+ assert_type (s .pow (s2 .abs (), fill_value = 0 ), "pd.Series[int]" ),
680
+ pd .Series ,
681
+ np .integer ,
682
+ )
647
683
648
684
check (assert_type (divmod (s , s2 ), tuple ["pd.Series[int]" , "pd.Series[int]" ]), tuple )
649
685
650
686
651
687
def test_types_scalar_arithmetic () -> None :
652
688
s = pd .Series ([0 , 1 , - 10 ])
653
689
654
- res_add1 : pd .Series = s + 1
655
- res_add2 : pd . Series = s .add (1 , fill_value = 0 )
690
+ check ( assert_type ( s + 1 , " pd.Series[int]" ), pd . Series , np . integer )
691
+ check ( assert_type ( s .add (1 , fill_value = 0 ), "pd.Series[int]" ), pd . Series , np . integer )
656
692
657
- res_sub : pd .Series = s - 1
658
- res_sub2 : pd . Series = s .sub (1 , fill_value = 0 )
693
+ check ( assert_type ( s - 1 , pd .Series ), pd . Series , np . integer )
694
+ check ( assert_type ( s .sub (1 , fill_value = 0 ), "pd.Series[int]" ), pd . Series , np . integer )
659
695
660
- res_mul : pd .Series = s * 2
661
- res_mul2 : pd . Series = s .mul (2 , fill_value = 0 )
696
+ check ( assert_type ( s * 2 , pd .Series ), pd . Series , np . integer )
697
+ check ( assert_type ( s .mul (2 , fill_value = 0 ), pd . Series ), pd . Series , np . integer )
662
698
663
- res_div : pd .Series = s / 2
664
- res_div2 : pd .Series = s .div (2 , fill_value = 0 )
699
+ check (assert_type (s / 2 , pd .Series ), pd .Series , np .float64 )
700
+ check (
701
+ assert_type (s .div (2 , fill_value = 0 ), "pd.Series[float]" ), pd .Series , np .float64
702
+ )
665
703
666
- res_floordiv : pd .Series = s // 2
667
- res_floordiv2 : pd .Series = s .floordiv (2 , fill_value = 0 )
704
+ check (assert_type (s // 2 , "pd.Series[int]" ), pd .Series , np .integer )
705
+ check (
706
+ assert_type (s .floordiv (2 , fill_value = 0 ), "pd.Series[int]" ),
707
+ pd .Series ,
708
+ np .integer ,
709
+ )
668
710
669
- res_mod : pd .Series = s % 2
670
- res_mod2 : pd . Series = s .mod (2 , fill_value = 0 )
711
+ check ( assert_type ( s % 2 , " pd.Series[int]" ), pd . Series , np . integer )
712
+ check ( assert_type ( s .mod (2 , fill_value = 0 ), "pd.Series[int]" ), pd . Series , np . integer )
671
713
672
- res_pow : pd .Series = s ** 2
673
- res_pow1 : pd .Series = s ** 0
674
- res_pow2 : pd .Series = s ** 0.213
675
- res_pow3 : pd . Series = s .pow (0.5 )
714
+ check ( assert_type ( s ** 2 , " pd.Series[int]" ), pd . Series , np . integer )
715
+ check ( assert_type ( s ** 0 , " pd.Series[int]" ), pd . Series , np . integer )
716
+ check ( assert_type ( s ** 0.213 , " pd.Series[int]" ), pd . Series , np . float64 )
717
+ check ( assert_type ( s .pow (0.5 ), "pd.Series[int]" ), pd . Series , np . float64 )
676
718
677
719
678
720
# GH 103
679
721
def test_types_complex_arithmetic () -> None :
680
722
c = 1 + 1j
681
723
s = pd .Series ([1.0 , 2.0 , 3.0 ])
682
- x = s + c
683
- y = s - c
724
+ check ( assert_type ( s + c , pd . Series ), pd . Series )
725
+ check ( assert_type ( s - c , pd . Series ), pd . Series )
684
726
685
727
686
728
def test_types_groupby () -> None :
@@ -1105,8 +1147,8 @@ def test_types_getitem() -> None:
1105
1147
s = pd .Series ({"key" : [0 , 1 , 2 , 3 ]})
1106
1148
key : list [int ] = s ["key" ]
1107
1149
s2 = pd .Series ([0 , 1 , 2 , 3 ])
1108
- value : int = s2 [0 ]
1109
- s3 : pd .Series = s [: 2 ]
1150
+ check ( assert_type ( s2 [0 ], int ), np . integer )
1151
+ check ( assert_type ( s [: 2 ], pd .Series ), pd . Series )
1110
1152
1111
1153
1112
1154
def test_types_getitem_by_timestamp () -> None :
@@ -1117,9 +1159,9 @@ def test_types_getitem_by_timestamp() -> None:
1117
1159
1118
1160
def test_types_eq () -> None :
1119
1161
s1 = pd .Series ([1 , 2 , 3 ])
1120
- res1 : pd . Series = s1 == 1
1162
+ check ( assert_type ( s1 == 1 , "pd.Series[bool]" ), pd . Series , np . bool_ )
1121
1163
s2 = pd .Series ([1 , 2 , 4 ])
1122
- res2 : pd . Series = s1 == s2
1164
+ check ( assert_type ( s1 == s2 , "pd.Series[bool]" ), pd . Series , np . bool_ )
1123
1165
1124
1166
1125
1167
def test_types_rename_axis () -> None :
@@ -1317,7 +1359,7 @@ def test_series_multiindex_getitem() -> None:
1317
1359
s = pd .Series (
1318
1360
[1 , 2 , 3 , 4 ], index = pd .MultiIndex .from_product ([["a" , "b" ], ["x" , "y" ]])
1319
1361
)
1320
- s1 : pd . Series = s ["a" , :]
1362
+ check ( assert_type ( s ["a" , :], "pd.Series[int]" ), pd . Series , np . integer )
1321
1363
1322
1364
1323
1365
def test_series_mul () -> None :
@@ -1408,13 +1450,19 @@ def test_cat_accessor() -> None:
1408
1450
1409
1451
1410
1452
def test_cat_ctor_values () -> None :
1411
- c1 = pd .Categorical (["a" , "b" , "a" ])
1453
+ check ( assert_type ( pd .Categorical (["a" , "b" , "a" ]), pd . Categorical ), pd . Categorical )
1412
1454
# GH 95
1413
- c2 = pd .Categorical (pd .Series (["a" , "b" , "a" ]))
1455
+ check (
1456
+ assert_type (pd .Categorical (pd .Series (["a" , "b" , "a" ])), pd .Categorical ),
1457
+ pd .Categorical ,
1458
+ )
1414
1459
s : Sequence = cast (Sequence , ["a" , "b" , "a" ])
1415
- c3 = pd .Categorical (s )
1460
+ check ( assert_type ( pd .Categorical (s ), pd . Categorical ), pd . Categorical )
1416
1461
# GH 107
1417
- c4 = pd .Categorical (np .array ([1 , 2 , 3 , 1 , 1 ]))
1462
+ check (
1463
+ assert_type (pd .Categorical (np .array ([1 , 2 , 3 , 1 , 1 ])), pd .Categorical ),
1464
+ pd .Categorical ,
1465
+ )
1418
1466
1419
1467
1420
1468
def test_iloc_getitem_ndarray () -> None :
@@ -2768,7 +2816,7 @@ def test_astype_other() -> None:
2768
2816
2769
2817
def test_all_astype_args_tested () -> None :
2770
2818
"""Check that all relevant numpy type aliases are tested."""
2771
- NUMPY_ALIASES : set [str ] = {k for k in np .sctypeDict }
2819
+ NUMPY_ALIASES : set [str | int ] = {k for k in np .sctypeDict }
2772
2820
EXCLUDED_ALIASES = {
2773
2821
"datetime64" ,
2774
2822
"m" ,
@@ -2889,7 +2937,7 @@ def test_convert_dtypes_dtype_backend() -> None:
2889
2937
def test_apply_returns_none () -> None :
2890
2938
# GH 557
2891
2939
s = pd .Series ([1 , 2 , 3 ])
2892
- check (assert_type (s .apply (lambda x : None ), pd .Series ), pd .Series )
2940
+ check (assert_type (s .apply (lambda _ : None ), pd .Series ), pd .Series )
2893
2941
2894
2942
2895
2943
def test_loc_callable () -> None :
0 commit comments