11import numpy as np
22
33from pandas .compat import zip
4- from pandas .core .common import isnull , _values_from_object
4+ from pandas .core .common import isnull , _values_from_object , is_bool_dtype
55import pandas .compat as compat
66from pandas .util .decorators import Appender
77import re
@@ -632,9 +632,9 @@ def str_split(arr, pat=None, n=None, return_type='series'):
632632 pat : string, default None
633633 String or regular expression to split on. If None, splits on whitespace
634634 n : int, default None (all)
635- return_type : {'series', 'frame'}, default 'series
635+ return_type : {'series', 'frame'}, default 'series'
636636 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).
638638
639639 Notes
640640 -----
@@ -646,9 +646,13 @@ def str_split(arr, pat=None, n=None, return_type='series'):
646646 """
647647 from pandas .core .series import Series
648648 from pandas .core .frame import DataFrame
649+ from pandas .core .index import Index
649650
650651 if return_type not in ('series' , 'frame' ):
651652 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" )
652656 if pat is None :
653657 if n is None or n == 0 :
654658 n = - 1
@@ -926,9 +930,9 @@ def do_copy(target):
926930class StringMethods (object ):
927931
928932 """
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.
932936
933937 Examples
934938 --------
@@ -957,11 +961,18 @@ def __iter__(self):
957961 def _wrap_result (self , result ):
958962 from pandas .core .series import Series
959963 from pandas .core .frame import DataFrame
964+ from pandas .core .index import Index
960965
961966 if not hasattr (result , 'ndim' ):
962967 return result
963968 elif result .ndim == 1 :
964969 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 is_bool_dtype (result ):
974+ return result
975+ return Index (result , name = name or self .series .name )
965976 return Series (result , index = self .series .index ,
966977 name = name or self .series .name )
967978 else :
0 commit comments