nan

Python/Pandas: counting the number of missing/NaN in each row

為{幸葍}努か 提交于 2019-12-02 17:24:59
I've got a dataset with a big number of rows. Some of the values are NaN, like this: In [91]: df Out[91]: 1 3 1 1 1 1 3 1 1 1 2 3 1 1 1 1 1 NaN NaN NaN 1 3 1 1 1 1 1 1 1 1 And I want to count the number of NaN values in each string, it would be like this: In [91]: list = <somecode with df> In [92]: list Out[91]: [0, 0, 0, 3, 0, 0] What is the best and fastest way to do it? You could first find if element is NaN or not by isnull() and then take row-wise sum(axis=1) In [195]: df.isnull().sum(axis=1) Out[195]: 0 0 1 0 2 0 3 3 4 0 5 0 dtype: int64 And, if you want the output as list, you can In

Fast check for NaN in NumPy

别说谁变了你拦得住时间么 提交于 2019-12-02 14:46:30
I'm looking for the fastest way to check for the occurrence of NaN ( np.nan ) in a NumPy array X . np.isnan(X) is out of the question, since it builds a boolean array of shape X.shape , which is potentially gigantic. I tried np.nan in X , but that seems not to work because np.nan != np.nan . Is there a fast and memory-efficient way to do this at all? (To those who would ask "how gigantic": I can't tell. This is input validation for library code.) NPE Ray's solution is good. However, on my machine it is about 2.5x faster to use numpy.sum in place of numpy.min : In [13]: %timeit np.isnan(np.min

weird NaN when raising a number to a non-integer power [duplicate]

可紊 提交于 2019-12-02 13:02:42
This question already has an answer here: Exponentiation with negative base 2 answers I executed the following code: tau <- 0.25 h <- 0.6 * n ^ (-1 / 5) * (4.5 * dnorm(qnorm(tau)) ^ 4 * qnorm(tau) / (2 * (qnorm(tau) ^ 2 + 1)) ^ 2) ^ (1/5), and R keeps producing NaN . However, R actually computes (4.5 * dnorm(qnorm(tau)) ^ 4 * qnorm(tau) / (2 * (qnorm(tau) ^ 2 + 1)) ^ 2) to be equal to -0.003655336 . The weird thing is when I did the following k <- -0.003655336 k ^ (1 / 3) NaN was produced again. You are calculating the cube root of a negative number. Although, as pointed out by @mra68, a real

How set values in pandas dataframe based on NaN values of another column?

北城以北 提交于 2019-12-02 11:56:46
问题 I have dataframe named df with original shape (4361, 15) . Some of agefm column`s values are NaN. Just look: > df[df.agefm.isnull() == True].agefm.shape (2282,) Then I create new column and set all its values to 0: df['nevermarr'] = 0 So I would like to set nevermarr value to 1, then in that row agefm is Nan: df[df.agefm.isnull() == True].nevermarr = 1 Nothing changed: > df['nevermarr'].sum() 0 What am I doing wrong? 回答1: The best is use numpy.where: df['nevermarr'] = np.where(df.agefm.isnull

Haskell: comparing NaN values

萝らか妹 提交于 2019-12-02 11:53:19
问题 I wrote quickcheck tests for a Haskell program that optimizes and evaluates a function. The problem is quickcheck generates expressions resulting in NaN like: > acos(2) NaN Haskell evaluates the following statement as false: > acos(2)==acos(2) False So my quickcheck tests fail with this comparison. Is there any way to compare NaN values? 回答1: No, as is defined by IEEE 754 comparing 2 NaN s always return false. To chceck if your value is NaN in Haskell you can use isNaN method or write it by

Sum numbers returns NaN

喜夏-厌秋 提交于 2019-12-02 09:50:47
I'm trying to do a sum of numbers inside div's, so, I did: $(document).ready(function() { var numbers, sumNumbers; $(".item").each(function() { numbers = $(this).children().text(); numbers = +numbers; sumNumbers += numbers; }); console.log(sumNumbers); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="item"> <span class="itemNum">0</span> </div> <div class="item"> <span class="itemNum">2</span> </div> <div class="item"> <span class="itemNum">1</span> </div> But, same converting the numbers from text to number with +numbers is returned NaN,

Why is Infinity × 0 = NaN?

故事扮演 提交于 2019-12-02 09:36:13
问题 IEEE 754 specifies the result of 1 / 0 as ∞ (Infinity). However, IEEE 754 then specifies the result of 0 × ∞ as NaN. This feels counter-intuitive : Why is 0 × ∞ not 0? We can think of 1 / 0 = ∞ as the limit of 1 / z as z tends to zero We can think of 0 × ∞ = 0 as the limit of 0 × z as z tends to ∞. Why does the IEEE standard follow intuition 1. but not 2.? 回答1: It is easier to understand the behavior of IEEE 754 floating point zeros and infinities if you do not think of them as being

Remove a tuple containing nan in list of tuples — Python

∥☆過路亽.° 提交于 2019-12-02 04:29:35
I have a long list of tuples and want to remove any tuple that has a nan in it using Python. What I currently have: x = [('Recording start', 0), (nan, 4), (nan, 7), ..., ('Event marker 1', 150)] Result I'm looking for: x = [('Recording start', 0), ('Event marker 1', 150)] I've tried use np.isnan and variants of that, but have had no success and keep getting an error: ufunc 'isnan' is not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule "safe" Any suggestions would be appreciated!! You could use list comprehension

Why is Infinity × 0 = NaN?

本小妞迷上赌 提交于 2019-12-02 04:00:13
IEEE 754 specifies the result of 1 / 0 as ∞ (Infinity). However, IEEE 754 then specifies the result of 0 × ∞ as NaN. This feels counter-intuitive : Why is 0 × ∞ not 0? We can think of 1 / 0 = ∞ as the limit of 1 / z as z tends to zero We can think of 0 × ∞ = 0 as the limit of 0 × z as z tends to ∞. Why does the IEEE standard follow intuition 1. but not 2.? It is easier to understand the behavior of IEEE 754 floating point zeros and infinities if you do not think of them as being literally zero or infinite. The floating point zeros not only represent the real number zero. They also represent

Wrong result from mean(x, na.rm = TRUE)

独自空忆成欢 提交于 2019-12-02 00:30:39
I want to compute the mean, min and max of a series of Managers returns, as follows: ManagerRet <-data.frame(diff(Managerprices)/lag(Managerprices,k=-1)) I then replace return = 0 with NaN since data are extracted from a database and not all the dates are populated. ManagerRet = replace(ManagerRet,ManagerRet==0,NaN) I have the following 3 function > min(ManagerRet,na.rm = TRUE) [1] -0.0091716 > max(ManagerRet,na.rm = TRUE) [1] 0.007565 > mean(ManagerRet,na.rm = TRUE)*252 [1] NaN Why the mean function returns a NaN value while min and max performe calculation properly? Below you can find the