@@ -144,6 +144,41 @@ class Index(IndexOpsMixin, StringAccessorMixin, PandasObject):
144
144
def __new__ (cls , data = None , dtype = None , copy = False , name = None ,
145
145
fastpath = False , tupleize_cols = True , ** kwargs ):
146
146
147
+ def convert_to_int_index (_data , _copy , _name ):
148
+ """
149
+ Attempt to convert an array of data into an integer index.
150
+
151
+ Parameters
152
+ ----------
153
+ _data : The data to convert.
154
+ _copy : Whether to copy the data or not.
155
+ _name : The name of the index returned.
156
+
157
+ Returns
158
+ -------
159
+ int_index : _data converted to either an Int64Index or a
160
+ UInt64Index, or None, if the conversion was
161
+ not successful.
162
+ """
163
+ from .numeric import Int64Index , UInt64Index
164
+ try :
165
+ res = _data .astype ('i8' , copy = False )
166
+ if (res == _data ).all ():
167
+ return Int64Index (res , copy = _copy ,
168
+ name = _name )
169
+ except (OverflowError , TypeError , ValueError ):
170
+ pass
171
+
172
+ # Conversion to int64 failed (possibly due to
173
+ # overflow), so let's try now with uint64.
174
+ try :
175
+ res = _data .astype ('u8' , copy = False )
176
+ if (res == _data ).all ():
177
+ return UInt64Index (res , copy = _copy ,
178
+ name = _name )
179
+ except (TypeError , ValueError ):
180
+ return None
181
+
147
182
if name is None and hasattr (data , 'name' ):
148
183
name = data .name
149
184
@@ -202,28 +237,15 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None,
202
237
elif inferred in ['floating' , 'mixed-integer-float' ]:
203
238
204
239
# 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
240
+ # then coerce to integer.
241
+ out = convert_to_int_index (data , copy , name )
215
242
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
243
+ # Conversion was successful.
244
+ if out is not None :
245
+ return out
225
246
226
- # return an actual float index
247
+ # Return an actual float index.
248
+ from .numeric import Float64Index
227
249
return Float64Index (data , copy = copy , dtype = dtype ,
228
250
name = name )
229
251
@@ -270,13 +292,13 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None,
270
292
if dtype is None :
271
293
inferred = lib .infer_dtype (subarr )
272
294
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 )
295
+ out = convert_to_int_index ( subarr , copy , name )
296
+
297
+ if out is not None :
298
+ return out
299
+ else :
300
+ return Index (subarr , copy = copy ,
301
+ dtype = object , name = name )
280
302
elif inferred in ['floating' , 'mixed-integer-float' ]:
281
303
from .numeric import Float64Index
282
304
return Float64Index (subarr , copy = copy , name = name )
0 commit comments