diff --git a/dios/indexer.py b/dios/indexer.py index f1a4c430904ec5c79e25f257bc7da5a0785ac154..d835de3a00bcc03d65df75463c5e27beed2d8e36 100644 --- a/dios/indexer.py +++ b/dios/indexer.py @@ -61,14 +61,22 @@ class _Indexer: dat_xloc = getattr(dat, xloc) val = value[i] if iter else value + # a hashable rowkey (.xloc[3, c] = value) : + # loc: rowkey not present in self -> prevent that .loc create a new item + # loc: rowkey present in self -> OK + # iloc: OK (cannot set a new item) if _is_hashable(rowkey): - # prevent setting a new value with .loc - if rowkey not in dat: + if xloc == 'loc' and rowkey not in dat: raise KeyError(rowkey) - else: - # cannot set to empty series - if len(dat_xloc[rowkey]) == 0: - continue + + # not a hashable rowkey (.xloc[[3,4], c] = value), + # so we can ask for a length, otherwise we might get a single value + # returned, that has no length. But we need to do this check, because, + # setting a value to an empty series, always fail. + # Nevertheless, if any key doesn't exist, the correct KeyError is thrown here, + # instead of later. + elif len(dat_xloc[rowkey]) == 0: + continue dat_xloc[rowkey] = val