nan

sending NaN in json

痴心易碎 提交于 2019-12-18 11:38:20
问题 I am trying to encode an array which contains floats and NaN into JSON string from Python using json.dumps() . But the encoded JSON string is not being decoded successfully in PHP. Is the NaN causing this problem? How can I work around this situation? 回答1: json.dumps has an allow_nan parameter, which defaults to True. NaN, Infinity and -Infinity are not part of JSON, but they are standard in Javascript, so they're commonly used extensions. If the recipient can't handle them, set allow_nan

What are all the possible calculations that could cause a NaN in Python? [closed]

本小妞迷上赌 提交于 2019-12-18 10:22:33
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I've been searching around, and there appear to be scattered discussions about NaN s in different programming languages, including some specific cases, but nothing exhaustive or clear. What are the most common operations that would cause a NaN , in Python, which originate while

Python Pandas replace NaN in one column with value from corresponding row of second column

无人久伴 提交于 2019-12-18 10:09:01
问题 I am working with this Pandas DataFrame in Python 2.7. File heat Farheit Temp_Rating 1 YesQ 75 N/A 1 NoR 115 N/A 1 YesA 63 N/A 1 NoT 83 41 1 NoY 100 80 1 YesZ 56 12 2 YesQ 111 N/A 2 NoR 60 N/A 2 YesA 19 N/A 2 NoT 106 77 2 NoY 45 21 2 YesZ 40 54 3 YesQ 84 N/A 3 NoR 67 N/A 3 YesA 94 N/A 3 NoT 68 39 3 NoY 63 46 3 YesZ 34 81 I need to replace all NaNs in the Temp_Rating column with the value from the Farheit column. This is what I need: File heat Observation 1 YesQ 75 1 NoR 115 1 YesA 63 1 YesQ

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

末鹿安然 提交于 2019-12-18 09:32:58
问题 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. 回答1: Use ffill along the first axis.

Winsorizing data by column in pandas with NaN

二次信任 提交于 2019-12-18 08:54: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

Java maths - testing for NaN

寵の児 提交于 2019-12-18 06:16:11
问题 I would like to have some kind of project-wide fail fast mechanism (maybe a RuntimeException ) for any code that causes assignment of NaN . In my project NaN is never a valid value. I realise I could add asserts (using isNaN) or other tests throughout but I want to know if there is a more elegant way. 回答1: Yes, you can use AspectJ (aspect oriented programming) to throw an error whenever a value is set to NaN. Essentially, you want to intercept whenever a value is set, and perform some other

Python numpy.nan and logical functions: wrong results

爱⌒轻易说出口 提交于 2019-12-18 06:13:06
问题 I get some surprising results when trying to evaluate logical expressions on data that might contain nan values (as defined in numpy). I would like to understand why this results arise and how to implement the correct way. What I don't understand is why these expressions evaluate to the value they do: from numpy import nan nan and True >>> True # this is wrong.. I would expect to evaluate to nan True and nan >>> nan # OK nan and False >>> False # OK regardless the value of the first element #

how to test if a variable is pd.NaT?

怎甘沉沦 提交于 2019-12-18 05:27:41
问题 I'm trying to test if one of my variables is pd.NaT. I know it is NaT, and still it won't pass the test. As an example, the following code prints nothing : a=pd.NaT if a == pd.NaT: print("a not NaT") Does anyone have a clue ? Is there a way to effectively test if a is NaT? 回答1: Pandas NaT behaves like a floating-point NaN , in that it's not equal to itself. Instead, you can use pandas.isnull : In [21]: pandas.isnull(pandas.NaT) Out[21]: True This also returns True for None and NaN.

c# NaN comparison differences between Equals() and ==

二次信任 提交于 2019-12-18 04:34:15
问题 Check this out : var a = Double.NaN; Console.WriteLine(a == a); Console.ReadKey(); Prints "False" var a = Double.NaN; Console.WriteLine(a.Equals(a)); Console.ReadKey(); Prints "True"! Why it prints "True"? Due to floating point numbers specification, value that is NaN is not equal to itself! So it seems that Equals() method is implemented wrong... Am I missing something ? 回答1: I found an article addressing your question: .NET Security Blog: Why == and the Equals Method Return Different

Checking for NaN presence in a container

╄→尐↘猪︶ㄣ 提交于 2019-12-18 03:25:33
问题 NaN is handled perfectly when I check for its presence in a list or a set. But I don't understand how. [UPDATE: no it's not; it is reported as present if the identical instance of NaN is found; if only non-identical instances of NaN are found, it is reported as absent.] I thought presence in a list is tested by equality, so I expected NaN to not be found since NaN != NaN. hash(NaN) and hash(0) are both 0. How do dictionaries and sets tell NaN and 0 apart? Is it safe to check for NaN presence