5

Why does numpy return different results with missing values when using a Pandas series compared to accessing the series' values as in the following:

import pandas as pd
import numpy as np

data = pd.DataFrame(dict(a=[1, 2, 3, np.nan, np.nan, 6]))
np.sum(data['a'])

#12.0

np.sum(data['a'].values)
#nan
1
  • Based on the answer by @coldspeed, this isn't quite a duplicate. I'm willing to remove it though if it doesn't add anything. Commented Mar 12, 2019 at 20:37

1 Answer 1

5

Calling np.sum on a pandas Series delegates to Series.sum, which ignores NaNs when computing the sum (BY DEFAULT).

data['a'].sum()
# 12.0

np.sum(data['a'])
# 12.0

You can see this from the source code of np.sum:

np.sum??

def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue, initial=np._NoValue):
    ...
    return _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,

Taking a look at the source code for _wrapreduction, we see:

np.core.fromnumeric._wrapreduction??

def _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs):
    ...
    if type(obj) is not mu.ndarray:
        try:
            reduction = getattr(obj, method)   # get reference to Series.add 

reduction is then finally called at the end of the function:

            return reduction(axis=axis, out=out, **passkwargs)           
Sign up to request clarification or add additional context in comments.

7 Comments

Dang, beat me to it. Was just copying over the numpy source haha. +1
Thanks! This applies to other numpy functions acting on Pandas series as well (such as mean, min, max, median, etc.)?
@willk Yes, most ufuncs, actually. There might be exceptions that I am not aware of though.
There is a numpy function for summing up arrays with NaN: numpy.nansum(): Function is used when we want to compute the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.
Thanks. I guess the safest option is to use data['a'].sum(skipna=True) to be explicit. Or use the np.nansum function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.