Skip to content

Commit a9f4db4

Browse files
committed
ENH: allow Int64Index to have a platform int dtype
1 parent bab9148 commit a9f4db4

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

pandas/core/index.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def __new__(cls, data, dtype=None, copy=False, name=None):
107107
return PeriodIndex(data, copy=copy, name=name)
108108

109109
if issubclass(data.dtype.type, np.integer):
110-
return Int64Index(data, copy=copy, name=name)
110+
return Int64Index(data, copy=copy, dtype=dtype, name=name)
111111

112112
subarr = com._ensure_object(data)
113113
elif np.isscalar(data):
@@ -1296,7 +1296,12 @@ def __new__(cls, data, dtype=None, copy=False, name=None):
12961296
raise TypeError('String dtype not supported, you may need '
12971297
'to explicitly cast to int')
12981298
elif issubclass(data.dtype.type, np.integer):
1299-
subarr = np.array(data, dtype=np.int64, copy=copy)
1299+
# don't force the upcast as we may be dealing
1300+
# with a platform int
1301+
if dtype is None or not issubclass(np.dtype(dtype).type, np.integer):
1302+
dtype = np.int64
1303+
1304+
subarr = np.array(data, dtype=dtype, copy=copy)
13001305
else:
13011306
subarr = np.array(data, dtype=np.int64, copy=copy)
13021307
if len(data) > 0:
@@ -1316,10 +1321,6 @@ def inferred_type(self):
13161321
def _constructor(self):
13171322
return Int64Index
13181323

1319-
@cache_readonly
1320-
def dtype(self):
1321-
return np.dtype('int64')
1322-
13231324
@property
13241325
def is_all_dates(self):
13251326
"""

pandas/tests/test_common.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,26 @@ def test_ensure_int32():
253253
result = com._ensure_int32(values)
254254
assert(result.dtype == np.int32)
255255

256+
def test_ensure_platform_int():
257+
258+
# verify that when we create certain types of indices
259+
# they remain the correct type under platform conversions
260+
from pandas.core.index import Int64Index
261+
262+
# int64
263+
x = Int64Index([1, 2, 3], dtype='int64')
264+
assert(x.dtype == np.int64)
265+
266+
pi = com._ensure_platform_int(x)
267+
assert(pi.dtype == np.int_)
268+
269+
# int32
270+
x = Int64Index([1, 2, 3], dtype='int32')
271+
assert(x.dtype == np.int32)
272+
273+
pi = com._ensure_platform_int(x)
274+
assert(pi.dtype == np.int_)
275+
256276
# TODO: fix this broken test
257277

258278
# def test_console_encode():

0 commit comments

Comments
 (0)