b.sum() yields 0 while b.rolling(2).sum() yields nan, where b=pd.Series([np.nan, np.nan])

olivia

pandas.sum() will skip NaN by default, so I would expect b.rolling(2).sum() to also replace the NaN with 0. However, b.rolling(2).sum() yields NaN instead of 0, where b = pd.Series([np.nan, np.nan]).

mcskinner

The DataFrame.rolling method takes a min_periods parameter, which defaults to the size of the window. Until that many non-missing values are accumulated, the rolling calculation will leave the values missing. All your values are missing, so the calculation will never accumulate 2 values.

For this all-NaN case, you could just set the parameter to 0.

pd.Series([np.NaN, np.NaN]).rolling(2, min_periods=0).sum()                                                                                                                                                                         
# 0    0.0
# 1    0.0

But this will also change the behavior for series where the first two values are present. It will leave the first value in place, not empty to indicate that there weren't 2 values in the rolling sum yet.

pd.Series([1, 2]).rolling(2).sum()                                                                                                                                                                                                  
# 0    NaN
# 1    3.0

pd.Series([1, 2]).rolling(2, min_periods=0).sum()                                                                                                                                                                                   
# 0    1.0
# 1    3.0

To avoid that, I would need to know more about your use case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sum of dataframes : treating NaN as 0 when summed with other values, but returning NaN where all summed elements are NaN

.agg Sum Converting NaN to 0

How to list values from column A where column B is NaN?

Adding series to Pandas dataframe yields column of NaN

Scipy griddata with 'linear' and 'cubic' yields nan

Trying to divide a dataframe column by a float yields NaN

Assign first element of groupby to a column yields NaN

Group by and find sum for groups but return NaN as NaN, not 0

NaN or 0 shows up in sum up

Is *a+=*b same as int sum = *a+*b where sum = *a?

How to sum 2 pandas Series with diffent rows treating NaN as 0

Array sum returns NaN

How to sum NaN in numpy?

How to Sum Column (A) and Column (B) split into B and B2

Cosine similarity yields 'nan' values pt.II

Sum of NaNs to equal NaN (not zero)

matlab sum function of NAN values

Javascript Sum function return NaN

Javascript - Sum of an array equals NaN

Calculate the sum of values replacing NaN

Python find index of nan in nan-list yields error only sometimes?

Collect values of pandas dataframe column A if column B is NaN (Python)

Pandas pivot_table replaces nan with 0 aggfunc='sum'

Pandas: Sum multiple columns, but write NaN if any column in that row is NaN or 0

How can I make NaN values sum to NaN rather than 0 when using df.resample?

Make 2 arrays ($B and $C) using $A elements that [ (sum of $B elements) - (sum of $C elements) ] equals minimum number (0 if possible)?

Given 2 arrays a and b, return an array that represent sum of a and b

What does `input.Sum(b => b - '0')` mean and why does it work without defining `b`?

Pandas sum and agg(sum) yields different values