@@ -1890,18 +1890,14 @@ def rindex(self, sub, start=0, end=None):
1890
1890
docstring = _shared_docs ['ismethods' ] %
1891
1891
_shared_docs ['isdecimal' ])
1892
1892
1893
-
1894
- class StringAccessorMixin (object ):
1895
- """ Mixin to add a `.str` acessor to the class."""
1896
-
1897
- # string methods
1898
- def _make_str_accessor (self ):
1893
+ @classmethod
1894
+ def _make_accessor (cls , data ):
1899
1895
from pandas .core .index import Index
1900
1896
1901
- if (isinstance (self , ABCSeries ) and
1902
- not ((is_categorical_dtype (self .dtype ) and
1903
- is_object_dtype (self .values .categories )) or
1904
- (is_object_dtype (self .dtype )))):
1897
+ if (isinstance (data , ABCSeries ) and
1898
+ not ((is_categorical_dtype (data .dtype ) and
1899
+ is_object_dtype (data .values .categories )) or
1900
+ (is_object_dtype (data .dtype )))):
1905
1901
# it's neither a string series not a categorical series with
1906
1902
# strings inside the categories.
1907
1903
# this really should exclude all series with any non-string values
@@ -1910,23 +1906,27 @@ def _make_str_accessor(self):
1910
1906
raise AttributeError ("Can only use .str accessor with string "
1911
1907
"values, which use np.object_ dtype in "
1912
1908
"pandas" )
1913
- elif isinstance (self , Index ):
1909
+ elif isinstance (data , Index ):
1914
1910
# can't use ABCIndex to exclude non-str
1915
1911
1916
1912
# see scc/inferrence.pyx which can contain string values
1917
1913
allowed_types = ('string' , 'unicode' , 'mixed' , 'mixed-integer' )
1918
- if self .inferred_type not in allowed_types :
1914
+ if data .inferred_type not in allowed_types :
1919
1915
message = ("Can only use .str accessor with string values "
1920
1916
"(i.e. inferred_type is 'string', 'unicode' or "
1921
1917
"'mixed')" )
1922
1918
raise AttributeError (message )
1923
- if self .nlevels > 1 :
1919
+ if data .nlevels > 1 :
1924
1920
message = ("Can only use .str accessor with Index, not "
1925
1921
"MultiIndex" )
1926
1922
raise AttributeError (message )
1927
- return StringMethods (self )
1923
+ return StringMethods (data )
1924
+
1925
+
1926
+ class StringAccessorMixin (object ):
1927
+ """ Mixin to add a `.str` acessor to the class."""
1928
1928
1929
- str = AccessorProperty (StringMethods , _make_str_accessor )
1929
+ str = AccessorProperty (StringMethods )
1930
1930
1931
1931
def _dir_additions (self ):
1932
1932
return set ()
0 commit comments