Skip to content

Commit

Permalink
BUG: prevent uint64->int64 casting overflows. close #2355
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Nov 28, 2012
1 parent 238f522 commit 6c6dae7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,11 @@ def form_blocks(arrays, names, axes):
else:
datetime_items.append((k, v))
elif issubclass(v.dtype.type, np.integer):
if v.dtype == np.uint64:
# HACK #2355 definite overflow
if (v > 2**63 - 1).any():
object_items.append((k, v))
continue
int_items.append((k, v))
elif v.dtype == np.bool_:
bool_items.append((k, v))
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,24 @@ def test_constructor_bool(self):
1 : np.zeros(10, dtype=bool)})
self.assertEqual(df.values.dtype, np.bool_)

def test_constructor_overflow_int64(self):
values = np.array([2**64 - i for i in range(1, 10)],
dtype=np.uint64)

result = DataFrame({'a': values})
self.assert_(result['a'].dtype == object)

# #2355
data_scores = [(6311132704823138710, 273), (2685045978526272070, 23),
(8921811264899370420, 45), (17019687244989530680L, 270),
(9930107427299601010L, 273)]
dtype = [('uid', 'u8'), ('score', 'u8')]
data = np.zeros((len(data_scores),),dtype=dtype)
data[:] = data_scores
df_crawls = DataFrame(data)
self.assert_(df_crawls['uid'].dtype == object)


def test_is_mixed_type(self):
self.assert_(not self.frame._is_mixed_type)
self.assert_(self.mixed_frame._is_mixed_type)
Expand Down

0 comments on commit 6c6dae7

Please sign in to comment.