33
33
import pandas .core .indexes .base as ibase
34
34
from pandas .core .indexes .base import Index , _index_shared_docs
35
35
from pandas .core .indexes .extension import (
36
- ExtensionIndex ,
36
+ NDArrayBackedExtensionIndex ,
37
37
inherit_names ,
38
38
make_wrapped_arith_op ,
39
39
)
@@ -82,7 +82,7 @@ def wrapper(left, right):
82
82
cache = True ,
83
83
)
84
84
@inherit_names (["mean" , "asi8" , "freq" , "freqstr" ], DatetimeLikeArrayMixin )
85
- class DatetimeIndexOpsMixin (ExtensionIndex ):
85
+ class DatetimeIndexOpsMixin (NDArrayBackedExtensionIndex ):
86
86
"""
87
87
Common ops mixin to support a unified interface datetimelike Index.
88
88
"""
@@ -191,7 +191,7 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
191
191
192
192
maybe_slice = lib .maybe_indices_to_slice (indices , len (self ))
193
193
194
- result = ExtensionIndex .take (
194
+ result = NDArrayBackedExtensionIndex .take (
195
195
self , indices , axis , allow_fill , fill_value , ** kwargs
196
196
)
197
197
if isinstance (maybe_slice , slice ):
@@ -496,17 +496,6 @@ def where(self, cond, other=None):
496
496
arr = self ._data ._from_backing_data (result )
497
497
return type (self )._simple_new (arr , name = self .name )
498
498
499
- def putmask (self , mask , value ):
500
- try :
501
- value = self ._data ._validate_where_value (value )
502
- except (TypeError , ValueError ):
503
- return self .astype (object ).putmask (mask , value )
504
-
505
- result = self ._data ._ndarray .copy ()
506
- np .putmask (result , mask , value )
507
- arr = self ._data ._from_backing_data (result )
508
- return type (self )._simple_new (arr , name = self .name )
509
-
510
499
def _summary (self , name = None ) -> str :
511
500
"""
512
501
Return a summarized representation.
@@ -575,41 +564,30 @@ def shift(self, periods=1, freq=None):
575
564
# --------------------------------------------------------------------
576
565
# List-like Methods
577
566
578
- def delete (self , loc ):
579
- new_i8s = np .delete (self .asi8 , loc )
580
-
567
+ def _get_delete_freq (self , loc : int ):
568
+ """
569
+ Find the `freq` for self.delete(loc).
570
+ """
581
571
freq = None
582
572
if is_period_dtype (self .dtype ):
583
573
freq = self .freq
584
- elif is_integer (loc ):
585
- if loc in (0 , - len (self ), - 1 , len (self ) - 1 ):
586
- freq = self .freq
587
- else :
588
- if is_list_like (loc ):
589
- loc = lib .maybe_indices_to_slice (
590
- np .asarray (loc , dtype = np .intp ), len (self )
591
- )
592
- if isinstance (loc , slice ) and loc .step in (1 , None ):
593
- if loc .start in (0 , None ) or loc .stop in (len (self ), None ):
574
+ elif self .freq is not None :
575
+ if is_integer (loc ):
576
+ if loc in (0 , - len (self ), - 1 , len (self ) - 1 ):
594
577
freq = self .freq
578
+ else :
579
+ if is_list_like (loc ):
580
+ loc = lib .maybe_indices_to_slice (
581
+ np .asarray (loc , dtype = np .intp ), len (self )
582
+ )
583
+ if isinstance (loc , slice ) and loc .step in (1 , None ):
584
+ if loc .start in (0 , None ) or loc .stop in (len (self ), None ):
585
+ freq = self .freq
586
+ return freq
595
587
596
- arr = type (self ._data )._simple_new (new_i8s , dtype = self .dtype , freq = freq )
597
- return type (self )._simple_new (arr , name = self .name )
598
-
599
- def insert (self , loc : int , item ):
588
+ def _get_insert_freq (self , loc , item ):
600
589
"""
601
- Make new Index inserting new item at location
602
-
603
- Parameters
604
- ----------
605
- loc : int
606
- item : object
607
- if not either a Python datetime or a numpy integer-like, returned
608
- Index dtype will be object rather than datetime.
609
-
610
- Returns
611
- -------
612
- new_index : Index
590
+ Find the `freq` for self.insert(loc, item).
613
591
"""
614
592
value = self ._data ._validate_insert_value (item )
615
593
item = self ._data ._box_func (value )
@@ -630,14 +608,20 @@ def insert(self, loc: int, item):
630
608
# Adding a single item to an empty index may preserve freq
631
609
if self .freq .is_on_offset (item ):
632
610
freq = self .freq
611
+ return freq
633
612
634
- arr = self ._data
613
+ @doc (NDArrayBackedExtensionIndex .delete )
614
+ def delete (self , loc ):
615
+ result = super ().delete (loc )
616
+ result ._data ._freq = self ._get_delete_freq (loc )
617
+ return result
635
618
636
- new_values = np . concatenate ([ arr . _ndarray [: loc ], [ value ], arr . _ndarray [ loc :]] )
637
- new_arr = self . _data . _from_backing_data ( new_values )
638
- new_arr . _freq = freq
619
+ @ doc ( NDArrayBackedExtensionIndex . insert )
620
+ def insert ( self , loc : int , item ):
621
+ result = super (). insert ( loc , item )
639
622
640
- return type (self )._simple_new (new_arr , name = self .name )
623
+ result ._data ._freq = self ._get_insert_freq (loc , item )
624
+ return result
641
625
642
626
# --------------------------------------------------------------------
643
627
# Join/Set Methods
0 commit comments