@@ -632,9 +632,9 @@ def str_split(arr, pat=None, n=None, return_type='series'):
632
632
pat : string, default None
633
633
String or regular expression to split on. If None, splits on whitespace
634
634
n : int, default None (all)
635
- return_type : {'series', 'frame'}, default 'series
635
+ return_type : {'series', 'frame'}, default 'series'
636
636
If frame, returns a DataFrame (elements are strings)
637
- If series, returns an Series (elements are lists of strings).
637
+ If series, returns a Series (elements are lists of strings).
638
638
639
639
Notes
640
640
-----
@@ -646,9 +646,13 @@ def str_split(arr, pat=None, n=None, return_type='series'):
646
646
"""
647
647
from pandas .core .series import Series
648
648
from pandas .core .frame import DataFrame
649
+ from pandas .core .index import Index
649
650
650
651
if return_type not in ('series' , 'frame' ):
651
652
raise ValueError ("return_type must be {'series', 'frame'}" )
653
+ if return_type == 'frame' and isinstance (arr , Index ):
654
+ raise ValueError ("return_type='frame' is not supported for string "
655
+ "methods on Index" )
652
656
if pat is None :
653
657
if n is None or n == 0 :
654
658
n = - 1
@@ -926,9 +930,9 @@ def do_copy(target):
926
930
class StringMethods (object ):
927
931
928
932
"""
929
- Vectorized string functions for Series. NAs stay NA unless handled
930
- otherwise by a particular method. Patterned after Python's string methods,
931
- with some inspiration from R's stringr package.
933
+ Vectorized string functions for Series and Index . NAs stay NA unless
934
+ handled otherwise by a particular method. Patterned after Python's string
935
+ methods, with some inspiration from R's stringr package.
932
936
933
937
Examples
934
938
--------
@@ -957,11 +961,18 @@ def __iter__(self):
957
961
def _wrap_result (self , result ):
958
962
from pandas .core .series import Series
959
963
from pandas .core .frame import DataFrame
964
+ from pandas .core .index import Index
960
965
961
966
if not hasattr (result , 'ndim' ):
962
967
return result
963
968
elif result .ndim == 1 :
964
969
name = getattr (result , 'name' , None )
970
+ if isinstance (self .series , Index ):
971
+ # if result is a boolean np.array, return the np.array
972
+ # instead of wrapping it into a boolean Index (GH 8875)
973
+ if result .dtype == bool :
974
+ return result
975
+ return Index (result , name = name or self .series .name )
965
976
return Series (result , index = self .series .index ,
966
977
name = name or self .series .name )
967
978
else :
0 commit comments