-
Notifications
You must be signed in to change notification settings - Fork 968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
setnames() errors for NA 'new' when 'old' provided, silently ignored in full assignment (no 'old') #6236
Comments
I'm not sure this is a bug in behavior, but definitely docs could be clarified. Here is my interpretation
|
I think it's clearer in the simple, explicit case It's harder for me to see why we have different behavior based on DT = data.table(a=1)
setnames(DT, NA_character_)
setnames(DT, 'a', NA_character_) I guess I'm struggling to see the use case where we'd come up with such a vector of column names with missings. Here's a workaround, but I wonder why we'd have setnames(DT, new_with_missings)
local({
idx <- na.omit(new_with_missings)
setnames(DT, -attr(idx, "na.action"), idx)
}) |
you are right it is weird, and un-documented, (but maybe used in the wild, I'm not sure) so I think we could go either way (continue supporting this or error) |
Yeah we can try this as well, if any problems occur we can just revert it and i will file a PR with the documentation instead. |
Definitely. But should we allow |
You are right, for consistency, you could make an argument for skip in the old and new case too. This is tricky. My suggestion for a way forward in #6240 was updating the docs. Adding one sentence "Missing values in 'new' mean to not rename that column, and are only allowed when 'old' is not provided." seems to fix the issue for me. Maybe somebody else can read the docs and see if that fixes this? I feel there is some relationship to the skip_absent arg?
BTW the name of the issue I think could be clarified--- for me the difference is not partial vs full, it is rename by name (old+new) vs by order/index. > setnames(DT,c("b","a"),c("y",NA_character_))
Error: NA in 'new' at positions [2] |
Good callout. But I don't think that's the case (maybe it should though?): DT=data.table(a=1,b=2)
setnames(DT,c(NA, 'c')) # a->a, b->c
setnames(DT, c('a', 'c'), c('d', NA))
# Error in stopf("NA in 'new' at positions %s", brackify(which(is.na(new)))) :
# NA in 'new' at positions [2]
setnames(DT, c(NA, 'c'), c('d', 'e'))
# Error in stopf("NA (or out of bounds) in 'old' at positions %s", brackify(which(is.na(old)))) :
# NA (or out of bounds) in 'old' at positions [1]
setnames(DT, c('a', 'c'), c('d', NA), skip_absent=TRUE)
# Error in stopf("NA in 'new' at positions %s", brackify(which(is.na(new)))) :
# NA in 'new' at positions [2]
setnames(DT, c(NA, 'c'), c('d', 'e'), skip_absent=TRUE)
# Error in stopf("NA (or out of bounds) in 'old' at positions %s", brackify(which(is.na(old)))) :
# NA (or out of bounds) in 'old' at positions [1] The case can be made for the last case behavior should be a->a, c->e -- that's not at odds with the documentation:
I agree that's a good fix. We can revisit if there's a user request to support a different behavior. |
Seems odd.
The text was updated successfully, but these errors were encountered: