From c19ed89aab322037869302161d5f7ee23c54a3cd Mon Sep 17 00:00:00 2001 From: Bert Palm <bert.palm@ufz.de> Date: Mon, 23 Mar 2020 20:06:48 +0100 Subject: [PATCH] fixed iloc bug --- dios/indexer.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/dios/indexer.py b/dios/indexer.py index f1a4c43..d835de3 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 -- GitLab