How to replace numpy.nan to -1 (in one line of code)? [duplicate]












-1
















This question already has an answer here:




  • finding and replacing 'nan' with a number

    2 answers



  • convert nan value to zero

    9 answers




I have z = np.array([4.4, 3, 0, np.nan, -1, 6]) and just can't find any quick and friendly solution for easy replacement. I can't believe it's sometimes such not user friendly language.



I tried:



np.where(z == np.nan, -1, z)



nothing happens



np.nan_to_num(z, -1)



changes to zeros



z = [-1 if np.nan(x) else x for x in z]



TypeError: 'float' object is not callable



Why such earsy things can't be just as pure easy? I must use numpy only.










share|improve this question













marked as duplicate by jedwards python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 20 at 14:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • In your list comprehension, changing np.nan(x) to np.isnan(x) will work. (Alternatively, and possibly faster, would be: z[np.isnan(z)] = -1).

    – jedwards
    Jan 20 at 14:19













  • Maybe it's duplicated but I also tried to show Python 4 needs to have better implementations for some base functions.

    – Peter.k
    Jan 20 at 14:25
















-1
















This question already has an answer here:




  • finding and replacing 'nan' with a number

    2 answers



  • convert nan value to zero

    9 answers




I have z = np.array([4.4, 3, 0, np.nan, -1, 6]) and just can't find any quick and friendly solution for easy replacement. I can't believe it's sometimes such not user friendly language.



I tried:



np.where(z == np.nan, -1, z)



nothing happens



np.nan_to_num(z, -1)



changes to zeros



z = [-1 if np.nan(x) else x for x in z]



TypeError: 'float' object is not callable



Why such earsy things can't be just as pure easy? I must use numpy only.










share|improve this question













marked as duplicate by jedwards python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 20 at 14:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • In your list comprehension, changing np.nan(x) to np.isnan(x) will work. (Alternatively, and possibly faster, would be: z[np.isnan(z)] = -1).

    – jedwards
    Jan 20 at 14:19













  • Maybe it's duplicated but I also tried to show Python 4 needs to have better implementations for some base functions.

    – Peter.k
    Jan 20 at 14:25














-1












-1








-1









This question already has an answer here:




  • finding and replacing 'nan' with a number

    2 answers



  • convert nan value to zero

    9 answers




I have z = np.array([4.4, 3, 0, np.nan, -1, 6]) and just can't find any quick and friendly solution for easy replacement. I can't believe it's sometimes such not user friendly language.



I tried:



np.where(z == np.nan, -1, z)



nothing happens



np.nan_to_num(z, -1)



changes to zeros



z = [-1 if np.nan(x) else x for x in z]



TypeError: 'float' object is not callable



Why such earsy things can't be just as pure easy? I must use numpy only.










share|improve this question















This question already has an answer here:




  • finding and replacing 'nan' with a number

    2 answers



  • convert nan value to zero

    9 answers




I have z = np.array([4.4, 3, 0, np.nan, -1, 6]) and just can't find any quick and friendly solution for easy replacement. I can't believe it's sometimes such not user friendly language.



I tried:



np.where(z == np.nan, -1, z)



nothing happens



np.nan_to_num(z, -1)



changes to zeros



z = [-1 if np.nan(x) else x for x in z]



TypeError: 'float' object is not callable



Why such earsy things can't be just as pure easy? I must use numpy only.





This question already has an answer here:




  • finding and replacing 'nan' with a number

    2 answers



  • convert nan value to zero

    9 answers








python numpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 20 at 14:13









Peter.kPeter.k

446412




446412




marked as duplicate by jedwards python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 20 at 14:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by jedwards python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 20 at 14:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • In your list comprehension, changing np.nan(x) to np.isnan(x) will work. (Alternatively, and possibly faster, would be: z[np.isnan(z)] = -1).

    – jedwards
    Jan 20 at 14:19













  • Maybe it's duplicated but I also tried to show Python 4 needs to have better implementations for some base functions.

    – Peter.k
    Jan 20 at 14:25



















  • In your list comprehension, changing np.nan(x) to np.isnan(x) will work. (Alternatively, and possibly faster, would be: z[np.isnan(z)] = -1).

    – jedwards
    Jan 20 at 14:19













  • Maybe it's duplicated but I also tried to show Python 4 needs to have better implementations for some base functions.

    – Peter.k
    Jan 20 at 14:25

















In your list comprehension, changing np.nan(x) to np.isnan(x) will work. (Alternatively, and possibly faster, would be: z[np.isnan(z)] = -1).

– jedwards
Jan 20 at 14:19







In your list comprehension, changing np.nan(x) to np.isnan(x) will work. (Alternatively, and possibly faster, would be: z[np.isnan(z)] = -1).

– jedwards
Jan 20 at 14:19















Maybe it's duplicated but I also tried to show Python 4 needs to have better implementations for some base functions.

– Peter.k
Jan 20 at 14:25





Maybe it's duplicated but I also tried to show Python 4 needs to have better implementations for some base functions.

– Peter.k
Jan 20 at 14:25












1 Answer
1






active

oldest

votes


















3














Use np.isnan(x). You can replace like this:



z = np.array([4.4, 3, 0, np.nan, -1, 6])
z[np.isnan(z)] = -1
print(z)

# [ 4.4 3. 0. -1. -1. 6. ]





share|improve this answer
























  • God bless you. Thanks for very quick response.

    – Peter.k
    Jan 20 at 14:22


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Use np.isnan(x). You can replace like this:



z = np.array([4.4, 3, 0, np.nan, -1, 6])
z[np.isnan(z)] = -1
print(z)

# [ 4.4 3. 0. -1. -1. 6. ]





share|improve this answer
























  • God bless you. Thanks for very quick response.

    – Peter.k
    Jan 20 at 14:22
















3














Use np.isnan(x). You can replace like this:



z = np.array([4.4, 3, 0, np.nan, -1, 6])
z[np.isnan(z)] = -1
print(z)

# [ 4.4 3. 0. -1. -1. 6. ]





share|improve this answer
























  • God bless you. Thanks for very quick response.

    – Peter.k
    Jan 20 at 14:22














3












3








3







Use np.isnan(x). You can replace like this:



z = np.array([4.4, 3, 0, np.nan, -1, 6])
z[np.isnan(z)] = -1
print(z)

# [ 4.4 3. 0. -1. -1. 6. ]





share|improve this answer













Use np.isnan(x). You can replace like this:



z = np.array([4.4, 3, 0, np.nan, -1, 6])
z[np.isnan(z)] = -1
print(z)

# [ 4.4 3. 0. -1. -1. 6. ]






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 20 at 14:19









JeppeJeppe

678615




678615













  • God bless you. Thanks for very quick response.

    – Peter.k
    Jan 20 at 14:22



















  • God bless you. Thanks for very quick response.

    – Peter.k
    Jan 20 at 14:22

















God bless you. Thanks for very quick response.

– Peter.k
Jan 20 at 14:22





God bless you. Thanks for very quick response.

– Peter.k
Jan 20 at 14:22





Popular posts from this blog

Homophylophilia

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage