Skip to content

[Python] Array equals returns incorrectly if NaNs are in arrays #22446

Open

Description

@asfimport
import numpy as np
import pyarrow as pa

data = [0, 1, np.nan, None, 4]

arr1 = pa.array(data)
arr2 = pa.array(data)

pa.Array.equals(arr1, arr2)

Unsure if this is expected behavior, but in Arrow 0.12.1 this returned True as compared to False in 0.14.1.

Reporter: Keith Kraus / @kkraus14

Related issues:

PRs and other links:

Note: This issue was originally created as ARROW-6043. Please see the migration documentation for further details.

Activity

  1. asfimport commented on Jul 25, 2019

    @asfimport
    Author

    Wes McKinney / @wesm:
    Somewhat surprised by this. Can you submit a PR?

  2. asfimport commented on Jul 26, 2019

    @asfimport
    Author

    Keith Kraus / @kkraus14:
    Will try to find some time to throw in a PR once our build issues are resolved :)

  3. asfimport commented on Jul 29, 2019

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    How is this surprising? It's well-known (well, perhaps not :-)) that NaNs compare unequal to themselves. Numpy behaves similarly:

    >>> a = np.array([1.0, np.nan])                                                                                                                                      
    >>> a == a                                                                                                                                                           
    array([ True, False])
    >>> (a == a).all()                                                                                                                                                   
    False

    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.

  4. asfimport commented on Jul 29, 2019

    @asfimport
    Author

    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

  5. asfimport commented on Jul 29, 2019

    @asfimport
    Author

    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.

  6. asfimport commented on Jul 29, 2019

    @asfimport
    Author

    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).

  7. asfimport commented on Jul 29, 2019

    @asfimport
    Author

    Neal Richardson / @nealrichardson:
    For reference, here's how R handles this:

    $ R 
    > a <- c(1, NA, 3, 4) 
    > b <- c(1, NA, 3, 4) 
    > a == b 
    [1] TRUE   NA TRUE TRUE 
    > 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.

     

  8. asfimport commented on Jul 29, 2019

    @asfimport
    Author

    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

  9. asfimport commented on Jul 29, 2019

    @asfimport
    Author

    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] TRUE   NA TRUE TRUE 
    > identical(a, a) 
    [1] TRUE
  10. asfimport commented on Jun 2, 2020

    @asfimport
    Author

    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())

  11. asfimport commented on Jul 16, 2021

    @asfimport
    Author

    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.

  12. asfimport commented on Jul 17, 2021

    @asfimport
    Author

    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).

  13. asfimport commented on Jul 28, 2021

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    We should first expose the EqualOptions members as keyword-only arguments in the various equals methods.

  14. honno commented on May 11, 2023

    @honno

    Worth noting Table.equals() treats NaNs as equal on the contrary

    >>> table = pa.Table.from_pydict({"foo": [float("nan")]})
    >>> table.equals(table)
    True

    and RecordBatch.equals() does not

    >>> record_batch = pa.RecordBatch.from_pydict({"foo": [float("nan")]})
    >>> record_batch.equals(record_batch)
    False

    (+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).

  15. judahrand commented on Sep 6, 2024

    @judahrand
    Contributor

    @honno it gets even worst than that!

    >>> table = pa.Table.from_pydict({"foo": [float("nan")]})
    >>> table.equals(table)
    True
    >>> table_1 = pa.Table.from_pydict({"foo": [float("nan")]})
    >>> table_2 = pa.Table.from_pydict({"foo": [float("nan")]})
    >>> table_1.equals(table_2)
    False
  16. added
    Status: needs champion
    on Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: PythonPriority: CriticalStatus: needs championType: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions