1
1
import numpy as np
2
2
3
3
from 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
5
5
import pandas .compat as compat
6
6
from pandas .util .decorators import Appender
7
7
import re
@@ -632,9 +632,10 @@ 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', 'index', ' 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 or index, returns the same type as the original object
638
+ (elements are lists of strings).
638
639
639
640
Notes
640
641
-----
@@ -646,9 +647,13 @@ def str_split(arr, pat=None, n=None, return_type='series'):
646
647
"""
647
648
from pandas .core .series import Series
648
649
from pandas .core .frame import DataFrame
650
+ from pandas .core .index import Index
649
651
650
- if return_type not in ('series' , 'frame' ):
651
- raise ValueError ("return_type must be {'series', 'frame'}" )
652
+ if return_type not in ('series' , 'index' , 'frame' ):
653
+ raise ValueError ("return_type must be {'series', 'index', 'frame'}" )
654
+ if return_type == 'frame' and isinstance (arr , Index ):
655
+ raise ValueError ("return_type='frame' is not supported for string "
656
+ "methods on Index" )
652
657
if pat is None :
653
658
if n is None or n == 0 :
654
659
n = - 1
@@ -926,9 +931,9 @@ def do_copy(target):
926
931
class StringMethods (object ):
927
932
928
933
"""
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.
934
+ Vectorized string functions for Series and Index . NAs stay NA unless
935
+ handled otherwise by a particular method. Patterned after Python's string
936
+ methods, with some inspiration from R's stringr package.
932
937
933
938
Examples
934
939
--------
@@ -957,11 +962,18 @@ def __iter__(self):
957
962
def _wrap_result (self , result ):
958
963
from pandas .core .series import Series
959
964
from pandas .core .frame import DataFrame
965
+ from pandas .core .index import Index
960
966
961
967
if not hasattr (result , 'ndim' ):
962
968
return result
963
969
elif result .ndim == 1 :
964
970
name = getattr (result , 'name' , None )
971
+ if isinstance (self .series , Index ):
972
+ # if result is a boolean np.array, return the np.array
973
+ # instead of wrapping it into a boolean Index (GH 8875)
974
+ if is_bool_dtype (result ):
975
+ return result
976
+ return Index (result , name = name or self .series .name )
965
977
return Series (result , index = self .series .index ,
966
978
name = name or self .series .name )
967
979
else :
0 commit comments