Skip to content

Commit 049b897

Browse files
committed
Mask NaNs in index and dataframe values.
1 parent d6df0f0 commit 049b897

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

pandas/core/frame.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -7820,13 +7820,21 @@ def _count_level(self, level, axis=0, numeric_only=False):
78207820
f"Can only count levels on hierarchical {self._get_axis_name(axis)}."
78217821
)
78227822

7823+
# Mask NaNs: Mask rows where the index level is NaN and all values in
7824+
# the DataFrame that are NaN
78237825
if frame._is_mixed_type:
78247826
# Since we have mixed types, calling notna(frame.values) might
78257827
# upcast everything to object
7826-
mask = notna(frame).values
7828+
mask = (
7829+
notna(frame.index.get_level_values(level=level)).reshape(-1, 1) &
7830+
notna(frame).values
7831+
)
78277832
else:
78287833
# But use the speedup when we have homogeneous dtypes
7829-
mask = notna(frame.values)
7834+
mask = (
7835+
notna(frame.index.get_level_values(level=level)).reshape(-1, 1) &
7836+
notna(frame.values)
7837+
)
78307838

78317839
if axis == 1:
78327840
# We're transposing the mask rather than frame to avoid potential

0 commit comments

Comments
 (0)