8

Let me start with the example code:

import numpy
from pandas import DataFrame

a = DataFrame({"nums": [2233, -23160, -43608]})

a.nums = numpy.int64(a.nums)

print(a.nums ** 2)
print((a.nums ** 2).sum())

On my local machine, and other devs' machines, this works as expected and prints out:

0       4986289
1     536385600
2    1901657664
Name: nums, dtype: int64
2443029553

However, on our production server, we get:

0       4986289
1     536385600
2    1901657664
Name: nums, dtype: int64
-1851937743

Which is 32-bit integer overflow, despite it being an int64.

The production server is using the same versions of python, numpy, pandas, etc. It's a 64-bit Windows Server 2012 OS and everything reports 64-bit (e.g. python --version, sys.maxsize, plastform.architecture).

What could possibly be causing this?

13
  • Why don't you use regular Python integers that are capable of representing arbitrarily large numbers? Commented Apr 20, 2017 at 16:55
  • 4
    @ForceBru: They're slow, bulky, and cause weird breakages if you try to use object arrays full of integer objects. Commented Apr 20, 2017 at 17:03
  • 1
    Does one of the machines have bottleneck installed? Commented Apr 20, 2017 at 17:15
  • 1
    What is the output of print((a.nums.values**2).sum(dtype=np.int64))? Commented Apr 20, 2017 at 17:22
  • 2
    @SeanKramer: I just started digging through the code and wound up in bottleneck. I think bottleneck is mishandling numpy.int64 on platforms where a C long is 32-bit, and Pandas is getting a check wrong in its attempts to compensate for bottleneck's error. Commented Apr 20, 2017 at 17:32

1 Answer 1

5

This is a bug in the bottleneck library, which Pandas uses if it's installed. In some circumstances, bottleneck.nansum incorrectly has 32-bit overflow behavior when called on 64-bit input.

I believe this is due to bottleneck using PyInt_FromLong even when long is 32-bit. I'm not sure why that even compiles, actually. There's an issue report on the bottleneck issue tracker, not yet fixed, as well as an issue report on the Pandas issue tracker, where they tried to compensate for Bottleneck's issue (but I think they turned off Bottleneck when it does work instead of when it doesn't).

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.