Python Forum
filtering arrays - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: filtering arrays (/thread-27717.html)



filtering arrays - dramauh - Jun-18-2020

Hi everyone,

This is my first post in this forum. This question is not related with homework but it is really basic question. So I hope this is the correct section. The other day I was filtering some data using this similar routine

A=X
A[A==0]=numpy.nan
I did this to prevent the modification of the ndarray X. The interesting part is my array X was also modified!! I am not sure why. Thank you so much in advance for your help

Kind regards,

Estanislao

PD: I used python 3.7


RE: filtering arrays - Yoriz - Jun-18-2020

A==0 resolves to True or False, True & False are also represented as 0 & 1
A[A==0]=numpy.nan
will change value at the index of 0 or 1 depending on the outcome of A==0

Edit: may have gone off in a tangent
A=X
A is now pointing to the same object that X is
you would need a copy of x, presuming x is a list
A=X[:]



RE: filtering arrays - dramauh - Jun-18-2020

Hi Yoriz,

Yes, I know how it works that command. I used it a lot in MATLAB. My question is why that command modified the array A and the array X. I would have expected that only the array A is modified.

Regards,


RE: filtering arrays - Yoriz - Jun-18-2020

See the additional edit about copying in the above post
numpy.copy


RE: filtering arrays - dramauh - Jun-19-2020

Hi Yoriz,

Thank you so much. That was extremely helpful. I did not know the existence of that function.

Regards,