Skip to content

Commit 0a71f7a

Browse files
committed
Merge pull request #5117 from jtratner/cleanup-int64index-constructor
CLN: Remove redundant initialization from Int64Index and Float64Index constructor
2 parents 126c100 + c508814 commit 0a71f7a

File tree

1 file changed

+6
-33
lines changed

1 file changed

+6
-33
lines changed

pandas/core/index.py

+6-33
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ def _string_data_error(cls, data):
201201

202202
@classmethod
203203
def _coerce_to_ndarray(cls, data):
204+
"""coerces data to ndarray, raises on scalar data. Converts other
205+
iterables to list first and then to array. Does not touch ndarrays."""
204206

205207
if not isinstance(data, np.ndarray):
206208
if np.isscalar(data):
@@ -1624,7 +1626,7 @@ class Int64Index(Index):
16241626
Parameters
16251627
----------
16261628
data : array-like (1-dimensional)
1627-
dtype : NumPy dtype (default: object)
1629+
dtype : NumPy dtype (default: int64)
16281630
copy : bool
16291631
Make a copy of input ndarray
16301632
name : object
@@ -1651,33 +1653,8 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False):
16511653
subarr.name = name
16521654
return subarr
16531655

1654-
if not isinstance(data, np.ndarray):
1655-
if np.isscalar(data):
1656-
cls._scalar_data_error(data)
1657-
1658-
data = cls._coerce_to_ndarray(data)
1659-
1660-
if issubclass(data.dtype.type, compat.string_types):
1661-
cls._string_data_error(data)
1662-
1663-
elif issubclass(data.dtype.type, np.integer):
1664-
# don't force the upcast as we may be dealing
1665-
# with a platform int
1666-
if dtype is None or not issubclass(np.dtype(dtype).type, np.integer):
1667-
dtype = np.int64
1668-
1669-
subarr = np.array(data, dtype=dtype, copy=copy)
1670-
else:
1671-
subarr = np.array(data, dtype=np.int64, copy=copy)
1672-
if len(data) > 0:
1673-
if (subarr != data).any():
1674-
raise TypeError('Unsafe NumPy casting, you must '
1675-
'explicitly cast')
1676-
1677-
# other iterable of some kind
1678-
if not isinstance(data, (list, tuple)):
1679-
data = list(data)
1680-
data = np.asarray(data)
1656+
# isscalar, generators handled in coerce_to_ndarray
1657+
data = cls._coerce_to_ndarray(data)
16811658

16821659
if issubclass(data.dtype.type, compat.string_types):
16831660
cls._string_data_error(data)
@@ -1767,11 +1744,7 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False):
17671744
subarr.name = name
17681745
return subarr
17691746

1770-
if not isinstance(data, np.ndarray):
1771-
if np.isscalar(data):
1772-
cls._scalar_data_error(data)
1773-
1774-
data = cls._coerce_to_ndarray(data)
1747+
data = cls._coerce_to_ndarray(data)
17751748

17761749
if issubclass(data.dtype.type, compat.string_types):
17771750
cls._string_data_error(data)

0 commit comments

Comments
 (0)