@@ -6337,13 +6337,24 @@ def insert(self, loc: int, item) -> Index:
6337
6337
dtype = self ._find_common_type_compat (item )
6338
6338
return self .astype (dtype ).insert (loc , item )
6339
6339
6340
- arr = np .asarray (self )
6340
+ arr = self ._values
6341
+
6342
+ if arr .dtype != object or not isinstance (
6343
+ item , (tuple , np .datetime64 , np .timedelta64 )
6344
+ ):
6345
+ # with object-dtype we need to worry about numpy incorrectly casting
6346
+ # dt64/td64 to integer, also about treating tuples as sequences
6347
+ # special-casing dt64/td64 https://github.com/numpy/numpy/issues/12550
6348
+ casted = arr .dtype .type (item )
6349
+ new_values = np .insert (arr , loc , casted )
6350
+
6351
+ else :
6352
+ new_values = np .insert (arr , loc , None )
6353
+ new_values [loc ] = item
6341
6354
6342
- # Use constructor to ensure we get tuples cast correctly.
6343
6355
# Use self._constructor instead of Index to retain NumericIndex GH#43921
6344
- item = self ._constructor ([item ], dtype = self .dtype )._values
6345
- idx = np .concatenate ((arr [:loc ], item , arr [loc :]))
6346
- return self ._constructor ._with_infer (idx , name = self .name )
6356
+ # TODO(2.0) can use Index instead of self._constructor
6357
+ return self ._constructor ._with_infer (new_values , name = self .name )
6347
6358
6348
6359
def drop (self , labels , errors : str_t = "raise" ) -> Index :
6349
6360
"""
0 commit comments