replacing nan with 0 error:nan not supported for input type

  • Last Update :
  • Techknowledgy :

Ideally, avoid building object arrays in the first place. But given that you have this object array, to build the new array, you might as well just loop over the items in a in a list comprehension:

import numpy as np
a = np.array(['2014-06-01 03:14:34', np.nan], dtype = 'O')
b = np.array([item == item
   for item in a


[1 0]

NaN has the property that NaN != NaN. NaN is the only object in the pantheon of standard Python objects which has this property. Custom classes can be defined whose instances can also have this property. As long as a does not contains instances of such a custom class, you could use the item != item property to test for NaN:

In[43]: float('nan') != float('nan')
Out[43]: True

In[44]: np.nan != np.nan
Out[44]: True

In the code below, fields contains list of field names used in object array.

v_data = np.zeros((o_data.shape[0], len(fields)))
for rcounter, row in enumerate(o_data):
   for fcounter, field in enumerate(fields):
   v_data[rcounter, fcounter] = row[field]

You may use function:

def replace_nan(num):
   if not numpy.nan_to_num(num):
   return 1

Suggestion : 2

Last Updated : 24 Dec, 2021,GATE CS 2021 Syllabus





Suggestion : 3

As we have seen, Pandas treats None and NaN as essentially interchangeable for indicating missing or null values. To facilitate this convention, there are several useful methods for detecting, removing, and replacing null values in Pandas data structures. They are:,In the masking approach, the mask might be an entirely separate Boolean array, or it may involve appropriation of one bit in the data representation to locally indicate the null status of a value.,The way in which Pandas handles missing values is constrained by its reliance on the NumPy package, which does not have a built-in notion of NA values for non-floating-point data types.,NaN and None both have their place, and Pandas is built to handle the two of them nearly interchangeably, converting between them where appropriate:

import numpy as np
import pandas as pd
vals1 = np.array([1, None, 3, 4])
array([1, None, 3, 4], dtype = object)
for dtype in ['object', 'int']:
   print("dtype =", dtype) %
   timeit np.arange(1E6, dtype = dtype).sum()
dtype = object
10 loops, best of 3: 78.2 ms per loop

dtype = int
100 loops, best of 3: 3.06 ms per loop

Suggestion : 4

NaN is short for Not a number. It is used to represent entries that are undefined. It is also used for representing missing values in a dataset.,The output array has true for the indices which are NaNs in the original array and false for the rest.,You can also use interpolation to fill the missing values in a data frame. Interpolation is a slightly advanced method as compared to .fillna().,The concept of NaN existed even before Python was created. IEEE Standard for Floating-Point Arithmetic (IEEE 754) introduced NaN in 1985.

import numpy as np
arr = np.array([1, np.nan, 3, 4, 5, 6, np.nan])
[1. nan 3. 4. 5. 6. nan]

Suggestion : 5

To solve this error, you can check your data set for NaN values using numpy.isnan() and infinite values using numpy.isfinite(). You can replace NaN values using nan_to_num() if your data is in a numpy array or SciKit-Learn’s SimpleImputer.,We raise the error because the SimpleImputer method does not support infinite values. To solve this error, you can replace the np.inf with np.nan values as follows:,We can replace the NaN and infinity values using the nan_to_num() method. The method will set NaN values to zero and infinity values to a very large number. Let’s look at the code and the clean data:,Then we can use the SimpleImputer to fit and transform the data. In this case, we will replace the missing values with the mean along the column where each NaN value occurs.

pos_inf = float('inf')

neg_inf = -float('inf')

print('Positive infinity: ', pos_inf)

print('Negative infinity: ', neg_inf)


print('Positive infinity: ', pos_inf)

print('Negative infinity: ', neg_inf)
Positive infinity: inf
Negative infinity: -inf

The data generation looks as follows:

# Import numpy and AffinityPropagation

import numpy as np

from sklearn.cluster
import AffinityPropagation

# Number of NaN values to put into data

n = 4

data = np.random.randn(20)

# Get random indices in the data

index_nan = np.random.choice(data.size, n, replace = False)

# Replace data with NaN

data.ravel()[index_nan] = np.nan


The data consists of twenty random values, four of which are NaN, and the rest are numerical values. Let’s try to fit the data using the AffinityPropagation() class.

af = AffinityPropagation(random_state = 5).fit([data])
af= AffinityPropagation(random_state=5).fit([data])
ValueError: Input contains NaN, infinity or a value too large
for dtype('float64').

Suggestion : 6

Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the nan, posinf and/or neginf keywords.,Value to be used to fill positive infinity values. If no value is passed then positive infinity values will be replaced with a very large number.,If x is inexact, NaN is replaced by zero or by the user defined value in nan keyword, infinity is replaced by the largest finite floating point values representable by x.dtype or by the user defined value in posinf keyword and -infinity is replaced by the most negative finite floating point values representable by x.dtype or by the user defined value in neginf keyword.,Value to be used to fill negative infinity values. If no value is passed then negative infinity values will be replaced with a very small (or negative) number.

>>> np.nan_to_num(np.inf)
   np.nan_to_num(-np.inf) -
   1.7976931348623157e+308 >>>
   x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>>
array([1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -
      1.28000000e+002, 1.28000000e+002
   ]) >>>
   np.nan_to_num(x, nan = -9999, posinf = 33333333, neginf = 33333333)
array([3.3333333e+07, 3.3333333e+07, -9.9990000e+03,
      -1.2800000e+02, 1.2800000e+02
   ]) >>>
   y = np.array([complex(np.inf, np.nan), np.nan, complex(np.nan, np.inf)])
array([1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -
      1.28000000e+002, 1.28000000e+002
   ]) >>>
array([1.79769313e+308 + 0.00000000e+000 j, # may vary
      0.00000000e+000 + 0.00000000e+000 j,
      0.00000000e+000 + 1.79769313e+308 j
   ]) >>>
   np.nan_to_num(y, nan = 111111, posinf = 222222)
array([222222. + 111111. j, 111111. + 0. j, 111111. + 222222. j])

Suggestion : 7

Last modified: Aug 5, 2022, by MDN contributors

NaN === NaN; // false
Number.NaN === NaN; // false
isNaN(NaN); // true
isNaN(Number.NaN); // true
Number.isNaN(NaN); // true

function valueIsNaN(v) {
   return v !== v;
valueIsNaN(1); // false
valueIsNaN(NaN); // true
valueIsNaN(Number.NaN); // true
isNaN('hello world'); // true
Number.isNaN('hello world'); // false
isNaN(1n); // TypeError: Conversion from 'BigInt' to 'number' is not allowed.
Number.isNaN(1n); // false
const arr = [2, 4, NaN, 12];
arr.indexOf(NaN); // -1 (false)
arr.includes(NaN); // true
arr.findIndex((n) => Number.isNaN(n)); // 2