@@ -146,20 +146,20 @@ def test_types_all() -> None:
146146
147147def test_types_csv () -> None :
148148 s = pd .Series (data = [1 , 2 , 3 ])
149- csv_df : str = s .to_csv ()
149+ check ( assert_type ( s .to_csv (), str ), str )
150150
151151 with ensure_clean () as path :
152152 s .to_csv (path )
153- s2 : pd .DataFrame = pd .read_csv ( path )
153+ check ( assert_type ( pd . read_csv ( path ), pd .DataFrame ), pd .DataFrame )
154154
155155 with ensure_clean () as path :
156156 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 )
158158
159159 # This keyword was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
160160 with ensure_clean () as path :
161161 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 )
163163
164164
165165def test_types_copy () -> None :
@@ -177,7 +177,7 @@ def test_types_select() -> None:
177177 lower = "2.0.99" ,
178178 ):
179179 s [0 ]
180- s [1 :]
180+ check ( assert_type ( s [1 :], "pd.Series[int]" ), pd . Series , np . integer )
181181
182182
183183def test_types_iloc_iat () -> None :
@@ -230,11 +230,11 @@ def test_types_boolean_indexing() -> None:
230230def test_types_df_to_df_comparison () -> None :
231231 s = pd .Series (data = {"col1" : [1 , 2 ]})
232232 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 )
238238
239239
240240def test_types_head_tail () -> None :
@@ -310,7 +310,11 @@ def test_types_drop_multilevel() -> None:
310310 codes = [[0 , 0 , 0 , 1 , 1 , 1 ], [0 , 1 , 2 , 0 , 1 , 2 ]],
311311 )
312312 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+ )
314318
315319
316320def test_types_drop_duplicates () -> None :
@@ -383,7 +387,11 @@ def test_types_sort_index() -> None:
383387# This was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
384388def test_types_sort_index_with_key () -> None :
385389 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+ )
387395
388396
389397def test_types_sort_values () -> None :
@@ -413,7 +421,11 @@ def test_types_sort_values() -> None:
413421# This was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
414422def test_types_sort_values_with_key () -> None :
415423 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+ )
417429
418430
419431def test_types_shift () -> None :
@@ -441,18 +453,26 @@ def test_types_rank() -> None:
441453
442454def test_types_mean () -> None :
443455 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 )
448464
449465
450466def test_types_median () -> None :
451467 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 )
456476
457477
458478def test_types_sum () -> None :
@@ -630,17 +650,25 @@ def test_types_element_wise_arithmetic() -> None:
630650 s = pd .Series ([0 , 1 , - 10 ])
631651 s2 = pd .Series ([7 , - 5 , 10 ])
632652
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 )
635655
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 )
638659
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 )
641665
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+ )
644672
645673 res_floordiv : pd .Series = s // s2
646674 res_floordiv2 : pd .Series = s .floordiv (s2 , fill_value = 0 )
@@ -657,8 +685,8 @@ def test_types_element_wise_arithmetic() -> None:
657685def test_types_scalar_arithmetic () -> None :
658686 s = pd .Series ([0 , 1 , - 10 ])
659687
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 )
662690
663691 res_sub : pd .Series = s - 1
664692 res_sub2 : pd .Series = s .sub (1 , fill_value = 0 )
@@ -681,8 +709,8 @@ def test_types_scalar_arithmetic() -> None:
681709 res_pow3 : pd .Series = s .pow (0.5 )
682710
683711
684- # GH 103
685712def test_types_complex_arithmetic () -> None :
713+ # GH 103
686714 c = 1 + 1j
687715 s = pd .Series ([1.0 , 2.0 , 3.0 ])
688716 x = s + c
@@ -1111,8 +1139,8 @@ def test_types_getitem() -> None:
11111139 s = pd .Series ({"key" : [0 , 1 , 2 , 3 ]})
11121140 key : list [int ] = s ["key" ]
11131141 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 )
11161144
11171145
11181146def test_types_getitem_by_timestamp () -> None :
@@ -1123,9 +1151,9 @@ def test_types_getitem_by_timestamp() -> None:
11231151
11241152def test_types_eq () -> None :
11251153 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 )
11271155 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 )
11291157
11301158
11311159def test_types_rename_axis () -> None :
@@ -1183,6 +1211,7 @@ def add1(x: int) -> int:
11831211 s5 = pd .Series ([1 , 2 , 3 ]).rename ({1 : 10 })
11841212 check (assert_type (s5 , "pd.Series[int]" ), pd .Series , np .integer )
11851213 # inplace
1214+ # TODO fix issue with inplace=True returning a Series, cf pandas #60942
11861215 s6 : None = pd .Series ([1 , 2 , 3 ]).rename ("A" , inplace = True )
11871216
11881217 if TYPE_CHECKING_INVALID_USAGE :
@@ -1192,7 +1221,7 @@ def add1(x: int) -> int:
11921221def test_types_ne () -> None :
11931222 s1 = pd .Series ([1 , 2 , 3 ])
11941223 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 )
11961225
11971226
11981227def test_types_bfill () -> None :
@@ -1261,7 +1290,7 @@ def test_types_ffill() -> None:
12611290
12621291def test_types_as_type () -> None :
12631292 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 )
12651294
12661295
12671296def test_types_dot () -> None :
@@ -1414,13 +1443,19 @@ def test_cat_accessor() -> None:
14141443
14151444
14161445def 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 )
14181447 # 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+ )
14201452 s : Sequence = cast (Sequence , ["a" , "b" , "a" ])
1421- c3 = pd .Categorical (s )
1453+ check ( assert_type ( pd .Categorical (s ), pd . Categorical ), pd . Categorical )
14221454 # 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+ )
14241459
14251460
14261461def test_iloc_getitem_ndarray () -> None :
@@ -1478,8 +1513,8 @@ def test_iloc_setitem_ndarray() -> None:
14781513def test_types_iter () -> None :
14791514 s = pd .Series ([1 , 2 , 3 ], dtype = int )
14801515 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 )
14831518
14841519
14851520def test_types_to_list () -> None :
@@ -2707,12 +2742,12 @@ def test_astype_bytes(cast_arg: BytesDtypeArg, target_type: type) -> None:
27072742@pytest .mark .parametrize ("cast_arg, target_type" , ASTYPE_CATEGORICAL_ARGS , ids = repr )
27082743def test_astype_categorical (cast_arg : CategoryDtypeArg , target_type : type ) -> None :
27092744 s = pd .Series (["a" , "b" ])
2710- check (s .astype ("category" ), pd .Series , target_type )
2745+ check (s .astype (cast_arg ), pd .Series , target_type )
27112746
27122747 if TYPE_CHECKING :
27132748 # pandas category
27142749 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]" )
27162751 # pyarrow dictionary
27172752 # assert_type(s.astype("dictionary[pyarrow]"), "pd.Series[Categorical]")
27182753
0 commit comments