I am reading in a bunch of CSV files (measurement data for water levels over time) to do various analysis and visualizations on them.
Due to various reasons beyond my control, these time series often have missing data, so I do two things:
I count them in total with
Rlength = len(RainD) # Counts everything, including NaN
Rcount = RainD.count() # Counts only valid numbers
NaN_Number = Rlength - Rcount
and discard the dataset if I have more missing data than a certain threshold:
Percent_Data = Rlength/100
Five_Percent = Percent_Data*5
if NaN_Number > Five_Percent:
...
If the number of NaN is sufficiently small, I would like to fill the gaps with
RainD.level = RainD.level.fillna(method='pad', limit=2)
And now for the issue: It's monthly data, so if I have more than two consecutive NaNs, I also want to discard the data, since that would mean that I "guess" a whole season, or even more.
The documentation for fillna
doesn't really mention what happens when there is more consecutive NaNs than my specified limit=2
, but when I look at RainD.describe()
before and after ...fillna...
and compare it with the base CSV, it's clear that it fills the first two NaNs, and then leaves the rest as it is, instead of erroring out.
So, long story short:
How do I identify a number of consecutive NaNs with Pandas, without some complicated and time consuming non-Pandas loop?