Q:

Pandas datetime resample count non-zero

I have a time series of daily rainfall data that looks like this:
                     PRCP
year_month_day           
1797-01-01 00:00:00   0.0
1797-01-02 00:00:00   0.0
1797-01-03 00:00:00   1.1
1797-01-04 00:00:00   0.0
1797-01-05 00:00:00   3.5
1797-02-01 00:00:00   8.1
1797-02-02 00:00:00   3.0
1797-02-03 00:00:00   0.0
1797-02-04 00:00:00   0.0
1797-02-05 00:00:00   0.0
1797-03-01 00:00:00   0.0
1797-03-02 00:00:00   0.0
1797-03-03 00:00:00   0.0
1797-03-04 00:00:00   0.0
1797-03-05 00:00:00   1.5
1797-04-01 00:00:00   6.3
1797-04-02 00:00:00  24.0
1797-04-03 00:00:00   0.0
1797-04-04 00:00:00   2.2
1797-04-05 00:00:00   5.9
1797-05-01 00:00:00   0.0
1797-05-02 00:00:00  15.9
1797-05-03 00:00:00   0.0
1797-05-04 00:00:00   0.0
1797-05-05 00:00:00   0.0
1797-06-01 00:00:00   1.6
1797-06-02 00:00:00   0.0
1797-06-03 00:00:00   0.0
1797-06-04 00:00:00   7.9
1797-06-05 00:00:00   0.0

I have been able to import it with the index column as a pandas datetime object. I am trying to count all of the non-zero raindays per month. I can group by month with:
grouped = df.groupby(pd.Grouper(freq='M'))

and can count everything per month with:
raindays = grouped.resample("M").count()

But that also counts days with 0 rainfall. I found hints about using nunique(), but it doesn't seem to work with resample. eg:
raindays = grouped.resample("M").nunique()

returns error:
AttributeError: 'DataFrameGroupBy' object has no attribute 'nunique'

Is there a way to count non zero values in a grouped pandas object?

A:

Mask those 0s and try again.
df.mask(df.PRCP.eq(0)).groupby(pd.Grouper(freq='M')).count()

Or, the more obvious version with replace.
df.replace({0 : np.nan}).groupby(pd.Grouper(freq='M')).count()

                PRCP
year_month_day      
1797-01-31         2
1797-02-28         2
1797-03-31         1
1797-04-30         4
1797-05-31         1
1797-06-30         2

