From c508814b5b543da5f71aedb97c5a52756a57d765 Mon Sep 17 00:00:00 2001 From: Jeffrey Tratner Date: Sat, 5 Oct 2013 11:32:14 -0400 Subject: [PATCH] CLN: Cleanup Int64Index and Float64Index __new__ Int64Index had duplicate code under a branch plus both were looking for scalar data errors handled in `_coerce_to_ndarray` anyways. --- pandas/core/index.py | 39 ++++++--------------------------------- 1 file changed, 6 insertions(+), 33 deletions(-) diff --git a/pandas/core/index.py b/pandas/core/index.py index 3f491b4271ddc..c39364d58e205 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -201,6 +201,8 @@ def _string_data_error(cls, data): @classmethod def _coerce_to_ndarray(cls, data): + """coerces data to ndarray, raises on scalar data. Converts other + iterables to list first and then to array. Does not touch ndarrays.""" if not isinstance(data, np.ndarray): if np.isscalar(data): @@ -1624,7 +1626,7 @@ class Int64Index(Index): Parameters ---------- data : array-like (1-dimensional) - dtype : NumPy dtype (default: object) + dtype : NumPy dtype (default: int64) copy : bool Make a copy of input ndarray name : object @@ -1651,33 +1653,8 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False): subarr.name = name return subarr - if not isinstance(data, np.ndarray): - if np.isscalar(data): - cls._scalar_data_error(data) - - data = cls._coerce_to_ndarray(data) - - if issubclass(data.dtype.type, compat.string_types): - cls._string_data_error(data) - - elif issubclass(data.dtype.type, np.integer): - # don't force the upcast as we may be dealing - # with a platform int - if dtype is None or not issubclass(np.dtype(dtype).type, np.integer): - dtype = np.int64 - - subarr = np.array(data, dtype=dtype, copy=copy) - else: - subarr = np.array(data, dtype=np.int64, copy=copy) - if len(data) > 0: - if (subarr != data).any(): - raise TypeError('Unsafe NumPy casting, you must ' - 'explicitly cast') - - # other iterable of some kind - if not isinstance(data, (list, tuple)): - data = list(data) - data = np.asarray(data) + # isscalar, generators handled in coerce_to_ndarray + data = cls._coerce_to_ndarray(data) if issubclass(data.dtype.type, compat.string_types): cls._string_data_error(data) @@ -1767,11 +1744,7 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False): subarr.name = name return subarr - if not isinstance(data, np.ndarray): - if np.isscalar(data): - cls._scalar_data_error(data) - - data = cls._coerce_to_ndarray(data) + data = cls._coerce_to_ndarray(data) if issubclass(data.dtype.type, compat.string_types): cls._string_data_error(data)