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
[Python] Array equals returns incorrectly if NaNs are in arrays #22446
Antoine Pitrou / @pitrou:
How is this surprising? It's well-known (well, perhaps not :-)) that NaNs compare unequal to themselves. Numpy behaves similarly:
In other words, Arrow was previously incorrect and it was fixed.
I think this is even less of a problem in Arrow than Numpy, because a missing value in Arrow is represented as a None (which compares equal to itself), so the only reason to have a NaN in Arrow data should be when you really need to represent floating-point NaN.
Wes McKinney / @wesm:
This is true – I think we might need to think about semantic array equality (according to type-specific comparison rules) versus memory equality (i.e. what does memcmp on each non-null value yield). Both have their use cases I suppose
Keith Kraus / @kkraus14:
For context, the source of us finding this bug was we were using this function within cuDF unit tests to check for correct null vs NaN handling behavior. I agree this makes sense to return false, but a function that does memory comparison as opposed to value comparison would be useful.
We worked around this today by unsafe typecasting from float to int for now which effectively turns it into a memory comparison.
Antoine Pitrou / @pitrou:
Actually, if you look on the C++ side (see cpp/src/arrow/compare.h), the equality comparisons now take an optional EqualOptions where you can choose to set nans_equal to true.
So I would suggest to also expose those parameters in Python. Perhaps like it is done in CSV (see _csv.pyx).
$R
> a <- c(1, NA, 3, 4)
> b <- c(1, NA, 3, 4)
> a == b
[1] TRUENATRUETRUE
> all(a == b)
[1] NA
> all(a == b, na.rm = TRUE)
[1] TRUE
> identical(a, b)
[1] TRUE
"array equals" is a vectorized operation, so you get an array back. NA == NA is itself NA (because since the data is missing, we don't know what it is, so logically we also don't know if they're equal). If you want the scalar version, you can do the equivalent to what Antoine showed in Numpy with all(), but note that all() returns NA here, not FALSE, for the same reason as the scalar comparison. all() (along with many other base R aggregation functions) does take a na.rm argument though, so you can do the test only on the non-missing values.
identical() does the object comparison and shows the arrays to be the same, despite the missing data semantics.
At the risk of sparking controversy, I'd suggest that if we're going to model our missing data semantics on some language's implementation, R is a great choice, particularly if you care about statistical theory (which we should). That said, there are reasonable cases where you might want to do various kinds of comparisons, and the question is really about what we choose to make the default/natural way and then making the other ways possible somehow.
Wes McKinney / @wesm: @nealrichardson I think you're talking about something different. In Arrow NaN is actually not-a-number, not null (which is the most similar concept to R's NA). NumPy has no missing values at all
Neal Richardson / @nealrichardson:
Right; I was under the impression that NaN was (also) commonly used to indicate missing values in numpy because it doesn't have a separate missing value. Maybe that was mistaken.
FTR, here's the R example with NaN. Same behavior as with NA:
> NaN == NaN
[1] NA
> a <- c(1, NaN, 3, 4)
> a == a
[1] TRUENATRUETRUE
> identical(a, a)
[1] TRUE
Wes McKinney / @wesm:
I still think we need to clearly distinguish "data structure equality" from "semantic equality". For example, "semantic equality" is probably better addressed by kernels ((a == b).all())
Jonathan Keane / @jonkeane:
This has also come up in a slightly different context (comparing NaNs to non-missing values) and I made ARROW-13364, though depending on how this ticket is resolved they might go together.
Joris Van den Bossche / @jorisvandenbossche:
As mentioned in that other ticket, I think we should keep discussing them separately. Using the terminology of Wes' last comment, this issue is (originally) about "data structure equality" (do these objects contain equivalent data? i.e. arr1.equals(arr2) = True or False), while the other is about "semantic/analytical equality" (or comparisons in general, i.e. the comparison compute kernels).
Activity
asfimport commentedon Jul 25, 2019
Wes McKinney / @wesm:
Somewhat surprised by this. Can you submit a PR?
asfimport commentedon Jul 26, 2019
Keith Kraus / @kkraus14:
Will try to find some time to throw in a PR once our build issues are resolved :)
asfimport commentedon Jul 29, 2019
Antoine Pitrou / @pitrou:
How is this surprising? It's well-known (well, perhaps not :-)) that NaNs compare unequal to themselves. Numpy behaves similarly:
In other words, Arrow was previously incorrect and it was fixed.
I think this is even less of a problem in Arrow than Numpy, because a missing value in Arrow is represented as a None (which compares equal to itself), so the only reason to have a NaN in Arrow data should be when you really need to represent floating-point NaN.
asfimport commentedon Jul 29, 2019
Wes McKinney / @wesm:
This is true – I think we might need to think about semantic array equality (according to type-specific comparison rules) versus memory equality (i.e. what does
memcmpon each non-null value yield). Both have their use cases I supposeasfimport commentedon Jul 29, 2019
Keith Kraus / @kkraus14:
For context, the source of us finding this bug was we were using this function within cuDF unit tests to check for correct null vs NaN handling behavior. I agree this makes sense to return false, but a function that does memory comparison as opposed to value comparison would be useful.
We worked around this today by unsafe typecasting from float to int for now which effectively turns it into a memory comparison.
asfimport commentedon Jul 29, 2019
Antoine Pitrou / @pitrou:
Actually, if you look on the C++ side (see
cpp/src/arrow/compare.h), the equality comparisons now take an optionalEqualOptionswhere you can choose to setnans_equalto true.So I would suggest to also expose those parameters in Python. Perhaps like it is done in CSV (see _csv.pyx).
asfimport commentedon Jul 29, 2019
Neal Richardson / @nealrichardson:
For reference, here's how R handles this:
"array equals" is a vectorized operation, so you get an array back. NA == NA is itself NA (because since the data is missing, we don't know what it is, so logically we also don't know if they're equal). If you want the scalar version, you can do the equivalent to what Antoine showed in Numpy with all(), but note that all() returns NA here, not FALSE, for the same reason as the scalar comparison. all() (along with many other base R aggregation functions) does take a na.rm argument though, so you can do the test only on the non-missing values.
identical() does the object comparison and shows the arrays to be the same, despite the missing data semantics.
At the risk of sparking controversy, I'd suggest that if we're going to model our missing data semantics on some language's implementation, R is a great choice, particularly if you care about statistical theory (which we should). That said, there are reasonable cases where you might want to do various kinds of comparisons, and the question is really about what we choose to make the default/natural way and then making the other ways possible somehow.
asfimport commentedon Jul 29, 2019
Wes McKinney / @wesm:
@nealrichardson I think you're talking about something different. In Arrow NaN is actually not-a-number, not null (which is the most similar concept to R's NA). NumPy has no missing values at all
asfimport commentedon Jul 29, 2019
Neal Richardson / @nealrichardson:
Right; I was under the impression that
NaNwas (also) commonly used to indicate missing values in numpy because it doesn't have a separate missing value. Maybe that was mistaken.FTR, here's the R example with NaN. Same behavior as with NA:
asfimport commentedon Jun 2, 2020
Wes McKinney / @wesm:
I still think we need to clearly distinguish "data structure equality" from "semantic equality". For example, "semantic equality" is probably better addressed by kernels (
(a == b).all())asfimport commentedon Jul 16, 2021
Jonathan Keane / @jonkeane:
This has also come up in a slightly different context (comparing
NaNs to non-missing values) and I made ARROW-13364, though depending on how this ticket is resolved they might go together.asfimport commentedon Jul 17, 2021
Joris Van den Bossche / @jorisvandenbossche:
As mentioned in that other ticket, I think we should keep discussing them separately. Using the terminology of Wes' last comment, this issue is (originally) about "data structure equality" (do these objects contain equivalent data? i.e.
arr1.equals(arr2) = True or False), while the other is about "semantic/analytical equality" (or comparisons in general, i.e. the comparison compute kernels).asfimport commentedon Jul 28, 2021
Antoine Pitrou / @pitrou:
We should first expose the
EqualOptionsmembers as keyword-only arguments in the variousequalsmethods.honno commentedon May 11, 2023
Worth noting
Table.equals()treats NaNs as equal on the contraryand
RecordBatch.equals()does not(+1 for an equals option, but also just to say it'd be nice if there was consistency amongst the default calls of
equals()too).judahrand commentedon Sep 6, 2024
@honno it gets even worst than that!