From 12016feb8770c91d0c6c2d63a6fa6792b172c80e Mon Sep 17 00:00:00 2001 From: gfyoung Date: Thu, 7 Jun 2018 22:23:53 -0700 Subject: [PATCH 1/2] MAINT: More useful error msg on Index overflow Display a more friendly error message when there is an OverflowError during Index construction. Partially addresses gh-15832. --- pandas/core/indexes/base.py | 9 ++++++++- pandas/tests/indexes/test_base.py | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index dff6b5421d5ab..691142fc69ff6 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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 ' diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index f9f16dc0ce8b7..c264f5f79e47e 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -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', From 44e95e6005bb175ec8fa269991bf40f65186fc34 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Thu, 7 Jun 2018 22:30:18 -0700 Subject: [PATCH 2/2] DOC: Clarify how Index.__new__ handles dtype Partially addresses gh-15823. --- pandas/core/indexes/base.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 691142fc69ff6..bf1051332ee19 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -181,6 +181,9 @@ class Index(IndexOpsMixin, PandasObject): ---------- data : array-like (1-dimensional) dtype : NumPy dtype (default: object) + If dtype is None, we find the dtype that best fits the data. + If an actual dtype is provided, we coerce to that dtype if it's safe. + Otherwise, an error will be raised. copy : bool Make a copy of input ndarray name : object