Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:15:14+00:00 2026-06-04T20:15:14+00:00

Is there an idiomatic way to compare two NumPy arrays that would treat NaNs

  • 0

Is there an idiomatic way to compare two NumPy arrays that would treat NaNs as being equal to each other (but not equal to anything other than a NaN).

For example, I want the following two arrays to compare equal:

np.array([1.0, np.NAN, 2.0])
np.array([1.0, np.NAN, 2.0])

and the following two arrays to compare unequal:

np.array([1.0, np.NAN, 2.0])
np.array([1.0, 0.0, 2.0])

I am looking for a method that would produce a scalar Boolean outcome.

The following would do it:

np.all((a == b) | (np.isnan(a) & np.isnan(b)))

but it’s clunky and creates all those intermediate arrays.

Is there a way that’s easier on the eye and makes better use of memory?

P.S. If it helps, the arrays are known to have the same shape and dtype.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:15:17+00:00Added an answer on June 4, 2026 at 8:15 pm

    If you really care about memory use (e.g. have very large arrays), then you should use numexpr and the following expression will work for you:

    np.all(numexpr.evaluate('(a==b)|((a!=a)&(b!=b))'))
    

    I’ve tested it on very big arrays with length of 3e8, and the code has the same performance on my machine as

    np.all(a==b)
    

    and uses the same amount of memory

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an idiomatic way to get available namespaces that can be used? (all-ns)
Is there a idiomatic way of removing elements from PATH-like shell variables? That is
Given a numpy array and a __getitem__ -type index, is there an idiomatic way
Is there an idiomatic way to bind variables in a MATLAB function? It seems
What is the (or is there an) idiomatic way to strip newlines from strings
I'm learning WPF, and am wondering if there's an idiomatic way to create a
Is there an idiomatic way of doing this?
Is there a more concise and idiomatic way to write the following code, which
Is there a more idiomatic/readable way of writing a get-and-set function in Clojure than:
What would be an idiomatic way of executing a function within a time limit?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.