0

I want to replace all values smaller than -999 with NaN and values falling within -999 to 0 with -0.1 in a 2D array.

I can replace one value using

data[data < -999] = 'nan'

However, when I use

data[data < -999] = 'nan'
data[data < 0] = -0.1

It says

RuntimeWarning: invalid value encountered in less
data[data < 0] = -0.1

How to replace values < -999 with NaN and values within the range of -999 to 0 with -0.1

1
  • Just to be clear, what data type is data? What does print(type(data)) return? Commented Mar 14, 2016 at 19:23

1 Answer 1

1

Your line

data[data < -999] = 'nan'

Isn't doing what you think. data < -999 evaluates to falsey in Python2.7, even though it's a weird thing to do; you're comparing a list to an int. This means the expression maps to

data[0] = 'nan'

Which is why you see only one value change. Note that this syntax is not allowed in Python3

It looks like you're trying to do

for column in data:
    for ix, num in enumerate(column):
        if num < -999:
            column[ix] = 'nan'
        elif num < 0:
            column[ix] = -0.1

A quick example of why yours doesn't work:

>>> data = [[-9999, -9999], [-9999, -9999]]
>>> data[data<-999] = 'nan'
>>> data
['nan', [-9999, -9999]]
>>> 
Sign up to request clarification or add additional context in comments.

6 Comments

I am using Python 2.7 and has to replace the values in a 2D array. data[data < -999] = 'nan' worked fine for me. I was wondering whether I can use it further.
@Mario The only way it will appear to work will be if the only value in data less than -999 is the very first. See my example above for a counterexample to it working
Yes, the first value in the 2D array was -9999, but it replaced all the those values with NaN, here the if else with for loop is not working for me.
@Mario apologies, I had a small typo; please try again
@Mario it's fixed to work for a two-dimensional array - let me know if you have an issue
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.