@@ -478,6 +478,56 @@ 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
+
496
+ if lib .is_scalar (key ):
497
+ raise ValueError ("setting an array element with a sequence." )
498
+
499
+ if (not is_slice
500
+ and len (key ) != len (value )
501
+ and not com .is_bool_indexer (key )):
502
+ msg = ("shape mismatch: value array of length '{}' does not "
503
+ "match indexing result of length '{}'." )
504
+ raise ValueError (msg .format (len (key ), len (value )))
505
+ if not is_slice and len (key ) == 0 :
506
+ return
507
+
508
+ value = type (self )._from_sequence (value , dtype = self .dtype )
509
+ self ._check_compatible_with (value )
510
+ value = value .asi8
511
+ elif isinstance (value , self ._scalar_type ):
512
+ self ._check_compatible_with (value )
513
+ value = self ._unbox_scalar (value )
514
+ elif isna (value ) or value == iNaT :
515
+ value = iNaT
516
+ else :
517
+ msg = (
518
+ "'value' should be a '{scalar}', 'NaT', or array of those. "
519
+ "Got '{typ}' instead."
520
+ )
521
+ raise TypeError (msg .format (scalar = self ._scalar_type .__name__ ,
522
+ typ = type (value ).__name__ ))
523
+ self ._data [key ] = value
524
+ self ._maybe_clear_freq ()
525
+
526
+ def _maybe_clear_freq (self ):
527
+ # inplace operations like __setitem__ may invalidate the freq of
528
+ # DatetimeArray and TimedeltaArray
529
+ pass
530
+
481
531
def astype (self , dtype , copy = True ):
482
532
# Some notes on cases we don't have to handle here in the base class:
483
533
# 1. PeriodArray.astype handles period -> period
0 commit comments