13
13
14
14
from pandas ._typing import ArrayLike
15
15
from pandas .compat .numpy import function as nv
16
- from pandas .errors import AbstractMethodError
17
16
from pandas .util ._decorators import (
18
17
cache_readonly ,
19
18
doc ,
20
19
)
21
20
from pandas .util ._exceptions import rewrite_exception
22
21
23
- from pandas .core .dtypes .cast import (
24
- find_common_type ,
25
- infer_dtype_from ,
26
- )
27
22
from pandas .core .dtypes .common import (
28
23
is_dtype_equal ,
29
24
is_object_dtype ,
34
29
ABCSeries ,
35
30
)
36
31
32
+ from pandas .core .array_algos .putmask import validate_putmask
37
33
from pandas .core .arrays import (
38
34
Categorical ,
39
35
DatetimeArray ,
@@ -297,6 +293,21 @@ def searchsorted(self, value, side="left", sorter=None) -> np.ndarray:
297
293
# overriding IndexOpsMixin improves performance GH#38083
298
294
return self ._data .searchsorted (value , side = side , sorter = sorter )
299
295
296
+ def putmask (self , mask , value ) -> Index :
297
+ mask , noop = validate_putmask (self ._data , mask )
298
+ if noop :
299
+ return self .copy ()
300
+
301
+ try :
302
+ self ._validate_fill_value (value )
303
+ except (ValueError , TypeError ):
304
+ dtype = self ._find_common_type_compat (value )
305
+ return self .astype (dtype ).putmask (mask , value )
306
+
307
+ arr = self ._data .copy ()
308
+ arr .putmask (mask , value )
309
+ return type (self )._simple_new (arr , name = self .name )
310
+
300
311
# ---------------------------------------------------------------------
301
312
302
313
def _get_engine_target (self ) -> np .ndarray :
@@ -323,9 +334,30 @@ def repeat(self, repeats, axis=None):
323
334
result = self ._data .repeat (repeats , axis = axis )
324
335
return type (self )._simple_new (result , name = self .name )
325
336
326
- def insert (self , loc : int , item ):
327
- # ExtensionIndex subclasses must override Index.insert
328
- raise AbstractMethodError (self )
337
+ def insert (self , loc : int , item ) -> Index :
338
+ """
339
+ Make new Index inserting new item at location. Follows
340
+ Python list.append semantics for negative values.
341
+
342
+ Parameters
343
+ ----------
344
+ loc : int
345
+ item : object
346
+
347
+ Returns
348
+ -------
349
+ new_index : Index
350
+ """
351
+ try :
352
+ result = self ._data .insert (loc , item )
353
+ except (ValueError , TypeError ):
354
+ # e.g. trying to insert an integer into a DatetimeIndex
355
+ # We cannot keep the same dtype, so cast to the (often object)
356
+ # minimal shared dtype before doing the insert.
357
+ dtype = self ._find_common_type_compat (item )
358
+ return self .astype (dtype ).insert (loc , item )
359
+ else :
360
+ return type (self )._simple_new (result , name = self .name )
329
361
330
362
def _validate_fill_value (self , value ):
331
363
"""
@@ -426,60 +458,3 @@ def _get_engine_target(self) -> np.ndarray:
426
458
def _from_join_target (self , result : np .ndarray ) -> ArrayLike :
427
459
assert result .dtype == self ._data ._ndarray .dtype
428
460
return self ._data ._from_backing_data (result )
429
-
430
- def insert (self : _T , loc : int , item ) -> Index :
431
- """
432
- Make new Index inserting new item at location. Follows
433
- Python list.append semantics for negative values.
434
-
435
- Parameters
436
- ----------
437
- loc : int
438
- item : object
439
-
440
- Returns
441
- -------
442
- new_index : Index
443
-
444
- Raises
445
- ------
446
- ValueError if the item is not valid for this dtype.
447
- """
448
- arr = self ._data
449
- try :
450
- code = arr ._validate_scalar (item )
451
- except (ValueError , TypeError ):
452
- # e.g. trying to insert an integer into a DatetimeIndex
453
- # We cannot keep the same dtype, so cast to the (often object)
454
- # minimal shared dtype before doing the insert.
455
- dtype , _ = infer_dtype_from (item , pandas_dtype = True )
456
- dtype = find_common_type ([self .dtype , dtype ])
457
- return self .astype (dtype ).insert (loc , item )
458
- else :
459
- new_vals = np .concatenate (
460
- (
461
- arr ._ndarray [:loc ],
462
- np .asarray ([code ], dtype = arr ._ndarray .dtype ),
463
- arr ._ndarray [loc :],
464
- )
465
- )
466
- new_arr = arr ._from_backing_data (new_vals )
467
- return type (self )._simple_new (new_arr , name = self .name )
468
-
469
- def putmask (self , mask , value ) -> Index :
470
- res_values = self ._data .copy ()
471
- try :
472
- res_values .putmask (mask , value )
473
- except (TypeError , ValueError ):
474
- return self .astype (object ).putmask (mask , value )
475
-
476
- return type (self )._simple_new (res_values , name = self .name )
477
-
478
- # error: Argument 1 of "_wrap_joined_index" is incompatible with supertype
479
- # "Index"; supertype defines the argument type as "Union[ExtensionArray, ndarray]"
480
- def _wrap_joined_index ( # type: ignore[override]
481
- self : _T , joined : NDArrayBackedExtensionArray , other : _T
482
- ) -> _T :
483
- name = get_op_result_name (self , other )
484
-
485
- return type (self )._simple_new (joined , name = name )
0 commit comments