Skip to content

Commit 6c6dae7

Browse files
committed
BUG: prevent uint64->int64 casting overflows. close #2355
1 parent 238f522 commit 6c6dae7

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

pandas/core/internals.py

+5
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,11 @@ def form_blocks(arrays, names, axes):
13071307
else:
13081308
datetime_items.append((k, v))
13091309
elif issubclass(v.dtype.type, np.integer):
1310+
if v.dtype == np.uint64:
1311+
# HACK #2355 definite overflow
1312+
if (v > 2**63 - 1).any():
1313+
object_items.append((k, v))
1314+
continue
13101315
int_items.append((k, v))
13111316
elif v.dtype == np.bool_:
13121317
bool_items.append((k, v))

pandas/tests/test_frame.py

+18
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,24 @@ def test_constructor_bool(self):
17621762
1 : np.zeros(10, dtype=bool)})
17631763
self.assertEqual(df.values.dtype, np.bool_)
17641764

1765+
def test_constructor_overflow_int64(self):
1766+
values = np.array([2**64 - i for i in range(1, 10)],
1767+
dtype=np.uint64)
1768+
1769+
result = DataFrame({'a': values})
1770+
self.assert_(result['a'].dtype == object)
1771+
1772+
# #2355
1773+
data_scores = [(6311132704823138710, 273), (2685045978526272070, 23),
1774+
(8921811264899370420, 45), (17019687244989530680L, 270),
1775+
(9930107427299601010L, 273)]
1776+
dtype = [('uid', 'u8'), ('score', 'u8')]
1777+
data = np.zeros((len(data_scores),),dtype=dtype)
1778+
data[:] = data_scores
1779+
df_crawls = DataFrame(data)
1780+
self.assert_(df_crawls['uid'].dtype == object)
1781+
1782+
17651783
def test_is_mixed_type(self):
17661784
self.assert_(not self.frame._is_mixed_type)
17671785
self.assert_(self.mixed_frame._is_mixed_type)

0 commit comments

Comments
 (0)