Skip to content

Commit

Permalink
MAINT: More useful error msg on Index overflow
Browse files Browse the repository at this point in the history
Display a more friendly error message when there
is an OverflowError during Index construction.

Partially addresses pandas-devgh-15832.
  • Loading branch information
gfyoung committed Jun 8, 2018
1 parent abfac97 commit 18d3433
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,14 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None,
if is_integer_dtype(dtype):
inferred = lib.infer_dtype(data)
if inferred == 'integer':
data = np.array(data, copy=copy, dtype=dtype)
try:
data = np.array(data, copy=copy, dtype=dtype)
except OverflowError:
# gh-15823: a more user-friendly error message
raise OverflowError(
"the elements provided in the data cannot "
"all be casted to the dtype {dtype}"
.format(dtype=dtype))
elif inferred in ['floating', 'mixed-integer-float']:
if isna(data).any():
raise ValueError('cannot convert float '
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,13 @@ def test_constructor_nonhashable_name(self, indices):
tm.assert_raises_regex(TypeError, message,
indices.set_names, names=renamed)

def test_constructor_overflow_int64(self):
# see gh-15832
msg = ("the elements provided in the data cannot "
"all be casted to the dtype int64")
with tm.assert_raises_regex(OverflowError, msg):
Index([np.iinfo(np.uint64).max - 1], dtype="int64")

def test_view_with_args(self):

restricted = ['unicodeIndex', 'strIndex', 'catIndex', 'boolIndex',
Expand Down

0 comments on commit 18d3433

Please sign in to comment.