@@ -202,28 +202,15 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None,
202202 elif inferred in ['floating' , 'mixed-integer-float' ]:
203203
204204 # If we are actually all equal to integers,
205- # then coerce to integer
206- from .numeric import (Int64Index , UInt64Index ,
207- Float64Index )
208- try :
209- res = data .astype ('i8' , copy = False )
210- if (res == data ).all ():
211- return Int64Index (res , copy = copy ,
212- name = name )
213- except (OverflowError , TypeError , ValueError ):
214- pass
205+ # then coerce to integer.
206+ out = cls ._convert_to_int_index (data , copy , name )
215207
216- # Conversion to int64 failed (possibly due to
217- # overflow), so let's try now with uint64.
218- try :
219- res = data .astype ('u8' , copy = False )
220- if (res == data ).all ():
221- return UInt64Index (res , copy = copy ,
222- name = name )
223- except (TypeError , ValueError ):
224- pass
208+ # Conversion was successful.
209+ if out is not None :
210+ return out
225211
226- # return an actual float index
212+ # Return an actual float index.
213+ from .numeric import Float64Index
227214 return Float64Index (data , copy = copy , dtype = dtype ,
228215 name = name )
229216
@@ -270,13 +257,13 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None,
270257 if dtype is None :
271258 inferred = lib .infer_dtype (subarr )
272259 if inferred == 'integer' :
273- from . numeric import Int64Index , UInt64Index
274- try :
275- return Int64Index ( subarr . astype ( 'i8' ), copy = copy ,
276- name = name )
277- except OverflowError :
278- return UInt64Index (subarr . astype ( 'u8' ) , copy = copy ,
279- name = name )
260+ out = cls . _convert_to_int_index ( subarr , copy , name )
261+
262+ if out is not None :
263+ return out
264+
265+ return Index (subarr , copy = copy ,
266+ dtype = object , name = name )
280267 elif inferred in ['floating' , 'mixed-integer-float' ]:
281268 from .numeric import Float64Index
282269 return Float64Index (subarr , copy = copy , name = name )
@@ -351,6 +338,42 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None,
351338 See each method's docstring.
352339 """
353340
341+ @classmethod
342+ def _convert_to_int_index (cls , data , copy , name ):
343+ """
344+ Attempt to convert an array of data into an integer index.
345+
346+ Parameters
347+ ----------
348+ data : The data to convert.
349+ copy : Whether to copy the data or not.
350+ name : The name of the index returned.
351+
352+ Returns
353+ -------
354+ int_index : data converted to either an Int64Index or a
355+ UInt64Index, or None, if the conversion was
356+ not successful.
357+ """
358+ from .numeric import Int64Index , UInt64Index
359+ try :
360+ res = data .astype ('i8' , copy = False )
361+ if (res == data ).all ():
362+ return Int64Index (res , copy = copy , name = name )
363+ except (OverflowError , TypeError , ValueError ):
364+ pass
365+
366+ # Conversion to int64 failed (possibly due to
367+ # overflow), so let's try now with uint64.
368+ try :
369+ res = data .astype ('u8' , copy = False )
370+ if (res == data ).all ():
371+ return UInt64Index (res , copy = copy , name = name )
372+ except (TypeError , ValueError ):
373+ pass
374+
375+ return None
376+
354377 @classmethod
355378 def _simple_new (cls , values , name = None , dtype = None , ** kwargs ):
356379 """
0 commit comments