nan

Is there a better way of making numpy.argmin() ignore NaN values

ε祈祈猫儿з 提交于 2019-11-29 17:32:14
问题 I want to get the index of the min value of a numpy array that contains NaNs and I want them ignored >>> a = array([ nan, 2.5, 3., nan, 4., 5.]) >>> a array([ NaN, 2.5, 3. , NaN, 4. , 5. ]) if I run argmin, it returns the index of the first NaN >>> a.argmin() 0 I substitute NaNs with Infs and then run argmin >>> a[isnan(a)] = Inf >>> a array([ Inf, 2.5, 3. , Inf, 4. , 5. ]) >>> a.argmin() 1 My dilemma is the following: I'd rather not change NaNs to Infs and then back after I'm done with

How to fill nan value with the previous available in the row?

社会主义新天地 提交于 2019-11-29 17:18:02
I need to fill the nan value in a dataframe by using the previous available value in a row, as the data are timeseries. Here there is an example: 1 2 3 4 b b nan c c nan d nan d nan nan c What I need is: 1 2 3 4 b b b c c c d d d d d c I know the method fillna from panda, but the methods are only based on columns. I think about doing a transposition and after the use of the fillna method, but I would like to know if there are more efficient way to do that. Use ffill along the first axis. df.ffill(axis=1) 1 2 3 4 0 b b b c 1 c c d d 2 d d d c 来源: https://stackoverflow.com/questions/47087907/how

pylab histogram get rid of nan

末鹿安然 提交于 2019-11-29 17:07:07
问题 I have a problem with making a histogram when some of my data contains "not a number" values. I can get rid of the error by using nan_to_num from numpy, but than i get a lot of zero values which mess up the histogram as well. pylab.figure() pylab.hist(numpy.nan_to_num(A)) pylab.show() So the idea would be to make another array in which all the nan values are gone, or to just mask them in the histogram in some way (preferrably with some builtin method). 回答1: Remove np.nan values from your

Why can itertools.groupby group the NaNs in lists but not in numpy arrays

☆樱花仙子☆ 提交于 2019-11-29 16:58:31
问题 I'm having a difficult time to debug a problem in which the float nan in a list and nan in a numpy.array are handled differently when these are used in itertools.groupby : Given the following list and array: from itertools import groupby import numpy as np lst = [np.nan, np.nan, np.nan, 0.16, 1, 0.16, 0.9999, 0.0001, 0.16, 0.101, np.nan, 0.16] arr = np.array(lst) When I iterate over the list the contiguous nan s are grouped: >>> for key, group in groupby(lst): ... if np.isnan(key): ... print

Javascript variable / 'NaN' behaving weirdly

不羁的心 提交于 2019-11-29 16:20:34
I have a couple of textboxes, Which return javascript Number values if valid data is in the textboxes, otherwise NaN . I get this strange behavior. When I checked in firebug (both the textboxes are blank): >>> hours NaN >>> minutes NaN >>> minutes == NaN false >>> hours == NaN false >>> hours == minutes false Why is it behaving so? Ray Toal NaN is not equal to anything, not even NaN . Reference at MDN More detailed SO question and answer For the authoritative source, see the ECMAScript 5 Official Specification , sections 11.9.1 and 11.9.3 : 1. If Type(x) is the same as Type(y), then [...] c.

Winsorizing data by column in pandas with NaN

可紊 提交于 2019-11-29 15:41:35
I'd like to winsorize several columns of data in a pandas Data Frame. Each column has some NaN, which affects the winsorization, so they need to be removed. The only way I know how to do this is to remove them for all of the data, rather than remove them only column-by-column. MWE: import numpy as np import pandas as pd from scipy.stats.mstats import winsorize # Create Dataframe N, M, P = 10**5, 4, 10**2 dates = pd.date_range('2001-01-01', periods=N//P, freq='D').repeat(P) df = pd.DataFrame(np.random.random((N, M)) , index=dates) df.index.names = ['DATE'] df.columns = ['one','two','three',

Python Pandas read_excel doesn't recognize null cell

纵然是瞬间 提交于 2019-11-29 14:47:40
My excel sheet: A B 1 first second 2 3 4 x y 5 z j Python code: df = pd.read_excel (filename, parse_cols=1) return a correct output: first second 0 NaN NaN 1 NaN NaN 2 x y 3 z j If i want work only with second column df = pd.read_excel (filename, parse_cols=[1]) return: second 0 y 1 j I'd have information about empty excel rows (NaN in my df) even if I work only with a specific column. If output loose NaN information it's not ok, for example, for skiprows paramater, etc Thanks For me works parameter skip_blank_lines=False : df = pd.read_excel ('test.xlsx', parse_cols=1, skip_blank_lines=False)

In which case could “a != a” return “true”?

落爺英雄遲暮 提交于 2019-11-29 14:40:30
java.lang.Math#min(double, double) : public static double min(double a, double b) { if (a != a) return a; // a is NaN if (a == 0.0d && b == 0.0d && Double.doubleToLongBits(b) == negativeZeroDoubleBits) return b; return (a <= b) ? a : b; } In which case could a != a return true ? It seems that it's when a is NaN, but I can't imagine an example. Could you please provide one? A simple example is double d = Double.NaN; // or double d = 0.0/0.0; // or double d = Double.POSITIVE_INFINITY + Double.NEGATIVE_INFINITY; if (Double.isNaN(a)) { // tests if a != a // do something BTW Double.compare() does

How to tell whether value is NaN without using isNaN, which has false positives? [duplicate]

荒凉一梦 提交于 2019-11-29 14:39:14
This question already has an answer here: How to test whether something is identically NaN? 3 answers How can I check whether the input value is NaN or not without using the isNaN function? If you can use ECMAScript 6 , you have Object.is : return Object.is(obj, NaN); Otherwise, here is one option, from the source code of underscore.js : // Is the given value `NaN`? _.isNaN = function(obj) { // `NaN` is the only value for which `===` is not reflexive. return obj !== obj; }; Also their note for that function: Note: this is not the same as the native isNaN function, which will also return true

When can Java produce a NaN?

廉价感情. 提交于 2019-11-29 13:49:12
I know what Java Double.NaN is. I have some Java code that produces NaN . // calculate errors delta = m1 + m2 - M; eta = f1 + f2 - F; for (int i = 0; i < numChildren; i++) { epsilon[i] = p[i]*m1+(1-p[i])*m2+q[i]*f1+(1-q[i])*f2-C[i]; } // use errors in gradient descent // set aside differences for the p's and q's float mDiff = m1 - m2; float fDiff = f1 - f2; // first update m's and f's m1 -= rate*delta; m2 -= rate*delta; f1 -= rate*eta; f2 -= rate*eta; for (int i = 0; i < numChildren; i++) { m1 -= rate*epsilon[i]*p[i]; m2 -= rate*epsilon[i]*(1-p[i]); f1 -= rate*epsilon[i]*q[i]; f2 -= rate