Removing NaNs in numpy arrays

2024/9/8 10:46:10

I have two numpy arrays that contains NaNs:

A = np.array([np.nan,   2,   np.nan,   3,   4])
B = np.array([   1  ,   2,     3   ,   4,  np.nan])

are there any smart way using numpy to remove the NaNs in both arrays, and also remove whats on the corresponding index in the other list? Making it look like this:

A = array([  2,   3, ])
B = array([  2,   4, ])
Answer

What you could do is add the 2 arrays together this will overwrite with NaN values where they are none, then use this to generate a boolean mask index and then use the index to index into your original numpy arrays:

In [193]:A = np.array([np.nan,   2,   np.nan,   3,   4])
B = np.array([   1  ,   2,     3   ,   4,  np.nan])
idx = np.where(~np.isnan(A+B))
idx
print(A[idx])
print(B[idx])
[ 2.  3.]
[ 2.  4.]

output from A+B:

In [194]:A+B
Out[194]:
array([ nan,   4.,  nan,   7.,  nan])

EDIT

As @Oliver W. has correctly pointed out, the np.where is unnecessary as np.isnan will produce a boolean index that you can use to index into the arrays:

In [199]:A = np.array([np.nan,   2,   np.nan,   3,   4])
B = np.array([   1  ,   2,     3   ,   4,  np.nan])
idx = (~np.isnan(A+B))
print(A[idx])
print(B[idx])
[ 2.  3.]
[ 2.  4.]
https://en.xdnf.cn/q/72686.html

Related Q&A

Run Python + OpenCV + dlib in Azure Functions

I have created an image processing script in Python (with dlib and OpenCV) - I was wondering how I can bring this functionality to Azure Functions, so that the script can be called via an API. As Pytho…

Best way to add python scripting into QT application?

I have a QT 4.6 application (C++ language) and i need to add python scripting to it on windows platform. Unfortunately, i never embed python before, and it seems to be a lot of different ways to do so.…

Unexpected behavior of python builtin str function

I am running into an issue with subtyping the str class because of the str.__call__ behavior I apparently do not understand. This is best illustrated by the simplified code below.class S(str):def __ini…

How to set cookies with GAE/Python for 1 month?

I need to implement the following:User input user id and pass We validate that on another server If they are correct, cookies with these details should be saved for one month Each time user uses my sit…

connection refused with Celery

I have a Django project on an Ubuntu EC2 node, which I have been using to set up an asynchronous using Celery. I am following How to list the queued items in celery? along with the docs, to experiment…

Routing all packets through my program?

I want to build an application that routes all network traffic (not just HTTP) through my application. Basically, what I want is all the traffic to be given to my application (they should never reach t…

python struct.pack(): pack multiple datas in a list or a tuple

Say i have a list or a tuple containing numbers of type long long,x = [12974658, 638364, 53637, 63738363]If want to struct.pack them individually, i have to use struct.pack(<Q, 12974658)or if i want…

Can I pass a list of colors for points to matplotlibs Axes.plot()?

Ive got a lot of points to plot and am noticing that plotting them individually in matplotlib takes much longer (more than 100 times longer, according to cProfile) than plotting them all at once. Howev…

Tidy data from multilevel Excel file via pandas

I want to produce tidy data from an Excel file which looks like this, with three levels of "merged" headers:Pandas reads the file just fine, with multilevel headers:# df = pandas.read_excel(t…

ttk tkinter multiple frames/windows

The following application I have created is used to demonstrate multiple windows in tkinter. The main problem is that none of the Entry controls, neither in the bmi-calculator or the converter, accept …