Skip to content

Commit 4a218da

Browse files
committed
ENH: speed boost in count by using Series.count
1 parent 6383436 commit 4a218da

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

pandas/core/frame.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,10 +2387,18 @@ def count(self, axis=0, level=None, numeric_only=False):
23872387
return self._count_level(level, axis=axis,
23882388
numeric_only=numeric_only)
23892389

2390-
y, axis_labels = self._get_agg_data(axis, numeric_only=numeric_only,
2391-
copy=False)
2392-
mask = notnull(y)
2393-
return Series(mask.sum(axis), index=axis_labels)
2390+
if numeric_only:
2391+
frame = self.ix[:, self._get_numeric_columns()]
2392+
else:
2393+
frame = self
2394+
2395+
result = frame.apply(Series.count, axis=axis)
2396+
2397+
# what happens with empty DataFrame
2398+
if isinstance(result, DataFrame):
2399+
result = Series({})
2400+
2401+
return result
23942402

23952403
def _count_level(self, level, axis=0, numeric_only=False):
23962404
# TODO: deal with sortedness??

0 commit comments

Comments
 (0)