You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent data.table assignment by reference behaviour #759
When assigning by reference with a data.table using a column from a second data.table, the results are inconsistent. When there are no matches by the key columns of both data.tables, it appears the assigment expression y := y is totally ignored - not even NAs are returned.
library(data.table)
dt1 <- data.table(id = 1:2, x = 3:4, key = "id")
dt2 <- data.table(id = 3:4, y = 5:6, key = "id")
print(dt1[dt2, y := y])
## id x # Would have also expected column: y
## 1: 1 3 # NA
## 2: 2 4 # NA
However, when there is a partial match, non-matching columns have a placeholder NA.
dt2[, id := 2:3]
print(dt1[dt2, y := y])
## id x y
## 1: 1 3 NA # <-- placeholder NA here
## 2: 2 4 5
This wreaks havoc on later code that assumes a y column exists in all cases. Otherwise I keep having to write cumbersome additional checks to take into account both cases.
Is there an elegant way around this inconsistency?
Side note: It's okay to just link to the SO issue. In fact it'd be nice if you edited your question to include the original link to your issues so that we can update the post once a decision is made and the issue is closed.
Please, note that my j expressions aren't as simple as in the example. It uses conversion from character to date which now breaks because the assignment operation gets executed even if the i expression doesn't select any row.
If this feature will stay in data.table I'm not sure how to get around this. Perhaps,
surrounding all affected statements by if-clauses which check if dt[i, ] has a non-empty result (which would require to execute the same ì expression twice),
adding an intermediate column to check if there is at least one non-NA value
adding an additional ifelse call to j
I'm happy to hear about any other suggestions which will save me from the additional work to fix this.
Potential solution
Perhaps, we have to distinguish here between a simple select statement in ì and joining another table as requested by @mchen402. If the select in i would return an empty table it could stay with the old behaviour not to execute the j and not add a column with all NAs.
Or, do we need another parameter or option here?
At least, this needs to be added to the list of POTENTIALLY BREAKING CHANGES in NEWS.md.
@UweBlock current approach is more consistent, it always add a column, and fills its will values depending on i match. IMO is more correct to have this extra check when you want to conditionally add a column. This shouldn't cost much if you are using index, otherwise you can use if(length(ii<-dt[..., which=TRUE])) dt[ii, b:=1] to avoid vector scan twice.
@jangorecki Agree. That was the thinking. To make the shape and type of the output not depend on data (whether the i expression returns all-FALSE). Assuming by 'current approach' you mean 'new approach'.
It uses conversion from character to date which now breaks because the assignment operation gets executed even if the i expression doesn't select any row.
What is this expression please (roughly) and why does it break on empty input? I'm thinking we should focus on your RHS of := so that it returns numeric(), integer() or character() on empty input.
And you must have switches in your current code after this statement then? To cope with the new b column existing or not depending on whether i returns all-FALSE or not?
I've tried to create a minimal working example but wasn't able to reproduce the strange behaviour of the production code.
In the production code, I replaced the complex date conversion stuff on the RHS by calls to lubridate::dmy which apparently is more robust to missing or NA input. Fortunately, the fixes required less effort than suspected and I was able to run all reports in time.
Now, with the fixes in place I feel comfortable with the new approach. However, I will spent some more time on the MWE.
Thanks for the update @UweBlock. We'll keep the change then unless anything else comes to light and move the news item up to the 'potentially breaking changes' section as you suggested.
> data.table(test=1)[test != 1, 'newCol' := stop("some error"), by = test] Error in [.data.table(data.table(test = 1), test != 1, :=("newCol", : some error
In hundreds of different piece of code I used the 1.9.6 inconsistency as a no-op case, expecting that the stop would never be evaluated if the RHS had a length of 0.
I understand the purpose of the change in data.table and I am looking for the best way to remediate my code and possibly an option to restore old functionality to give me time to remediate my code. Would it be possible add an option to restore this "inconsistency", like what was done for the other potentially breaking changes introduced in 1.9.8?
@brianbolt all breaking changes were listed in news file so be sure to check there. Also be aware if your application works fine you don't have to update package and your code. Multiple versions of same package can be managed by using R libraries. This is pretty old change already, I don't think there is easy way to suppress evaluation here.
@jangorecki I don't feel Nov 2016 (when 1.9.8 was released) is that old and other possibly breaking changes were given options (to be removed in 2 years) for using the old behavior in a deprecated way. That being said, I am aware that installing the old package in a local library is possible and it is what I am doing as a workaround for now until I can remediate my code.
For anyone else that might need to do this. Here is 1 way to install 1.9.6:
# if you want to install the package somewhere else then run the next line
.libPaths('/path/to/local/r_libs')
# install data.table 1.9.6 from the last day it was available on cran
install.packages("data.table", repos = "http://mran.revolutionanalytics.com/snapshot/2016-11-25")
I find this part of the documentation very confusing now as it seems to indicate that I am using data.table correctly. Essentially as a for loop.
The general form of data.table syntax is:
DT[ i, j, by ] # + extra arguments
| | |
| | -------> grouped by what?
| -------> what to do?
---> on which rows?
In my case, on which rows = 0 so I would expect (like in a normal for loop) that j would not be evaluated. I really do think this should be an option if not the default.
@brianbolt I see your point, but another way of thinking about it is that evaluation of j is appropriate even when i leads to an empty table. For example, I often use .N even when there are zero rows. One fix that may help in your use case (which isn't really clear to me), is adding if (.N > 0L) ahead of these stop statements. I sometimes use...
@MichaelChirico Late reply by 7 months (!) but are you still surprised by that error? It looks good and safe behaviour to return error in that case, to me. new_col to me suggests the intention is to add a new column, or not, based on the condition. Assigning NULL to a new column is a warning anyway (Adding new column 'new_col' then assigning NULL (deleting it)). And if some_condition was accidentally not a single TRUE or FALSE, but a length > 2 logical vector, then the new_col could conist of a mixture of value and NA.
How about if (DT[,any(some_condition)]) DT[,new_col:=value] instead.
Hmm. Can't say I remember what exactly I was trying to do. Certainly my syntax looks weird. := NULL should always be expected to try and delete a column... so yes I agree with you
Activity
arunsrinivasan commentedon Aug 6, 2014
Thanks @tunaaa once again for the report.
Side note: It's okay to just link to the SO issue. In fact it'd be nice if you edited your question to include the original link to your issues so that we can update the post once a decision is made and the issue is closed.
arunsrinivasan commentedon Mar 8, 2016
Updated SO post.
jangorecki commentedon Jul 7, 2016
I wonder if it makes sense to extend this feature for grouping operations?
It would be probably quite costly to determine data type for NA columns.
DT[NA] returns one all-NA row again, while retaining fix that DT[some…
UweBlock commentedon Oct 4, 2016
Now, the inconsistency is the other way around, unfortunately.
As a matter of fact I have to fix a bunch of code which is relying on the old behaviour of version 1.9.6 and 1.9.7 (until recently).
used to yield in
Now, with the latests commit it returns
Please, note that my
jexpressions aren't as simple as in the example. It uses conversion from character to date which now breaks because the assignment operation gets executed even if theiexpression doesn't select any row.If this feature will stay in
data.tableI'm not sure how to get around this. Perhaps,dt[i, ]has a non-empty result (which would require to execute the sameìexpression twice),ifelsecall tojI'm happy to hear about any other suggestions which will save me from the additional work to fix this.
Potential solution
Perhaps, we have to distinguish here between a simple select statement in
ìand joining another table as requested by @mchen402. If the select iniwould return an empty table it could stay with the old behaviour not to execute thejand not add a column with all NAs.Or, do we need another parameter or option here?
At least, this needs to be added to the list of POTENTIALLY BREAKING CHANGES in
NEWS.md.MichaelChirico commentedon Oct 4, 2016
I'm sorry, what is your expected output? I may have missed it
jangorecki commentedon Oct 4, 2016
@UweBlock current approach is more consistent, it always add a column, and fills its will values depending on
imatch. IMO is more correct to have this extra check when you want to conditionally add a column. This shouldn't cost much if you are using index, otherwise you can useif(length(ii<-dt[..., which=TRUE])) dt[ii, b:=1]to avoid vector scan twice.mattdowle commentedon Oct 4, 2016
@jangorecki Agree. That was the thinking. To make the shape and type of the output not depend on data (whether the i expression returns all-FALSE). Assuming by 'current approach' you mean 'new approach'.
@UweBlock Two questions. On :
What is this expression please (roughly) and why does it break on empty input? I'm thinking we should focus on your RHS of := so that it returns numeric(), integer() or character() on empty input.
And you must have switches in your current code after this statement then? To cope with the new b column existing or not depending on whether i returns all-FALSE or not?
mattdowle commentedon Oct 4, 2016
Here's an example of old behaviour we're thinking is undesirable :
New behaviour :
as.Date()works on emptycharacter()here which is why I'm asking for more info about your RHS please @UweBlock.UweBlock commentedon Oct 4, 2016
Thank you for your immediate reactions and suggestions, @MichaelChirico, @jangorecki, @mattdowle.
Two updates:
lubridate::dmywhich apparently is more robust to missing or NA input. Fortunately, the fixes required less effort than suspected and I was able to run all reports in time.Now, with the fixes in place I feel comfortable with the new approach. However, I will spent some more time on the MWE.
mattdowle commentedon Oct 11, 2016
Thanks for the update @UweBlock. We'll keep the change then unless anything else comes to light and move the news item up to the 'potentially breaking changes' section as you suggested.
brianbolt commentedon Apr 27, 2017
I did something very similar to this in data.table 1.9.6:
> data.table(test=1)[test != 1, 'newCol' := stop("some error"), by = test]now (since 1.9.8), the same code gives:
> data.table(test=1)[test != 1, 'newCol' := stop("some error"), by = test] Error in[.data.table(data.table(test = 1), test != 1,:=("newCol", : some errorIn hundreds of different piece of code I used the 1.9.6 inconsistency as a no-op case, expecting that the stop would never be evaluated if the RHS had a length of 0.
I understand the purpose of the change in data.table and I am looking for the best way to remediate my code and possibly an option to restore old functionality to give me time to remediate my code. Would it be possible add an option to restore this "inconsistency", like what was done for the other potentially breaking changes introduced in 1.9.8?
jangorecki commentedon Apr 28, 2017
@brianbolt all breaking changes were listed in news file so be sure to check there. Also be aware if your application works fine you don't have to update package and your code. Multiple versions of same package can be managed by using R libraries. This is pretty old change already, I don't think there is easy way to suppress evaluation here.
brianbolt commentedon Apr 28, 2017
@jangorecki I don't feel Nov 2016 (when 1.9.8 was released) is that old and other possibly breaking changes were given options (to be removed in 2 years) for using the old behavior in a deprecated way. That being said, I am aware that installing the old package in a local library is possible and it is what I am doing as a workaround for now until I can remediate my code.
For anyone else that might need to do this. Here is 1 way to install 1.9.6:
brianbolt commentedon Apr 28, 2017
I find this part of the documentation very confusing now as it seems to indicate that I am using data.table correctly. Essentially as a for loop.
In my case, on which rows = 0 so I would expect (like in a normal for loop) that j would not be evaluated. I really do think this should be an option if not the default.
franknarf1 commentedon Apr 28, 2017
@brianbolt I see your point, but another way of thinking about it is that evaluation of
jis appropriate even whenileads to an empty table. For example, I often use.Neven when there are zero rows. One fix that may help in your use case (which isn't really clear to me), is addingif (.N > 0L)ahead of thesestopstatements. I sometimes use...MichaelChirico commentedon Aug 8, 2017
just got bit by this... maybe worth adding to the jan's suggestion to the FAQ?
e.g., I just tried:
And was (somewhat) surprised to see the error:
mattdowle commentedon Mar 16, 2018
@MichaelChirico Late reply by 7 months (!) but are you still surprised by that error? It looks good and safe behaviour to return error in that case, to me.
new_colto me suggests the intention is to add a new column, or not, based on the condition. Assigning NULL to a new column is a warning anyway (Adding new column 'new_col' then assigning NULL (deleting it)). And ifsome_conditionwas accidentally not a single TRUE or FALSE, but a length > 2 logical vector, then the new_col could conist of a mixture of value and NA.How about
if (DT[,any(some_condition)]) DT[,new_col:=value]instead.MichaelChirico commentedon Mar 16, 2018
Hmm. Can't say I remember what exactly I was trying to do. Certainly my syntax looks weird.
:= NULLshould always be expected to try and delete a column... so yes I agree with you