@@ -484,17 +484,12 @@ def f(x):
484
484
return empty_row
485
485
486
486
if regex .groups == 1 :
487
- if isinstance (arr , Index ):
488
- result = Index ([f (val )[0 ] for val in arr ],
489
- name = _get_single_group_name (regex ),
490
- dtype = object )
491
- else :
492
- result = Series ([f (val )[0 ] for val in arr ],
493
- name = _get_single_group_name (regex ),
494
- index = arr .index , dtype = object )
487
+ result = np .array ([f (val )[0 ] for val in arr ], dtype = object )
488
+ name = _get_single_group_name (regex )
495
489
else :
496
490
if isinstance (arr , Index ):
497
491
raise ValueError ("only one regex group is supported with Index" )
492
+ name = None
498
493
names = dict (zip (regex .groupindex .values (), regex .groupindex .keys ()))
499
494
columns = [names .get (1 + i , i ) for i in range (regex .groups )]
500
495
if arr .empty :
@@ -504,7 +499,7 @@ def f(x):
504
499
columns = columns ,
505
500
index = arr .index ,
506
501
dtype = object )
507
- return result
502
+ return result , name
508
503
509
504
510
505
def str_get_dummies (arr , sep = '|' ):
@@ -1005,7 +1000,7 @@ def __iter__(self):
1005
1000
i += 1
1006
1001
g = self .get (i )
1007
1002
1008
- def _wrap_result (self , result ):
1003
+ def _wrap_result (self , result , ** kwargs ):
1009
1004
# leave as it is to keep extract and get_dummies results
1010
1005
# can be merged to _wrap_result_expand in v0.17
1011
1006
from pandas .core .series import Series
@@ -1014,16 +1009,16 @@ def _wrap_result(self, result):
1014
1009
1015
1010
if not hasattr (result , 'ndim' ):
1016
1011
return result
1017
- elif result .ndim == 1 :
1018
- name = getattr (result , 'name' , None )
1012
+ name = kwargs .get ('name' ) or getattr (result , 'name' , None ) or self .series .name
1013
+
1014
+ if result .ndim == 1 :
1019
1015
if isinstance (self .series , Index ):
1020
1016
# if result is a boolean np.array, return the np.array
1021
1017
# instead of wrapping it into a boolean Index (GH 8875)
1022
1018
if is_bool_dtype (result ):
1023
1019
return result
1024
- return Index (result , name = name or self .series .name )
1025
- return Series (result , index = self .series .index ,
1026
- name = name or self .series .name )
1020
+ return Index (result , name = name )
1021
+ return Series (result , index = self .series .index , name = name )
1027
1022
else :
1028
1023
assert result .ndim < 3
1029
1024
return DataFrame (result , index = self .series .index )
@@ -1271,7 +1266,11 @@ def get_dummies(self, sep='|'):
1271
1266
startswith = _pat_wrapper (str_startswith , na = True )
1272
1267
endswith = _pat_wrapper (str_endswith , na = True )
1273
1268
findall = _pat_wrapper (str_findall , flags = True )
1274
- extract = _pat_wrapper (str_extract , flags = True )
1269
+
1270
+ @copy (str_extract )
1271
+ def extract (self , pat , flags = 0 ):
1272
+ result , name = str_extract (self .series , pat , flags = flags )
1273
+ return self ._wrap_result (result , name = name )
1275
1274
1276
1275
_shared_docs ['find' ] = ("""
1277
1276
Return %(side)s indexes in each strings in the Series/Index
0 commit comments