0

I have a Pandas dataframe with columns of type float64

I try to compute apply sum function on some columns by numpy.sum

When I active the function np.sum(x[col_name]) I receiving the result of 'inf'

But when I check where is the 'inf' value by np.where(np.isinf(x[col_name])) I received empty results.

So, What I do wrong...

Thanks.

8
  • Can you provide a data sample? Commented Apr 20, 2020 at 20:28
  • No, unfortunately, the data is restricted. I look on the data, the data do not contain any inf number Commented Apr 20, 2020 at 20:30
  • Then I don't know what to say much more. Have a look at np.nansum: docs.scipy.org/doc/numpy/reference/generated/numpy.nansum.html It treats NaNs as 0 Commented Apr 20, 2020 at 20:37
  • Ok, After I digging in the data I found number like 1.79600000007e+308 the np is not recognized this number as nan and not as inf, Commented Apr 20, 2020 at 20:48
  • 1
    Try this: replace float64 with dtype=object Commented Apr 20, 2020 at 21:10

1 Answer 1

2

The problem appears to be that one of the numbers in your data, is bigger than the max np.float64 accepts. If you run, np.finfo(np.float64), you'll see the biggest number this dtype accepts:

Machine parameters for float64
---------------------------------------------------------------
precision =  15   resolution = 1.0000000000000001e-15
machep =    -52   eps =        2.2204460492503131e-16
negep =     -53   epsneg =     1.1102230246251565e-16
minexp =  -1022   tiny =       2.2250738585072014e-308
maxexp =   1024   max =        1.7976931348623157e+308
nexp =       11   min =        -max
--------------------------------------------------------------

According to this answer: https://stackoverflow.com/a/37272717/4014051 python objects use an arbitrary length implementation, therefore the solution would be to make the dtype of your array object. This means that your code will be slower overall, as your data are not numpy objects, but presumably it will output the correct sum.

Sign up to request clarification or add additional context in comments.

Comments

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.