nan

Is it possible to set a number to NaN or infinity?

大城市里の小女人 提交于 2019-12-04 07:24:58
问题 Is it possible to set an element of an array to NaN in Python? Additionally, is it possible to set a variable to +/- infinity? If so, is there any function to check whether a number is infinity or not? 回答1: Cast from string using float() : >>> float('NaN') nan >>> float('Inf') inf >>> -float('Inf') -inf >>> float('Inf') == float('Inf') True >>> float('Inf') == 1 False 回答2: Yes, you can use numpy for that. import numpy as np a = arange(3,dtype=float) a[0] = np.nan a[1] = np.inf a[2] = -np.inf

Remove NaN row from X array and also the corresponding row in Y

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 06:51:52
I have an X array with NaN and I can remove the row with NaN as such: import numpy as np x = x[~np.isnan(x)] But I have a corresponding Y array assert len(x) == len(y) # True x = x[~np.isnan(x)] assert len(x) == len(y) # False and breaks How do I remove the corresponding rows from the Y array? My X array looks like this: >>> x [[ 2.67510434 2.67521927 3.49296989 3.80100625 4. 2.83631844] [ 3.47538057 3.4752436 3.62245715 4.0720535 5. 3.7773169 ] [ 2.6157049 2.61583852 3.48335887 3.78088813 0. 2.78791096] ..., [ 3.60408952 3.60391203 3.64328267 4.1156462 5. 3.77933333] [ 2.66773792 2.66785516 3

Summing rows in grouped pandas dataframe and return NaN

早过忘川 提交于 2019-12-04 05:08:13
Example import pandas as pd import numpy as np d = {'l': ['left', 'right', 'left', 'right', 'left', 'right'], 'r': ['right', 'left', 'right', 'left', 'right', 'left'], 'v': [-1, 1, -1, 1, -1, np.nan]} df = pd.DataFrame(d) Problem When a grouped dataframe contains a value of np.NaN I want the grouped sum to be NaN as is given by the skipna=False flag for pd.Series.sum and also pd.DataFrame.sum however, this In [235]: df.v.sum(skipna=False) Out[235]: nan However, this behavior is not reflected in the pandas.DataFrame.groupby object In [237]: df.groupby('l')['v'].sum()['right'] Out[237]: 2.0 and

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

霸气de小男生 提交于 2019-12-04 04:44:17
问题 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

pandas groupby and rolling_apply ignoring NaNs

♀尐吖头ヾ 提交于 2019-12-04 03:41:05
I have a pandas dataframe and I want to calculate the rolling mean of a column (after a groupby clause). However, I want to exclude NaNs. For instance, if the groupby returns [2, NaN, 1], the result should be 1.5 while currently it returns NaN. I've tried the following but it doesn't seem to work: df.groupby(by=['var1'])['value'].apply(pd.rolling_apply, 3, lambda x: np.mean([i for i in x if i is not np.nan and i!='NaN'])) If I even try this: df.groupby(by=['var1'])['value'].apply(pd.rolling_apply, 3, lambda x: 1) I'm getting NaN in the output so it must be something to do with how pandas works

How to change NaN string representation in C#?

北城以北 提交于 2019-12-04 03:08:58
问题 My program saves a pointcloud to file, where each pointcloud is a Point3D[,] , from the System.Windows.Media.Media3D namespace. This shows a line of the output file (in portuguese): -112,644088741971;71,796623005014;NaN (Não é um número) while I'd like it to be (on order to be correctly parsed afterwards): -112,644088741971;71,796623005014;NaN The block of code that generates the file is here: var lines = new List<string>(); for (int rows = 0; rows < malha.GetLength(0); rows++) { for (int

R cor returns NaN sometimes

∥☆過路亽.° 提交于 2019-12-04 03:08:02
问题 I've been working on some data, available here: Dropbox' csv file (please be kind to use it to replicate the error). When I run the code: t<-read.csv("120.csv") x<-NULL for (i in 1:100){ x<-c(x,cor(t$nitrate,t$sulfate,use="na.or.complete")) } sum(is.nan(x)) I get random values of the last expression, usually around 55 to 60. I expect cor to give repetible results, so I expect x to be a vector of length=100 made of identical values. See, for example, the output of two independent runs: > x<

C++ NaN byte representation changes during assignment

ε祈祈猫儿з 提交于 2019-12-04 02:46:05
In trying to assign a NaN to a variable on an x64 processor *dest = *(float*)&sourceNaN; where unsigned char sourceNaN[] = {00,00, 0xa0, 0x7f}; The floating point instructions fld and fstp (seen in the disassembly) change the 0xa0 byte to an 0xe0. Thus the destination has an extra bit set. Can someone explain why this is happening? This is a Windows application. The assembly language code: 005C9B9C mov eax,dword ptr [ebp+10h] 005C9B9F fld dword ptr [ebp-80h] 005C9BA2 fstp dword ptr [eax] Sneftel 0x7fa00000 is a signalling NaN ("sNaN"). 0x7fe00000 is a quiet NaN ("qNaN"). I haven't heard of

Replace NaN in DataFrame index

扶醉桌前 提交于 2019-12-04 02:09:18
问题 I have a DataFrame which looks like this: one | two a | 2 | 5 b | 3 | 6 NaN | 0 | 0 How do I replace the NaN in the index with a string, say "No label"? I tried: df = df.replace(np.NaN, "No label") and df.index = df.index.replace(np.NaN, "No label") But got TypeError: expected string or buffer 回答1: You can process the original index as a Series first and then re-assign the index: import pandas as pd import numpy as np df = pd.DataFrame({'one': [2, 3, 0], 'two': [5, 6, 0]}, index=['a', 'b', np

Is `x!=x` a portable way to test for NaN?

无人久伴 提交于 2019-12-04 00:44:24
In C you can test to see if a double if NaN using isnan(x) . However many places online, including for example this SO answer say that you can simply use x!=x instead. Is x!=x in any C specification as a method that is guaranteed to test if x is NaN? I can't find it myself and I would like my code to work with different compilers. Please refer to the normative section Annex F: IEC 60559 floating-point arithmetic of the C standard: F.1 Introduction An implementation that defines __STDC_IEC_559__ shall conform to the specifications in this annex. Implementations that do not define __STDC_IEC_559