@@ -1142,27 +1142,24 @@ def count(self, level=None):
11421142 -------
11431143 nobs : int or Series (if level specified)
11441144 """
1145- if level is not None :
1146- mask = notnull (self ._values )
1145+ from pandas .core .index import _get_na_value
11471146
1148- if isinstance ( level , compat . string_types ) :
1149- level = self . index . _get_level_number ( level )
1147+ if level is None :
1148+ return notnull ( _values_from_object ( self )). sum ( )
11501149
1151- level_index = self .index .levels [level ]
1150+ if isinstance (level , compat .string_types ):
1151+ level = self .index ._get_level_number (level )
11521152
1153- if len (self ) == 0 :
1154- return self ._constructor (0 , index = level_index )\
1155- .__finalize__ (self )
1153+ lev = self .index .levels [level ]
1154+ lab = np .array (self .index .labels [level ], subok = False , copy = True )
11561155
1157- # call cython function
1158- max_bin = len (level_index )
1159- labels = com ._ensure_int64 (self .index .labels [level ])
1160- counts = lib .count_level_1d (mask .view (np .uint8 ),
1161- labels , max_bin )
1162- return self ._constructor (counts ,
1163- index = level_index ).__finalize__ (self )
1156+ mask = lab == - 1
1157+ if mask .any ():
1158+ lab [mask ] = cnt = len (lev )
1159+ lev = lev .insert (cnt , _get_na_value (lev .dtype .type ))
11641160
1165- return notnull (_values_from_object (self )).sum ()
1161+ out = np .bincount (lab [notnull (self .values )], minlength = len (lev ))
1162+ return self ._constructor (out , index = lev ).__finalize__ (self )
11661163
11671164 def mode (self ):
11681165 """Returns the mode(s) of the dataset.
@@ -2104,7 +2101,7 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
21042101
21052102 >>> import pandas as pd
21062103 >>> import numpy as np
2107- >>> series = pd.Series([20, 21, 12], index=['London',
2104+ >>> series = pd.Series([20, 21, 12], index=['London',
21082105 ... 'New York','Helsinki'])
21092106 London 20
21102107 New York 21
@@ -2132,7 +2129,7 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
21322129 dtype: int64
21332130
21342131 Define a custom function that needs additional positional
2135- arguments and pass these additional arguments using the
2132+ arguments and pass these additional arguments using the
21362133 ``args`` keyword.
21372134
21382135 >>> def subtract_custom_value(x, custom_value):
@@ -2158,7 +2155,7 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
21582155 Helsinki 87
21592156 dtype: int64
21602157
2161- Use a function from the Numpy library.
2158+ Use a function from the Numpy library.
21622159
21632160 >>> series.apply(np.log)
21642161 London 2.995732
0 commit comments