Change your code to:
for element in my_series:
if type(element) == float and pd.isna(element):
print('do A')
else:
print('do B')
Edit following the comment by Peter
I on purpose didn't change the original concept of processing the source Series in a loop. It looks like both print instructions are rather "placeholders", to be replaced with one piece of code for NaN values and another for other values.
Answer from Valdi_Bo on Stack OverflowChange your code to:
for element in my_series:
if type(element) == float and pd.isna(element):
print('do A')
else:
print('do B')
Edit following the comment by Peter
I on purpose didn't change the original concept of processing the source Series in a loop. It looks like both print instructions are rather "placeholders", to be replaced with one piece of code for NaN values and another for other values.
No need for explicit for loops. Based on your second attempt:
# Setup
my_series = pd.Series([np.nan, np.nan, ['A', 'B']])
# Solution
np.where(my_series.isnull(), 'do A', 'do B')
# Output
array(['do A', 'do A', 'do B'], dtype='<U4')
python - numpy.isnan(value) not the same as value == numpy.nan? - Stack Overflow
python - AttributeError: 'numpy.float64' object has no attribute 'log10' - Stack Overflow
BUG: np.isna() is working
nan in float64 does not evaluate to nan
numpy.log10 is a "ufunc", and the method Series.apply(func) has a special test for numpy ufuncs which makes test.apply(log10) equivalent to np.log10(test). This means test, a Pandas Series instance, is passed to log10. The data type of test is object, which means that the elements in test can be arbitrary Python objects. np.log10 doesn't know how to handle such a collection of objects (it doesn't "know" that those objects are, in fact, all np.float64 instances), so it attempts to dispatch the calculation to the individual elements in the Series. To do that, it expects the elements themselves to have a log10 method. That's when the error occurs: the elements in the Series (in this case, np.float64 instances) do not have a log10 method.
A couple alternative expression that should do what you want are np.log10(test.astype(np.float64)) or test.astype(np.float64).apply(np.log10). The essential part is that test.astype(np.float64) converts the data type of the Series object from object to np.float64.
I had a similar error message when using the standard deviation (np.std) instead of np.log10:
'AttributeError: 'numpy.float64' object has no attribute 'sqrt',
and this although I had previously converted the Pandas object X to a numpy array via np.asarray(X).
I could solve this problem by applying the above-mentioned solution:
X = pd.read_excel('file.xls')
Y = np.asarray(X).astype(np.float64)
Z = np.std(Y,axis=0)
np.isnan can be applied to NumPy arrays of native dtype (such as np.float64):
In [99]: np.isnan(np.array([np.nan, 0], dtype=np.float64))
Out[99]: array([ True, False], dtype=bool)
but raises TypeError when applied to object arrays:
In [96]: np.isnan(np.array([np.nan, 0], dtype=object))
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Since you have Pandas, you could use pd.isnull instead -- it can accept NumPy arrays of object or native dtypes:
In [97]: pd.isnull(np.array([np.nan, 0], dtype=float))
Out[97]: array([ True, False], dtype=bool)
In [98]: pd.isnull(np.array([np.nan, 0], dtype=object))
Out[98]: array([ True, False], dtype=bool)
Note that None is also considered a null value in object arrays.
A great substitute for np.isnan() and pd.isnull() is
for i in range(0,a.shape[0]):
if(a[i]!=a[i]):
//do something here
//a[i] is nan
since only nan is not equal to itself.
I am trying to plot a .txt file of lines of the form: filename.txt date magnitude V098550.txt 362.0 3.34717962317 but I am getting the error "numpy.float64' object has no attribute 'replace'". Does anyone know if this is a syntax error with numpy, or how I can resolve my issue?
import numpy as np
import matplotlib.pyplot as plt
x, y = np.loadtxt ("condensed.txt", usecols=(1, 2),
delimiter=",", unpack=True)
for ii in range (len(x)): x[ii].replace('.txt', '.lc\n') jd, npmag = np.loadtxt ("/net/jovan/export/jovan/oelkerrj/Vela/rotation/Vela/"+x[ii], usecols= (1, 2), unpack=True)
plt.scatter (jd, npmag)
plt.xlabel ('Time')
plt.ylabel ('Mag')
plt.ylim ([max (npmag), min (npmag)])
plt.show() # aftertest comment this out
fileName = x[ii][:-3] + ".png"
plt.savefig(fileName)print "done"
Hallo Everyone,
I hope you are all fine, I have this code which I am getting error from , I run the same code on my Spyder and I do not get any error but when I run it on Jupyter Notebook I get this error :
'numpy.float64' object has no attribute 'replace'
this is the code that I have :
oli_row2 = df_oli.iloc[2].tolist()
idx = oli_row2.index("DB")
check_words = oli_row2[idx:]
exact_order = ['NN','ch','ab','An','tr','bcc',]
for i in range(len(check_words)):
if not(check_words[i].replace(" ","")==exact_order[i].replace(" ","")):
new_row = table.add_row()
new_row.cells[0].text = 'Oli'
new_row.cells[1].text = 'Words are out of order in Column'
new_row.cells[2].text = str(check_words[idx])
oli_flag = True
print('Out of order')
I googled lit bit but I could not find any solution.
Thank you in advance,
Best Regards,