@@ -478,6 +478,52 @@ def __getitem__(self, key):
478
478
479
479
return self ._simple_new (result , ** attribs )
480
480
481
+ def __setitem__ (
482
+ self ,
483
+ key , # type: Union[int, Sequence[int], Sequence[bool], slice]
484
+ value , # type: Union[NaTType, Scalar, Sequence[Scalar]]
485
+ ):
486
+ # type: (...) -> None
487
+ # I'm fudging the types a bit here. The "Scalar" above really depends
488
+ # on type(self). For PeriodArray, it's Period (or stuff coercible
489
+ # to a period in from_sequence). For DatetimeArray, it's Timestamp...
490
+ # I don't know if mypy can do that, possibly with Generics.
491
+ # https://mypy.readthedocs.io/en/latest/generics.html
492
+
493
+ if is_list_like (value ):
494
+ is_slice = isinstance (key , slice )
495
+ if (not is_slice
496
+ and len (key ) != len (value )
497
+ and not com .is_bool_indexer (key )):
498
+ msg = ("shape mismatch: value array of length '{}' does not "
499
+ "match indexing result of length '{}'." )
500
+ raise ValueError (msg .format (len (key ), len (value )))
501
+ if not is_slice and len (key ) == 0 :
502
+ return
503
+
504
+ value = type (self )._from_sequence (value , dtype = self .dtype )
505
+ self ._check_compatible_with (value )
506
+ value = value .asi8
507
+ elif isinstance (value , self ._scalar_type ):
508
+ self ._check_compatible_with (value )
509
+ value = self ._unbox_scalar (value )
510
+ elif isna (value ) or value == iNaT :
511
+ value = iNaT
512
+ else :
513
+ msg = (
514
+ "'value' should be a '{scalar}', 'NaT', or array of those. "
515
+ "Got '{typ}' instead."
516
+ )
517
+ raise TypeError (msg .format (scalar = self ._scalar_type .__name__ ,
518
+ typ = type (value ).__name__ ))
519
+ self ._data [key ] = value
520
+ self ._maybe_clear_freq ()
521
+
522
+ def _maybe_clear_freq (self ):
523
+ # inplace operations like __setitem__ may invalidate the freq of
524
+ # DatetimeArray and TimedeltaArray
525
+ pass
526
+
481
527
def astype (self , dtype , copy = True ):
482
528
# Some notes on cases we don't have to handle here in the base class:
483
529
# 1. PeriodArray.astype handles period -> period
0 commit comments