<s> import numpy as np <EOL> from pandas . compat import zip <EOL> from pandas . core . common import ( isnull , notnull , _values_from_object , <EOL> is_bool_dtype , <EOL> is_list_like , is_categorical_dtype , <EOL> is_object_dtype ) <EOL> from pandas . core . algorithms import take_1d <EOL> import pandas . compat as compat <EOL> from pandas . core . base import AccessorProperty , NoNewAttributesMixin <EOL> from pandas . util . decorators import Appender , deprecate_kwarg <EOL> import re <EOL> import pandas . lib as lib <EOL> import warnings <EOL> import textwrap <EOL> import codecs <EOL> _cpython_optimized_encoders = ( <EOL> "<STR_LIT:utf-8>" , "<STR_LIT:utf8>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:ascii>" <EOL> ) <EOL> _cpython_optimized_decoders = _cpython_optimized_encoders + ( <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ) <EOL> _shared_docs = dict ( ) <EOL> def _get_array_list ( arr , others ) : <EOL> from pandas . core . series import Series <EOL> if len ( others ) and isinstance ( _values_from_object ( others ) [ <NUM_LIT:0> ] , <EOL> ( list , np . ndarray , Series ) ) : <EOL> arrays = [ arr ] + list ( others ) <EOL> else : <EOL> arrays = [ arr , others ] <EOL> return [ np . asarray ( x , dtype = object ) for x in arrays ] <EOL> def str_cat ( arr , others = None , sep = None , na_rep = None ) : <EOL> """<STR_LIT>""" <EOL> if sep is None : <EOL> sep = '<STR_LIT>' <EOL> if others is not None : <EOL> arrays = _get_array_list ( arr , others ) <EOL> n = _length_check ( arrays ) <EOL> masks = np . array ( [ isnull ( x ) for x in arrays ] ) <EOL> cats = None <EOL> if na_rep is None : <EOL> na_mask = np . logical_or . reduce ( masks , axis = <NUM_LIT:0> ) <EOL> result = np . empty ( n , dtype = object ) <EOL> np . putmask ( result , na_mask , np . nan ) <EOL> notmask = ~ na_mask <EOL> tuples = zip ( * [ x [ notmask ] for x in arrays ] ) <EOL> cats = [ sep . join ( tup ) for tup in tuples ] <EOL> result [ notmask ] = cats <EOL> else : <EOL> for i , x in enumerate ( arrays ) : <EOL> x = np . where ( masks [ i ] , na_rep , x ) <EOL> if cats is None : <EOL> cats = x <EOL> else : <EOL> cats = cats + sep + x <EOL> result = cats <EOL> return result <EOL> else : <EOL> arr = np . asarray ( arr , dtype = object ) <EOL> mask = isnull ( arr ) <EOL> if na_rep is None and mask . any ( ) : <EOL> if sep == '<STR_LIT>' : <EOL> na_rep = '<STR_LIT>' <EOL> else : <EOL> return sep . join ( arr [ notnull ( arr ) ] ) <EOL> return sep . join ( np . where ( mask , na_rep , arr ) ) <EOL> def _length_check ( others ) : <EOL> n = None <EOL> for x in others : <EOL> try : <EOL> if n is None : <EOL> n = len ( x ) <EOL> elif len ( x ) != n : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> except TypeError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return n <EOL> def _na_map ( f , arr , na_result = np . nan , dtype = object ) : <EOL> return _map ( f , arr , na_mask = True , na_value = na_result , dtype = dtype ) <EOL> def _map ( f , arr , na_mask = False , na_value = np . nan , dtype = object ) : <EOL> from pandas . core . series import Series <EOL> if not len ( arr ) : <EOL> return np . ndarray ( <NUM_LIT:0> , dtype = dtype ) <EOL> if isinstance ( arr , Series ) : <EOL> arr = arr . values <EOL> if not isinstance ( arr , np . ndarray ) : <EOL> arr = np . asarray ( arr , dtype = object ) <EOL> if na_mask : <EOL> mask = isnull ( arr ) <EOL> try : <EOL> result = lib . map_infer_mask ( arr , f , mask . view ( np . uint8 ) ) <EOL> except ( TypeError , AttributeError ) : <EOL> def g ( x ) : <EOL> try : <EOL> return f ( x ) <EOL> except ( TypeError , AttributeError ) : <EOL> return na_value <EOL> return _map ( g , arr , dtype = dtype ) <EOL> if na_value is not np . nan : <EOL> np . putmask ( result , mask , na_value ) <EOL> if result . dtype == object : <EOL> result = lib . maybe_convert_objects ( result ) <EOL> return result <EOL> else : <EOL> return lib . map_infer ( arr , f ) <EOL> def str_count ( arr , pat , flags = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> regex = re . compile ( pat , flags = flags ) <EOL> f = lambda x : len ( regex . findall ( x ) ) <EOL> return _na_map ( f , arr , dtype = int ) <EOL> def str_contains ( arr , pat , case = True , flags = <NUM_LIT:0> , na = np . nan , regex = True ) : <EOL> """<STR_LIT>""" <EOL> if regex : <EOL> if not case : <EOL> flags |= re . IGNORECASE <EOL> regex = re . compile ( pat , flags = flags ) <EOL> if regex . groups > <NUM_LIT:0> : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , UserWarning , <EOL> stacklevel = <NUM_LIT:3> ) <EOL> f = lambda x : bool ( regex . search ( x ) ) <EOL> else : <EOL> if case : <EOL> f = lambda x : pat in x <EOL> else : <EOL> upper_pat = pat . upper ( ) <EOL> f = lambda x : upper_pat in x <EOL> uppered = _na_map ( lambda x : x . upper ( ) , arr ) <EOL> return _na_map ( f , uppered , na , dtype = bool ) <EOL> return _na_map ( f , arr , na , dtype = bool ) <EOL> def str_startswith ( arr , pat , na = np . nan ) : <EOL> """<STR_LIT>""" <EOL> f = lambda x : x . startswith ( pat ) <EOL> return _na_map ( f , arr , na , dtype = bool ) <EOL> def str_endswith ( arr , pat , na = np . nan ) : <EOL> """<STR_LIT>""" <EOL> f = lambda x : x . endswith ( pat ) <EOL> return _na_map ( f , arr , na , dtype = bool ) <EOL> def str_replace ( arr , pat , repl , n = - <NUM_LIT:1> , case = True , flags = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> use_re = not case or len ( pat ) > <NUM_LIT:1> or flags <EOL> if use_re : <EOL> if not case : <EOL> flags |= re . IGNORECASE <EOL> regex = re . compile ( pat , flags = flags ) <EOL> n = n if n >= <NUM_LIT:0> else <NUM_LIT:0> <EOL> def f ( x ) : <EOL> return regex . sub ( repl , x , count = n ) <EOL> else : <EOL> f = lambda x : x . replace ( pat , repl , n ) <EOL> return _na_map ( f , arr ) <EOL> def str_repeat ( arr , repeats ) : <EOL> """<STR_LIT>""" <EOL> if lib . isscalar ( repeats ) : <EOL> def rep ( x ) : <EOL> try : <EOL> return compat . binary_type . __mul__ ( x , repeats ) <EOL> except TypeError : <EOL> return compat . text_type . __mul__ ( x , repeats ) <EOL> return _na_map ( rep , arr ) <EOL> else : <EOL> def rep ( x , r ) : <EOL> try : <EOL> return compat . binary_type . __mul__ ( x , r ) <EOL> except TypeError : <EOL> return compat . text_type . __mul__ ( x , r ) <EOL> repeats = np . asarray ( repeats , dtype = object ) <EOL> result = lib . vec_binop ( _values_from_object ( arr ) , repeats , rep ) <EOL> return result <EOL> def str_match ( arr , pat , case = True , flags = <NUM_LIT:0> , na = np . nan , as_indexer = False ) : <EOL> """<STR_LIT>""" <EOL> if not case : <EOL> flags |= re . IGNORECASE <EOL> regex = re . compile ( pat , flags = flags ) <EOL> if ( not as_indexer ) and regex . groups > <NUM_LIT:0> : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , FutureWarning , <EOL> stacklevel = <NUM_LIT:3> ) <EOL> if as_indexer and regex . groups > <NUM_LIT:0> : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , UserWarning , stacklevel = <NUM_LIT:3> ) <EOL> if ( not as_indexer ) and regex . groups > <NUM_LIT:0> : <EOL> dtype = object <EOL> def f ( x ) : <EOL> m = regex . match ( x ) <EOL> if m : <EOL> return m . groups ( ) <EOL> else : <EOL> return [ ] <EOL> else : <EOL> dtype = bool <EOL> f = lambda x : bool ( regex . match ( x ) ) <EOL> return _na_map ( f , arr , na , dtype = dtype ) <EOL> def _get_single_group_name ( rx ) : <EOL> try : <EOL> return list ( rx . groupindex . keys ( ) ) . pop ( ) <EOL> except IndexError : <EOL> return None <EOL> def _groups_or_na_fun ( regex ) : <EOL> """<STR_LIT>""" <EOL> if regex . groups == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> empty_row = [ np . nan ] * regex . groups <EOL> def f ( x ) : <EOL> if not isinstance ( x , compat . string_types ) : <EOL> return empty_row <EOL> m = regex . search ( x ) <EOL> if m : <EOL> return [ np . nan if item is None else item for item in m . groups ( ) ] <EOL> else : <EOL> return empty_row <EOL> return f <EOL> def _str_extract_noexpand ( arr , pat , flags = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> from pandas import DataFrame , Index <EOL> regex = re . compile ( pat , flags = flags ) <EOL> groups_or_na = _groups_or_na_fun ( regex ) <EOL> if regex . groups == <NUM_LIT:1> : <EOL> result = np . array ( [ groups_or_na ( val ) [ <NUM_LIT:0> ] for val in arr ] , dtype = object ) <EOL> name = _get_single_group_name ( regex ) <EOL> else : <EOL> if isinstance ( arr , Index ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> name = None <EOL> names = dict ( zip ( regex . groupindex . values ( ) , regex . groupindex . keys ( ) ) ) <EOL> columns = [ names . get ( <NUM_LIT:1> + i , i ) for i in range ( regex . groups ) ] <EOL> if arr . empty : <EOL> result = DataFrame ( columns = columns , dtype = object ) <EOL> else : <EOL> result = DataFrame ( <EOL> [ groups_or_na ( val ) for val in arr ] , <EOL> columns = columns , <EOL> index = arr . index , <EOL> dtype = object ) <EOL> return result , name <EOL> def _str_extract_frame ( arr , pat , flags = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> from pandas import DataFrame <EOL> regex = re . compile ( pat , flags = flags ) <EOL> groups_or_na = _groups_or_na_fun ( regex ) <EOL> names = dict ( zip ( regex . groupindex . values ( ) , regex . groupindex . keys ( ) ) ) <EOL> columns = [ names . get ( <NUM_LIT:1> + i , i ) for i in range ( regex . groups ) ] <EOL> if len ( arr ) == <NUM_LIT:0> : <EOL> return DataFrame ( columns = columns , dtype = object ) <EOL> try : <EOL> result_index = arr . index <EOL> except AttributeError : <EOL> result_index = None <EOL> return DataFrame ( <EOL> [ groups_or_na ( val ) for val in arr ] , <EOL> columns = columns , <EOL> index = result_index , <EOL> dtype = object ) <EOL> def str_extract ( arr , pat , flags = <NUM_LIT:0> , expand = None ) : <EOL> """<STR_LIT>""" <EOL> if expand is None : <EOL> warnings . warn ( <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" , <EOL> FutureWarning , <EOL> stacklevel = <NUM_LIT:3> ) <EOL> expand = False <EOL> if not isinstance ( expand , bool ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if expand : <EOL> return _str_extract_frame ( arr . _orig , pat , flags = flags ) <EOL> else : <EOL> result , name = _str_extract_noexpand ( arr . _data , pat , flags = flags ) <EOL> return arr . _wrap_result ( result , name = name , expand = expand ) <EOL> def str_extractall ( arr , pat , flags = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> from pandas import DataFrame , MultiIndex <EOL> regex = re . compile ( pat , flags = flags ) <EOL> if regex . groups == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> names = dict ( zip ( regex . groupindex . values ( ) , regex . groupindex . keys ( ) ) ) <EOL> columns = [ names . get ( <NUM_LIT:1> + i , i ) for i in range ( regex . groups ) ] <EOL> match_list = [ ] <EOL> index_list = [ ] <EOL> for subject_key , subject in arr . iteritems ( ) : <EOL> if isinstance ( subject , compat . string_types ) : <EOL> try : <EOL> key_list = list ( subject_key ) <EOL> except TypeError : <EOL> key_list = [ subject_key ] <EOL> for match_i , match_tuple in enumerate ( regex . findall ( subject ) ) : <EOL> na_tuple = [ <EOL> np . NaN if group == "<STR_LIT>" else group for group in match_tuple ] <EOL> match_list . append ( na_tuple ) <EOL> result_key = tuple ( key_list + [ match_i ] ) <EOL> index_list . append ( result_key ) <EOL> if <NUM_LIT:0> < len ( index_list ) : <EOL> index = MultiIndex . from_tuples ( <EOL> index_list , names = arr . index . names + [ "<STR_LIT>" ] ) <EOL> else : <EOL> index = None <EOL> result = DataFrame ( match_list , index , columns ) <EOL> return result <EOL> def str_get_dummies ( arr , sep = '<STR_LIT:|>' ) : <EOL> """<STR_LIT>""" <EOL> arr = arr . fillna ( '<STR_LIT>' ) <EOL> try : <EOL> arr = sep + arr + sep <EOL> except TypeError : <EOL> arr = sep + arr . astype ( str ) + sep <EOL> tags = set ( ) <EOL> for ts in arr . str . split ( sep ) : <EOL> tags . update ( ts ) <EOL> tags = sorted ( tags - set ( [ "<STR_LIT>" ] ) ) <EOL> dummies = np . empty ( ( len ( arr ) , len ( tags ) ) , dtype = np . int64 ) <EOL> for i , t in enumerate ( tags ) : <EOL> pat = sep + t + sep <EOL> dummies [ : , i ] = lib . map_infer ( arr . values , lambda x : pat in x ) <EOL> return dummies , tags <EOL> def str_join ( arr , sep ) : <EOL> """<STR_LIT>""" <EOL> return _na_map ( sep . join , arr ) <EOL> def str_findall ( arr , pat , flags = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> regex = re . compile ( pat , flags = flags ) <EOL> return _na_map ( regex . findall , arr ) <EOL> def str_find ( arr , sub , start = <NUM_LIT:0> , end = None , side = '<STR_LIT:left>' ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( sub , compat . string_types ) : <EOL> msg = '<STR_LIT>' <EOL> raise TypeError ( msg . format ( type ( sub ) . __name__ ) ) <EOL> if side == '<STR_LIT:left>' : <EOL> method = '<STR_LIT>' <EOL> elif side == '<STR_LIT:right>' : <EOL> method = '<STR_LIT>' <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if end is None : <EOL> f = lambda x : getattr ( x , method ) ( sub , start ) <EOL> else : <EOL> f = lambda x : getattr ( x , method ) ( sub , start , end ) <EOL> return _na_map ( f , arr , dtype = int ) <EOL> def str_index ( arr , sub , start = <NUM_LIT:0> , end = None , side = '<STR_LIT:left>' ) : <EOL> if not isinstance ( sub , compat . string_types ) : <EOL> msg = '<STR_LIT>' <EOL> raise TypeError ( msg . format ( type ( sub ) . __name__ ) ) <EOL> if side == '<STR_LIT:left>' : <EOL> method = '<STR_LIT:index>' <EOL> elif side == '<STR_LIT:right>' : <EOL> method = '<STR_LIT>' <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if end is None : <EOL> f = lambda x : getattr ( x , method ) ( sub , start ) <EOL> else : <EOL> f = lambda x : getattr ( x , method ) ( sub , start , end ) <EOL> return _na_map ( f , arr , dtype = int ) <EOL> def str_pad ( arr , width , side = '<STR_LIT:left>' , fillchar = '<STR_LIT:U+0020>' ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( fillchar , compat . string_types ) : <EOL> msg = '<STR_LIT>' <EOL> raise TypeError ( msg . format ( type ( fillchar ) . __name__ ) ) <EOL> if len ( fillchar ) != <NUM_LIT:1> : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if side == '<STR_LIT:left>' : <EOL> f = lambda x : x . rjust ( width , fillchar ) <EOL> elif side == '<STR_LIT:right>' : <EOL> f = lambda x : x . ljust ( width , fillchar ) <EOL> elif side == '<STR_LIT>' : <EOL> f = lambda x : x . center ( width , fillchar ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return _na_map ( f , arr ) <EOL> def str_split ( arr , pat = None , n = None ) : <EOL> """<STR_LIT>""" <EOL> if pat is None : <EOL> if n is None or n == <NUM_LIT:0> : <EOL> n = - <NUM_LIT:1> <EOL> f = lambda x : x . split ( pat , n ) <EOL> else : <EOL> if len ( pat ) == <NUM_LIT:1> : <EOL> if n is None or n == <NUM_LIT:0> : <EOL> n = - <NUM_LIT:1> <EOL> f = lambda x : x . split ( pat , n ) <EOL> else : <EOL> if n is None or n == - <NUM_LIT:1> : <EOL> n = <NUM_LIT:0> <EOL> regex = re . compile ( pat ) <EOL> f = lambda x : regex . split ( x , maxsplit = n ) <EOL> res = _na_map ( f , arr ) <EOL> return res <EOL> def str_rsplit ( arr , pat = None , n = None ) : <EOL> """<STR_LIT>""" <EOL> if n is None or n == <NUM_LIT:0> : <EOL> n = - <NUM_LIT:1> <EOL> f = lambda x : x . rsplit ( pat , n ) <EOL> res = _na_map ( f , arr ) <EOL> return res <EOL> def str_slice ( arr , start = None , stop = None , step = None ) : <EOL> """<STR_LIT>""" <EOL> obj = slice ( start , stop , step ) <EOL> f = lambda x : x [ obj ] <EOL> return _na_map ( f , arr ) <EOL> def str_slice_replace ( arr , start = None , stop = None , repl = None ) : <EOL> """<STR_LIT>""" <EOL> if repl is None : <EOL> repl = '<STR_LIT>' <EOL> def f ( x ) : <EOL> if x [ start : stop ] == '<STR_LIT>' : <EOL> local_stop = start <EOL> else : <EOL> local_stop = stop <EOL> y = '<STR_LIT>' <EOL> if start is not None : <EOL> y += x [ : start ] <EOL> y += repl <EOL> if stop is not None : <EOL> y += x [ local_stop : ] <EOL> return y <EOL> return _na_map ( f , arr ) <EOL> def str_strip ( arr , to_strip = None , side = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if side == '<STR_LIT>' : <EOL> f = lambda x : x . strip ( to_strip ) <EOL> elif side == '<STR_LIT:left>' : <EOL> f = lambda x : x . lstrip ( to_strip ) <EOL> elif side == '<STR_LIT:right>' : <EOL> f = lambda x : x . rstrip ( to_strip ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return _na_map ( f , arr ) <EOL> def str_wrap ( arr , width , ** kwargs ) : <EOL> r"""<STR_LIT>""" <EOL> kwargs [ '<STR_LIT:width>' ] = width <EOL> tw = textwrap . TextWrapper ( ** kwargs ) <EOL> return _na_map ( lambda s : '<STR_LIT:\n>' . join ( tw . wrap ( s ) ) , arr ) <EOL> def str_translate ( arr , table , deletechars = None ) : <EOL> """<STR_LIT>""" <EOL> if deletechars is None : <EOL> f = lambda x : x . translate ( table ) <EOL> else : <EOL> from pandas import compat <EOL> if compat . PY3 : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> f = lambda x : x . translate ( table , deletechars ) <EOL> return _na_map ( f , arr ) <EOL> def str_get ( arr , i ) : <EOL> """<STR_LIT>""" <EOL> f = lambda x : x [ i ] if len ( x ) > i else np . nan <EOL> return _na_map ( f , arr ) <EOL> def str_decode ( arr , encoding , errors = "<STR_LIT:strict>" ) : <EOL> """<STR_LIT>""" <EOL> if encoding in _cpython_optimized_decoders : <EOL> f = lambda x : x . decode ( encoding , errors ) <EOL> else : <EOL> decoder = codecs . getdecoder ( encoding ) <EOL> f = lambda x : decoder ( x , errors ) [ <NUM_LIT:0> ] <EOL> return _na_map ( f , arr ) <EOL> def str_encode ( arr , encoding , errors = "<STR_LIT:strict>" ) : <EOL> """<STR_LIT>""" <EOL> if encoding in _cpython_optimized_encoders : <EOL> f = lambda x : x . encode ( encoding , errors ) <EOL> else : <EOL> encoder = codecs . getencoder ( encoding ) <EOL> f = lambda x : encoder ( x , errors ) [ <NUM_LIT:0> ] <EOL> return _na_map ( f , arr ) <EOL> def _noarg_wrapper ( f , docstring = None , ** kargs ) : <EOL> def wrapper ( self ) : <EOL> result = _na_map ( f , self . _data , ** kargs ) <EOL> return self . _wrap_result ( result ) <EOL> wrapper . __name__ = f . __name__ <EOL> if docstring is not None : <EOL> wrapper . __doc__ = docstring <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return wrapper <EOL> def _pat_wrapper ( f , flags = False , na = False , ** kwargs ) : <EOL> def wrapper1 ( self , pat ) : <EOL> result = f ( self . _data , pat ) <EOL> return self . _wrap_result ( result ) <EOL> def wrapper2 ( self , pat , flags = <NUM_LIT:0> , ** kwargs ) : <EOL> result = f ( self . _data , pat , flags = flags , ** kwargs ) <EOL> return self . _wrap_result ( result ) <EOL> def wrapper3 ( self , pat , na = np . nan ) : <EOL> result = f ( self . _data , pat , na = na ) <EOL> return self . _wrap_result ( result ) <EOL> wrapper = wrapper3 if na else wrapper2 if flags else wrapper1 <EOL> wrapper . __name__ = f . __name__ <EOL> if f . __doc__ : <EOL> wrapper . __doc__ = f . __doc__ <EOL> return wrapper <EOL> def copy ( source ) : <EOL> "<STR_LIT>" <EOL> def do_copy ( target ) : <EOL> if source . __doc__ : <EOL> target . __doc__ = source . __doc__ <EOL> return target <EOL> return do_copy <EOL> class StringMethods ( NoNewAttributesMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data ) : <EOL> self . _is_categorical = is_categorical_dtype ( data ) <EOL> self . _data = data . cat . categories if self . _is_categorical else data <EOL> self . _orig = data <EOL> self . _freeze ( ) <EOL> def __getitem__ ( self , key ) : <EOL> if isinstance ( key , slice ) : <EOL> return self . slice ( start = key . start , stop = key . stop , step = key . step ) <EOL> else : <EOL> return self . get ( key ) <EOL> def __iter__ ( self ) : <EOL> i = <NUM_LIT:0> <EOL> g = self . get ( i ) <EOL> while g . notnull ( ) . any ( ) : <EOL> yield g <EOL> i += <NUM_LIT:1> <EOL> g = self . get ( i ) <EOL> def _wrap_result ( self , result , use_codes = True , <EOL> name = None , expand = None ) : <EOL> from pandas . core . index import Index , MultiIndex <EOL> if use_codes and self . _is_categorical : <EOL> result = take_1d ( result , self . _orig . cat . codes ) <EOL> if not hasattr ( result , '<STR_LIT>' ) or not hasattr ( result , '<STR_LIT>' ) : <EOL> return result <EOL> assert result . ndim < <NUM_LIT:3> <EOL> if expand is None : <EOL> expand = False if result . ndim == <NUM_LIT:1> else True <EOL> elif expand is True and not isinstance ( self . _orig , Index ) : <EOL> def cons_row ( x ) : <EOL> if is_list_like ( x ) : <EOL> return x <EOL> else : <EOL> return [ x ] <EOL> result = [ cons_row ( x ) for x in result ] <EOL> if not isinstance ( expand , bool ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if expand is False : <EOL> if name is None : <EOL> name = getattr ( result , '<STR_LIT:name>' , None ) <EOL> if name is None : <EOL> name = self . _orig . name <EOL> if isinstance ( self . _orig , Index ) : <EOL> if is_bool_dtype ( result ) : <EOL> return result <EOL> if expand : <EOL> result = list ( result ) <EOL> return MultiIndex . from_tuples ( result , names = name ) <EOL> else : <EOL> return Index ( result , name = name ) <EOL> else : <EOL> index = self . _orig . index <EOL> if expand : <EOL> cons = self . _orig . _constructor_expanddim <EOL> return cons ( result , columns = name , index = index ) <EOL> else : <EOL> cons = self . _orig . _constructor <EOL> return cons ( result , name = name , index = index ) <EOL> @ copy ( str_cat ) <EOL> def cat ( self , others = None , sep = None , na_rep = None ) : <EOL> data = self . _orig if self . _is_categorical else self . _data <EOL> result = str_cat ( data , others = others , sep = sep , na_rep = na_rep ) <EOL> return self . _wrap_result ( result , use_codes = ( not self . _is_categorical ) ) <EOL> @ deprecate_kwarg ( '<STR_LIT>' , '<STR_LIT>' , mapping = { '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True } ) <EOL> @ copy ( str_split ) <EOL> def split ( self , pat = None , n = - <NUM_LIT:1> , expand = False ) : <EOL> result = str_split ( self . _data , pat , n = n ) <EOL> return self . _wrap_result ( result , expand = expand ) <EOL> @ copy ( str_rsplit ) <EOL> def rsplit ( self , pat = None , n = - <NUM_LIT:1> , expand = False ) : <EOL> result = str_rsplit ( self . _data , pat , n = n ) <EOL> return self . _wrap_result ( result , expand = expand ) <EOL> _shared_docs [ '<STR_LIT>' ] = ( """<STR_LIT>""" ) <EOL> @ Appender ( _shared_docs [ '<STR_LIT>' ] % { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> def partition ( self , pat = '<STR_LIT:U+0020>' , expand = True ) : <EOL> f = lambda x : x . partition ( pat ) <EOL> result = _na_map ( f , self . _data ) <EOL> return self . _wrap_result ( result , expand = expand ) <EOL> @ Appender ( _shared_docs [ '<STR_LIT>' ] % { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> def rpartition ( self , pat = '<STR_LIT:U+0020>' , expand = True ) : <EOL> f = lambda x : x . rpartition ( pat ) <EOL> result = _na_map ( f , self . _data ) <EOL> return self . _wrap_result ( result , expand = expand ) <EOL> @ copy ( str_get ) <EOL> def get ( self , i ) : <EOL> result = str_get ( self . _data , i ) <EOL> return self . _wrap_result ( result ) <EOL> @ copy ( str_join ) <EOL> def join ( self , sep ) : <EOL> result = str_join ( self . _data , sep ) <EOL> return self . _wrap_result ( result ) <EOL> @ copy ( str_contains ) <EOL> def contains ( self , pat , case = True , flags = <NUM_LIT:0> , na = np . nan , regex = True ) : <EOL> result = str_contains ( self . _data , pat , case = case , flags = flags , na = na , <EOL> regex = regex ) <EOL> return self . _wrap_result ( result ) <EOL> @ copy ( str_match ) <EOL> def match ( self , pat , case = True , flags = <NUM_LIT:0> , na = np . nan , as_indexer = False ) : <EOL> result = str_match ( self . _data , pat , case = case , flags = flags , na = na , <EOL> as_indexer = as_indexer ) <EOL> return self . _wrap_result ( result ) <EOL> @ copy ( str_replace ) <EOL> def replace ( self , pat , repl , n = - <NUM_LIT:1> , case = True , flags = <NUM_LIT:0> ) : <EOL> result = str_replace ( self . _data , pat , repl , n = n , case = case , <EOL> flags = flags ) <EOL> return self . _wrap_result ( result ) <EOL> @ copy ( str_repeat ) <EOL> def repeat ( self , repeats ) : <EOL> result = str_repeat ( self . _data , repeats ) <EOL> return self . _wrap_result ( result ) <EOL> @ copy ( str_pad ) <EOL> def pad ( self , width , side = '<STR_LIT:left>' , fillchar = '<STR_LIT:U+0020>' ) : <EOL> result = str_pad ( self . _data , width , side = side , fillchar = fillchar ) <EOL> return self . _wrap_result ( result ) <EOL> _shared_docs [ '<STR_LIT>' ] = ( """<STR_LIT>""" ) <EOL> @ Appender ( _shared_docs [ '<STR_LIT>' ] % dict ( side = '<STR_LIT>' , <EOL> method = '<STR_LIT>' ) ) <EOL> def center ( self , width , fillchar = '<STR_LIT:U+0020>' ) : <EOL> return self . pad ( width , side = '<STR_LIT>' , fillchar = fillchar ) <EOL> @ Appender ( _shared_docs [ '<STR_LIT>' ] % dict ( side = '<STR_LIT:right>' , method = '<STR_LIT>' ) ) <EOL> def ljust ( self , width , fillchar = '<STR_LIT:U+0020>' ) : <EOL> return self . pad ( width , side = '<STR_LIT:right>' , fillchar = fillchar ) <EOL> @ Appender ( _shared_docs [ '<STR_LIT>' ] % dict ( side = '<STR_LIT:left>' , method = '<STR_LIT>' ) ) <EOL> def rjust ( self , width , fillchar = '<STR_LIT:U+0020>' ) : <EOL> return self . pad ( width , side = '<STR_LIT:left>' , fillchar = fillchar ) <EOL> def zfill ( self , width ) : <EOL> """<STR_LIT>""" <EOL> result = str_pad ( self . _data , width , side = '<STR_LIT:left>' , fillchar = '<STR_LIT:0>' ) <EOL> return self . _wrap_result ( result ) <EOL> @ copy ( str_slice ) <EOL> def slice ( self , start = None , stop = None , step = None ) : <EOL> result = str_slice ( self . _data , start , stop , step ) <EOL> return self . _wrap_result ( result ) <EOL> @ copy ( str_slice_replace ) <EOL> def slice_replace ( self , start = None , stop = None , repl = None ) : <EOL> result = str_slice_replace ( self . _data , start , stop , repl ) <EOL> return self . _wrap_result ( result ) <EOL> @ copy ( str_decode ) <EOL> def decode ( self , encoding , errors = "<STR_LIT:strict>" ) : <EOL> result = str_decode ( self . _data , encoding , errors ) <EOL> return self . _wrap_result ( result ) <EOL> @ copy ( str_encode ) <EOL> def encode ( self , encoding , errors = "<STR_LIT:strict>" ) : <EOL> result = str_encode ( self . _data , encoding , errors ) <EOL> return self . _wrap_result ( result ) <EOL> _shared_docs [ '<STR_LIT>' ] = ( """<STR_LIT>""" ) <EOL> @ Appender ( _shared_docs [ '<STR_LIT>' ] % dict ( side = '<STR_LIT>' , <EOL> method = '<STR_LIT>' ) ) <EOL> def strip ( self , to_strip = None ) : <EOL> result = str_strip ( self . _data , to_strip , side = '<STR_LIT>' ) <EOL> return self . _wrap_result ( result ) <EOL> @ Appender ( _shared_docs [ '<STR_LIT>' ] % dict ( side = '<STR_LIT>' , <EOL> method = '<STR_LIT>' ) ) <EOL> def lstrip ( self , to_strip = None ) : <EOL> result = str_strip ( self . _data , to_strip , side = '<STR_LIT:left>' ) <EOL> return self . _wrap_result ( result ) <EOL> @ Appender ( _shared_docs [ '<STR_LIT>' ] % dict ( side = '<STR_LIT>' , <EOL> method = '<STR_LIT>' ) ) <EOL> def rstrip ( self , to_strip = None ) : <EOL> result = str_strip ( self . _data , to_strip , side = '<STR_LIT:right>' ) <EOL> return self . _wrap_result ( result ) <EOL> @ copy ( str_wrap ) <EOL> def wrap ( self , width , ** kwargs ) : <EOL> result = str_wrap ( self . _data , width , ** kwargs ) <EOL> return self . _wrap_result ( result ) <EOL> @ copy ( str_get_dummies ) <EOL> def get_dummies ( self , sep = '<STR_LIT:|>' ) : <EOL> data = self . _orig . astype ( str ) if self . _is_categorical else self . _data <EOL> result , name = str_get_dummies ( data , sep ) <EOL> return self . _wrap_result ( result , use_codes = ( not self . _is_categorical ) , <EOL> name = name , expand = True ) <EOL> @ copy ( str_translate ) <EOL> def translate ( self , table , deletechars = None ) : <EOL> result = str_translate ( self . _data , table , deletechars ) <EOL> return self . _wrap_result ( result ) <EOL> count = _pat_wrapper ( str_count , flags = True ) <EOL> startswith = _pat_wrapper ( str_startswith , na = True ) <EOL> endswith = _pat_wrapper ( str_endswith , na = True ) <EOL> findall = _pat_wrapper ( str_findall , flags = True ) <EOL> @ copy ( str_extract ) <EOL> def extract ( self , pat , flags = <NUM_LIT:0> , expand = None ) : <EOL> return str_extract ( self , pat , flags = flags , expand = expand ) <EOL> @ copy ( str_extractall ) <EOL> def extractall ( self , pat , flags = <NUM_LIT:0> ) : <EOL> return str_extractall ( self . _orig , pat , flags = flags ) <EOL> _shared_docs [ '<STR_LIT>' ] = ( """<STR_LIT>""" ) <EOL> @ Appender ( _shared_docs [ '<STR_LIT>' ] % <EOL> dict ( side = '<STR_LIT>' , method = '<STR_LIT>' , <EOL> also = '<STR_LIT>' ) ) <EOL> def find ( self , sub , start = <NUM_LIT:0> , end = None ) : <EOL> result = str_find ( self . _data , sub , start = start , end = end , side = '<STR_LIT:left>' ) <EOL> return self . _wrap_result ( result ) <EOL> @ Appender ( _shared_docs [ '<STR_LIT>' ] % <EOL> dict ( side = '<STR_LIT>' , method = '<STR_LIT>' , <EOL> also = '<STR_LIT>' ) ) <EOL> def rfind ( self , sub , start = <NUM_LIT:0> , end = None ) : <EOL> result = str_find ( self . _data , sub , start = start , end = end , side = '<STR_LIT:right>' ) <EOL> return self . _wrap_result ( result ) <EOL> def normalize ( self , form ) : <EOL> """<STR_LIT>""" <EOL> import unicodedata <EOL> f = lambda x : unicodedata . normalize ( form , compat . u_safe ( x ) ) <EOL> result = _na_map ( f , self . _data ) <EOL> return self . _wrap_result ( result ) <EOL> _shared_docs [ '<STR_LIT:index>' ] = ( """<STR_LIT>""" ) <EOL> @ Appender ( _shared_docs [ '<STR_LIT:index>' ] % <EOL> dict ( side = '<STR_LIT>' , similar = '<STR_LIT>' , method = '<STR_LIT:index>' , <EOL> also = '<STR_LIT>' ) ) <EOL> def index ( self , sub , start = <NUM_LIT:0> , end = None ) : <EOL> result = str_index ( self . _data , sub , start = start , end = end , side = '<STR_LIT:left>' ) <EOL> return self . _wrap_result ( result ) <EOL> @ Appender ( _shared_docs [ '<STR_LIT:index>' ] % <EOL> dict ( side = '<STR_LIT>' , similar = '<STR_LIT>' , method = '<STR_LIT>' , <EOL> also = '<STR_LIT>' ) ) <EOL> def rindex ( self , sub , start = <NUM_LIT:0> , end = None ) : <EOL> result = str_index ( self . _data , sub , start = start , end = end , side = '<STR_LIT:right>' ) <EOL> return self . _wrap_result ( result ) <EOL> _shared_docs [ '<STR_LIT>' ] = ( """<STR_LIT>""" ) <EOL> len = _noarg_wrapper ( len , docstring = _shared_docs [ '<STR_LIT>' ] , dtype = int ) <EOL> _shared_docs [ '<STR_LIT>' ] = ( """<STR_LIT>""" ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> _shared_docs [ '<STR_LIT:title>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT:title>' ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , <EOL> method = '<STR_LIT>' ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> lower = _noarg_wrapper ( lambda x : x . lower ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> upper = _noarg_wrapper ( lambda x : x . upper ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> title = _noarg_wrapper ( lambda x : x . title ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT:title>' ] ) <EOL> capitalize = _noarg_wrapper ( lambda x : x . capitalize ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> swapcase = _noarg_wrapper ( lambda x : x . swapcase ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> _shared_docs [ '<STR_LIT>' ] = ( """<STR_LIT>""" ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> _shared_docs [ '<STR_LIT>' ] = dict ( type = '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> isalnum = _noarg_wrapper ( lambda x : x . isalnum ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> isalpha = _noarg_wrapper ( lambda x : x . isalpha ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> isdigit = _noarg_wrapper ( lambda x : x . isdigit ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> isspace = _noarg_wrapper ( lambda x : x . isspace ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> islower = _noarg_wrapper ( lambda x : x . islower ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> isupper = _noarg_wrapper ( lambda x : x . isupper ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> istitle = _noarg_wrapper ( lambda x : x . istitle ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> isnumeric = _noarg_wrapper ( lambda x : compat . u_safe ( x ) . isnumeric ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> isdecimal = _noarg_wrapper ( lambda x : compat . u_safe ( x ) . isdecimal ( ) , <EOL> docstring = _shared_docs [ '<STR_LIT>' ] % <EOL> _shared_docs [ '<STR_LIT>' ] ) <EOL> class StringAccessorMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def _make_str_accessor ( self ) : <EOL> from pandas . core . series import Series <EOL> from pandas . core . index import Index <EOL> if ( isinstance ( self , Series ) and <EOL> not ( ( is_categorical_dtype ( self . dtype ) and <EOL> is_object_dtype ( self . values . categories ) ) or <EOL> ( is_object_dtype ( self . dtype ) ) ) ) : <EOL> raise AttributeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif isinstance ( self , Index ) : <EOL> allowed_types = ( '<STR_LIT:string>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . inferred_type not in allowed_types : <EOL> message = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise AttributeError ( message ) <EOL> if self . nlevels > <NUM_LIT:1> : <EOL> message = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise AttributeError ( message ) <EOL> return StringMethods ( self ) <EOL> str = AccessorProperty ( StringMethods , _make_str_accessor ) <EOL> def _dir_additions ( self ) : <EOL> return set ( ) <EOL> def _dir_deletions ( self ) : <EOL> try : <EOL> getattr ( self , '<STR_LIT:str>' ) <EOL> except AttributeError : <EOL> return set ( [ '<STR_LIT:str>' ] ) <EOL> return set ( ) </s>
