@@ -81,11 +81,17 @@ def test_column_contains() -> None:
81
81
# https://github.com/microsoft/python-type-stubs/issues/199
82
82
df = pd .DataFrame ({"A" : [1 , 2 ], "B" : ["c" , "d" ], "E" : [3 , 4 ]})
83
83
84
- collist = [column for column in df .columns ]
85
-
86
- collist2 = [column for column in df .columns [df .columns .str .contains ("A|B" )]]
87
-
88
- length = len (df .columns [df .columns .str .contains ("A|B" )])
84
+ # check accessing the columns as Scalar
85
+ check (assert_type ([column for column in df .columns ], list [str ]), list )
86
+ # check slicing the columns with a Series[bool]
87
+ check (
88
+ assert_type (
89
+ [column for column in df .columns [df .columns .str .contains ("A|B" )]], list [str ]
90
+ ),
91
+ list ,
92
+ )
93
+ # check that generic methods are defined on a slice of an index
94
+ check (assert_type (len (df .columns [df .columns .str .contains ("A|B" )]), int ), int )
89
95
90
96
91
97
def test_column_sequence () -> None :
@@ -1163,11 +1169,19 @@ def test_value_counts() -> None:
1163
1169
1164
1170
1165
1171
def test_index_naming () -> None :
1166
- """Test that we can set the names of an index as a hashable."""
1172
+ """
1173
+ Test index names type both for the getter and the setter.
1174
+
1175
+ The names of an index should be settable with a sequence (not str) and names
1176
+ property is a list[str | None] (FrozenList).
1177
+ """
1167
1178
df = pd .DataFrame ({"a" : ["a" , "b" , "c" ], "i" : [10 , 11 , 12 ]})
1168
1179
1169
1180
df .index .names = ["idx" ]
1170
-
1181
+ check ( assert_type ( df . index . names , list [ str | None ]), list )
1171
1182
df .index .names = ("idx2" ,)
1183
+ check (assert_type (df .index .names , list [str | None ]), list )
1172
1184
df .index .names = [None ]
1185
+ check (assert_type (df .index .names , list [str | None ]), list )
1173
1186
df .index .names = (None ,)
1187
+ check (assert_type (df .index .names , list [str | None ]), list )
0 commit comments