@@ -1174,6 +1174,26 @@ def test_join_self(self):
1174
1174
for kind in kinds :
1175
1175
joined = res .join (res , how = kind )
1176
1176
self .assertIs (res , joined )
1177
+ def test_str_attribute (self ):
1178
+ # GH9068
1179
+ methods = ['strip' , 'rstrip' , 'lstrip' ]
1180
+ idx = Index ([' jack' , 'jill ' , ' jesse ' , 'frank' ])
1181
+ for method in methods :
1182
+ expected = Index ([getattr (str , method )(x ) for x in idx .values ])
1183
+ tm .assert_index_equal (getattr (Index .str , method )(idx .str ), expected )
1184
+
1185
+ # create a few instances that are not able to use .str accessor
1186
+ indices = [Index (range (5 )),
1187
+ tm .makeDateIndex (10 ),
1188
+ PeriodIndex (start = '2000' , end = '2010' , freq = 'A' )]
1189
+ for idx in indices :
1190
+ with self .assertRaisesRegexp (AttributeError , 'only use .str accessor' ):
1191
+ idx .str .repeat (2 )
1192
+
1193
+ # MultiIndex is not supported
1194
+ idx = pd .MultiIndex .from_tuples ([('foo' , '1' ), ('bar' , '3' )])
1195
+ with self .assertRaisesRegexp (AttributeError , 'MultiIndex' ):
1196
+ idx .str .len ()
1177
1197
1178
1198
def test_indexing_doesnt_change_class (self ):
1179
1199
idx = Index ([1 , 2 , 3 , 'a' , 'b' , 'c' ])
0 commit comments