From 5bd9720aa9becaab7926968c3b9340822cf05bec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Sun, 10 Nov 2019 12:22:56 +0100 Subject: [PATCH 01/18] adds test case --- pandas/tests/indexes/test_base.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index e43d340a46d9f..5102a034f2821 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -2821,3 +2821,10 @@ def test_shape_of_invalid_index(): idx = pd.Index([0, 1, 2, 3]) assert idx[:, None].shape == (4, 1) + +def test_index_construction_respects_dtype(): + index_list = [7606741985629028552, 17876870360202815256] + expected = np.asarray(index_list, dtype='uint64') + result = np.asarray(UInt64Index(index_list, dtype='uint64'), dtype='uint64') + + tm.assert_equal(expected, result) \ No newline at end of file From 5ad038fe137237c4fb5001a4fd2d7a8fc75a799a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Sun, 10 Nov 2019 12:24:51 +0100 Subject: [PATCH 02/18] Format file --- pandas/tests/indexes/test_base.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 5102a034f2821..4f129c185d2ed 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -2822,9 +2822,10 @@ def test_shape_of_invalid_index(): idx = pd.Index([0, 1, 2, 3]) assert idx[:, None].shape == (4, 1) + def test_index_construction_respects_dtype(): index_list = [7606741985629028552, 17876870360202815256] - expected = np.asarray(index_list, dtype='uint64') - result = np.asarray(UInt64Index(index_list, dtype='uint64'), dtype='uint64') + expected = np.asarray(index_list, dtype="uint64") + result = np.asarray(UInt64Index(index_list, dtype="uint64"), dtype="uint64") - tm.assert_equal(expected, result) \ No newline at end of file + tm.assert_equal(expected, result) From d620c519dbc1d8dbb4ab2c614fa5cf06a0ac33db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Sun, 10 Nov 2019 12:43:33 +0100 Subject: [PATCH 03/18] Fixes #29526 --- pandas/core/indexes/base.py | 5 +++-- pandas/core/indexes/numeric.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index c9697c530628a..d270abac6cb54 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4027,7 +4027,7 @@ def _string_data_error(cls, data): ) @classmethod - def _coerce_to_ndarray(cls, data): + def _coerce_to_ndarray(cls, data, dtype=None): """ Coerces data to ndarray. @@ -4047,7 +4047,8 @@ def _coerce_to_ndarray(cls, data): # other iterable of some kind if not isinstance(data, (ABCSeries, list, tuple)): data = list(data) - data = np.asarray(data) + + data = np.asarray(data, dtype=dtype) return data def _coerce_scalar_to_index(self, item): diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 46bb8eafee3b9..8b0ecf9263755 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -57,7 +57,7 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=None): return cls._simple_new(data, name=name) # is_scalar, generators handled in coerce_to_ndarray - data = cls._coerce_to_ndarray(data) + data = cls._coerce_to_ndarray(data, dtype=dtype) if issubclass(data.dtype.type, str): cls._string_data_error(data) From efc846dcff2bea530ba79abe99297939aca343d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Mon, 11 Nov 2019 22:23:58 +0100 Subject: [PATCH 04/18] Move tests --- pandas/tests/indexes/test_base.py | 8 -------- pandas/tests/indexes/test_numeric.py | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 4f129c185d2ed..e43d340a46d9f 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -2821,11 +2821,3 @@ def test_shape_of_invalid_index(): idx = pd.Index([0, 1, 2, 3]) assert idx[:, None].shape == (4, 1) - - -def test_index_construction_respects_dtype(): - index_list = [7606741985629028552, 17876870360202815256] - expected = np.asarray(index_list, dtype="uint64") - result = np.asarray(UInt64Index(index_list, dtype="uint64"), dtype="uint64") - - tm.assert_equal(expected, result) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index e424b3601a4b2..ab48bc381d149 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -944,6 +944,11 @@ def test_constructor(self): res = Index(np.array([-1, 2 ** 63], dtype=object)) tm.assert_index_equal(res, idx) + # https://github.com/pandas-dev/pandas/issues/29526 + idx = pd.UInt64Index([1, 2**63 + 1]) + res = pd.UInt64Index(np.array([1, 2**63 + 1], dtype="uint64")) + tm.assert_index_equal(res, idx) + def test_get_indexer(self, index_large): target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2 ** 63) indexer = index_large.get_indexer(target) @@ -1187,3 +1192,13 @@ def test_range_float_union_dtype(): result = other.union(index) tm.assert_index_equal(result, expected) + + +def test_index_construction_respects_dtype(): + # https://github.com/pandas-dev/pandas/issues/29526 + index_list = [1, 2**63 + 1] + + result = UInt64Index(index_list) + expected = UInt64Index(np.array(index_list, dtype="uint64")) + + tm.assert_equal(expected, result) From a59a9457c562da540a83726495dc11dd10d71c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Mon, 11 Nov 2019 22:25:07 +0100 Subject: [PATCH 05/18] Suggested improvements --- pandas/core/indexes/base.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index d270abac6cb54..57c8e318572ff 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4040,16 +4040,17 @@ def _coerce_to_ndarray(cls, data, dtype=None): When the data passed in is a scalar. """ - if not isinstance(data, (np.ndarray, Index)): - if data is None or is_scalar(data): - raise cls._scalar_data_error(data) + if isinstance(data, (np.ndarray, Index)): + return data - # other iterable of some kind - if not isinstance(data, (ABCSeries, list, tuple)): - data = list(data) + if is_scalar(data): + raise cls._scalar_data_error(data) + + # other iterable of some kind + if not isinstance(data, (ABCSeries, list, tuple)): + data = list(data) - data = np.asarray(data, dtype=dtype) - return data + return np.asarray(data, dtype=dtype) def _coerce_scalar_to_index(self, item): """ From 5c7c65c5e54de040f821ba48fb3d584100a77c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Mon, 11 Nov 2019 22:27:46 +0100 Subject: [PATCH 06/18] Does not rely on explicit arguments --- pandas/core/indexes/numeric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 8b0ecf9263755..113a36ae3439c 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -57,7 +57,7 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=None): return cls._simple_new(data, name=name) # is_scalar, generators handled in coerce_to_ndarray - data = cls._coerce_to_ndarray(data, dtype=dtype) + data = cls._coerce_to_ndarray(data, dtype=cls._default_dtype) if issubclass(data.dtype.type, str): cls._string_data_error(data) From 9ab3596ff970008706acaf6fb01191fd45526505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Mon, 11 Nov 2019 22:29:04 +0100 Subject: [PATCH 07/18] Test directly, second pass --- pandas/tests/indexes/test_numeric.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index ab48bc381d149..95d9636765d34 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -945,8 +945,8 @@ def test_constructor(self): tm.assert_index_equal(res, idx) # https://github.com/pandas-dev/pandas/issues/29526 - idx = pd.UInt64Index([1, 2**63 + 1]) - res = pd.UInt64Index(np.array([1, 2**63 + 1], dtype="uint64")) + idx = UInt64Index([1, 2**63 + 1]) + res = Index([1, 2 ** 63 + 1], dtype=np.uint64) tm.assert_index_equal(res, idx) def test_get_indexer(self, index_large): From 14e4e0399fd606a226f314f3981ffca03c269c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Mon, 11 Nov 2019 22:42:36 +0100 Subject: [PATCH 08/18] Adds what's new entry --- doc/source/whatsnew/v1.0.0.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 664fcc91bacc4..0c1af97660986 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -336,7 +336,8 @@ Numeric - Bug in :class:`DataFrame` logical operations (`&`, `|`, `^`) not matching :class:`Series` behavior by filling NA values (:issue:`28741`) - Bug in :meth:`DataFrame.interpolate` where specifying axis by name references variable before it is assigned (:issue:`29142`) - Improved error message when using `frac` > 1 and `replace` = False (:issue:`27451`) -- +- Bug in :class:`UInt64Index` precision loss while constructing from a list with values in the ``np.uint64`` range (:issue:`29526`) +- Conversion ^^^^^^^^^^ From 68fe30438024f76e2b4cb2fd5d59a4186b857459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Thu, 14 Nov 2019 22:30:23 +0100 Subject: [PATCH 09/18] Remove tailing whitespace in whatsnew entry --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 0c1af97660986..b26e2704d08db 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -337,7 +337,7 @@ Numeric - Bug in :meth:`DataFrame.interpolate` where specifying axis by name references variable before it is assigned (:issue:`29142`) - Improved error message when using `frac` > 1 and `replace` = False (:issue:`27451`) - Bug in :class:`UInt64Index` precision loss while constructing from a list with values in the ``np.uint64`` range (:issue:`29526`) -- +- Conversion ^^^^^^^^^^ From 458c25e89d8f44ce61e01e58796471b265436276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Thu, 14 Nov 2019 23:11:12 +0100 Subject: [PATCH 10/18] Move string check into Index._coerce_to_ndarray --- pandas/core/indexes/base.py | 3 +++ pandas/core/indexes/numeric.py | 5 +---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 57c8e318572ff..fa6f162eab4d5 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4046,6 +4046,9 @@ def _coerce_to_ndarray(cls, data, dtype=None): if is_scalar(data): raise cls._scalar_data_error(data) + if lib.infer_dtype(data) == 'string': + raise cls._string_data_error(data) + # other iterable of some kind if not isinstance(data, (ABCSeries, list, tuple)): data = list(data) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 113a36ae3439c..c00028828c360 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -56,12 +56,9 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=None): if fastpath: return cls._simple_new(data, name=name) - # is_scalar, generators handled in coerce_to_ndarray + # is_scalar, generators and strings are handled in coerce_to_ndarray data = cls._coerce_to_ndarray(data, dtype=cls._default_dtype) - if issubclass(data.dtype.type, str): - cls._string_data_error(data) - if copy or not is_dtype_equal(data.dtype, cls._default_dtype): subarr = np.array(data, dtype=cls._default_dtype, copy=copy) cls._assert_safe_casting(data, subarr) From 1df13c0023371b1ba6584c186c036754fc8172eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Sat, 16 Nov 2019 17:41:40 +0100 Subject: [PATCH 11/18] Reverts 458c25e89 and 5c7c65c5e --- pandas/core/indexes/base.py | 3 --- pandas/core/indexes/numeric.py | 7 +++++-- pandas/tests/indexes/test_numeric.py | 12 +----------- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index fa6f162eab4d5..57c8e318572ff 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4046,9 +4046,6 @@ def _coerce_to_ndarray(cls, data, dtype=None): if is_scalar(data): raise cls._scalar_data_error(data) - if lib.infer_dtype(data) == 'string': - raise cls._string_data_error(data) - # other iterable of some kind if not isinstance(data, (ABCSeries, list, tuple)): data = list(data) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index c00028828c360..00df65b7ccceb 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -56,8 +56,11 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=None): if fastpath: return cls._simple_new(data, name=name) - # is_scalar, generators and strings are handled in coerce_to_ndarray - data = cls._coerce_to_ndarray(data, dtype=cls._default_dtype) + # is_scalar, generators handled in coerce_to_ndarray + data = cls._coerce_to_ndarray(data, dtype=dtype) + + if issubclass(data.dtype.type, str): + cls._string_data_error(data) if copy or not is_dtype_equal(data.dtype, cls._default_dtype): subarr = np.array(data, dtype=cls._default_dtype, copy=copy) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index 95d9636765d34..4f4e44d6abe86 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -945,7 +945,7 @@ def test_constructor(self): tm.assert_index_equal(res, idx) # https://github.com/pandas-dev/pandas/issues/29526 - idx = UInt64Index([1, 2**63 + 1]) + idx = UInt64Index([1, 2**63 + 1], dtype=np.uint64) res = Index([1, 2 ** 63 + 1], dtype=np.uint64) tm.assert_index_equal(res, idx) @@ -1192,13 +1192,3 @@ def test_range_float_union_dtype(): result = other.union(index) tm.assert_index_equal(result, expected) - - -def test_index_construction_respects_dtype(): - # https://github.com/pandas-dev/pandas/issues/29526 - index_list = [1, 2**63 + 1] - - result = UInt64Index(index_list) - expected = UInt64Index(np.array(index_list, dtype="uint64")) - - tm.assert_equal(expected, result) From e6d5f6bbfadfa3ae7bedc831faf5951f94f86e6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Sat, 16 Nov 2019 17:44:46 +0100 Subject: [PATCH 12/18] Fix formatting --- pandas/core/indexes/numeric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 00df65b7ccceb..8b0ecf9263755 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -56,7 +56,7 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=None): if fastpath: return cls._simple_new(data, name=name) - # is_scalar, generators handled in coerce_to_ndarray + # is_scalar, generators handled in coerce_to_ndarray data = cls._coerce_to_ndarray(data, dtype=dtype) if issubclass(data.dtype.type, str): From a51bcecb52b6838be17443da05675db649a34883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Sat, 16 Nov 2019 18:36:59 +0100 Subject: [PATCH 13/18] Fix formatting in test_numeric.py --- pandas/tests/indexes/test_numeric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index 4f4e44d6abe86..bb768e8a41079 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -945,7 +945,7 @@ def test_constructor(self): tm.assert_index_equal(res, idx) # https://github.com/pandas-dev/pandas/issues/29526 - idx = UInt64Index([1, 2**63 + 1], dtype=np.uint64) + idx = UInt64Index([1, 2 ** 63 + 1], dtype=np.uint64) res = Index([1, 2 ** 63 + 1], dtype=np.uint64) tm.assert_index_equal(res, idx) From 13bda846351f969c0157bd4e064af25f9c36cda5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Sun, 17 Nov 2019 10:56:12 +0100 Subject: [PATCH 14/18] Moves _coerce_to_ndarray definition to NumericIndex --- pandas/core/indexes/base.py | 26 -------------------------- pandas/core/indexes/numeric.py | 27 +++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 57c8e318572ff..9cae9e6aac91b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4026,32 +4026,6 @@ def _string_data_error(cls, data): "to explicitly cast to a numeric type" ) - @classmethod - def _coerce_to_ndarray(cls, data, dtype=None): - """ - Coerces data to ndarray. - - Converts other iterables to list first and then to array. - Does not touch ndarrays. - - Raises - ------ - TypeError - When the data passed in is a scalar. - """ - - if isinstance(data, (np.ndarray, Index)): - return data - - if is_scalar(data): - raise cls._scalar_data_error(data) - - # other iterable of some kind - if not isinstance(data, (ABCSeries, list, tuple)): - data = list(data) - - return np.asarray(data, dtype=dtype) - def _coerce_scalar_to_index(self, item): """ We need to coerce a scalar to a compat for our index type. diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 8b0ecf9263755..f94d7a1aa73be 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -22,6 +22,7 @@ ABCFloat64Index, ABCInt64Index, ABCRangeIndex, + ABCSeries, ABCUInt64Index, ) from pandas.core.dtypes.missing import isna @@ -128,6 +129,32 @@ def _assert_safe_casting(cls, data, subarr): """ pass + @classmethod + def _coerce_to_ndarray(cls, data, dtype=None): + """ + Coerces data to ndarray. + + Converts other iterables to list first and then to array. + Does not touch ndarrays. + + Raises + ------ + TypeError + When the data passed in is a scalar. + """ + + if isinstance(data, (np.ndarray, Index)): + return data + + if is_scalar(data): + raise cls._scalar_data_error(data) + + # other iterable of some kind + if not isinstance(data, (ABCSeries, list, tuple)): + data = list(data) + + return np.asarray(data, dtype=dtype) + def _concat_same_dtype(self, indexes, name): result = type(indexes[0])(np.concatenate([x._values for x in indexes])) return result.rename(name) From 60b4f7bfefaad7baac1e5e0ab86a14b701c30c3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Sun, 17 Nov 2019 12:46:20 +0100 Subject: [PATCH 15/18] Adds type hint for dtype and requires it --- pandas/core/indexes/numeric.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index f94d7a1aa73be..52a7398186421 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -1,3 +1,4 @@ +from typing import Optional import warnings import numpy as np @@ -130,7 +131,7 @@ def _assert_safe_casting(cls, data, subarr): pass @classmethod - def _coerce_to_ndarray(cls, data, dtype=None): + def _coerce_to_ndarray(cls, data, dtype: Optional[np.dtype]): """ Coerces data to ndarray. From b41893de0a1d5ad62bae150731d9a70a253916d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Sun, 17 Nov 2019 18:21:41 +0100 Subject: [PATCH 16/18] Adds docstring --- pandas/core/indexes/numeric.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 52a7398186421..39eace39528f6 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -133,11 +133,24 @@ def _assert_safe_casting(cls, data, subarr): @classmethod def _coerce_to_ndarray(cls, data, dtype: Optional[np.dtype]): """ - Coerces data to ndarray. + Coerce data to ndarray unless it's instance of pd.Index. Converts other iterables to list first and then to array. Does not touch ndarrays. + Parameters + ---------- + data : array-like (1-dimensional) + dtype : numpy.dtype or None + If a numpy.dtype is passed, data will be casted to the type when + possible. + + Returns + ------- + numpy.ndarray or pd.Index + Numpy.ndarray created from data, or data itself if an instance of + pd.Index is passed. + Raises ------ TypeError From c9db70477ff2f30a771f02614749ef8f32b6c9c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Sun, 17 Nov 2019 22:17:43 +0100 Subject: [PATCH 17/18] Fixes typo in docstring --- pandas/core/indexes/numeric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 39eace39528f6..cf699b8b76c92 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -133,7 +133,7 @@ def _assert_safe_casting(cls, data, subarr): @classmethod def _coerce_to_ndarray(cls, data, dtype: Optional[np.dtype]): """ - Coerce data to ndarray unless it's instance of pd.Index. + Coerce data to ndarray unless it's an instance of pd.Index. Converts other iterables to list first and then to array. Does not touch ndarrays. From 5603e0f7a035cb84301f8c6e2c6c36de1738b9ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20=C3=96=C4=9Freden?= Date: Sun, 17 Nov 2019 22:30:30 +0100 Subject: [PATCH 18/18] Remove coerce_to_ndarray, add to __new__ --- pandas/core/indexes/numeric.py | 52 +++++++--------------------------- 1 file changed, 10 insertions(+), 42 deletions(-) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index cf699b8b76c92..ddb551d217928 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -1,4 +1,3 @@ -from typing import Optional import warnings import numpy as np @@ -58,8 +57,16 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=None): if fastpath: return cls._simple_new(data, name=name) - # is_scalar, generators handled in coerce_to_ndarray - data = cls._coerce_to_ndarray(data, dtype=dtype) + # Coerce to ndarray if not already ndarray or Index + if not isinstance(data, (np.ndarray, Index)): + if is_scalar(data): + raise cls._scalar_data_error(data) + + # other iterable of some kind + if not isinstance(data, (ABCSeries, list, tuple)): + data = list(data) + + data = np.asarray(data, dtype=dtype) if issubclass(data.dtype.type, str): cls._string_data_error(data) @@ -130,45 +137,6 @@ def _assert_safe_casting(cls, data, subarr): """ pass - @classmethod - def _coerce_to_ndarray(cls, data, dtype: Optional[np.dtype]): - """ - Coerce data to ndarray unless it's an instance of pd.Index. - - Converts other iterables to list first and then to array. - Does not touch ndarrays. - - Parameters - ---------- - data : array-like (1-dimensional) - dtype : numpy.dtype or None - If a numpy.dtype is passed, data will be casted to the type when - possible. - - Returns - ------- - numpy.ndarray or pd.Index - Numpy.ndarray created from data, or data itself if an instance of - pd.Index is passed. - - Raises - ------ - TypeError - When the data passed in is a scalar. - """ - - if isinstance(data, (np.ndarray, Index)): - return data - - if is_scalar(data): - raise cls._scalar_data_error(data) - - # other iterable of some kind - if not isinstance(data, (ABCSeries, list, tuple)): - data = list(data) - - return np.asarray(data, dtype=dtype) - def _concat_same_dtype(self, indexes, name): result = type(indexes[0])(np.concatenate([x._values for x in indexes])) return result.rename(name)