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')
if np.isnan(grad_norm.cpu()): AttributeError: 'float' object has no attribute 'cpu'
BUG: np.isna() is working
I have a problem to check the range and type of the entry values using .get: "AttributeError: 'float' object has no attribute 'get'"
python - float object has no attribute isna - Stack Overflow
The first correction: Don't use df as the parameter name in func, because the passed object is a row. Use e.g. row instead.
The second correction is that some cells contain values of string type, which has no isna() method. Use pd.isna() instead, as it works on a source argument of any type.
So define your function e.g. as:
def func(row):
if pd.isna(row.check1) & pd.isna(row.check2):
return row.colb
else:
return '-'
I added another return for else variant, but I assume that you have a couple of elif ... instructions there.
You are applying the isna method to a float object which, as the error states, doesn't have such a method:
>>> import pandas as pd
>>> pd.Series([1, np.nan]).apply(lambda x: x.isna())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ubuntu/documents/assets/envs/venv/lib/python3.6/site-packages/pandas/core/series.py", line 4212, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas/_libs/lib.pyx", line 2403, in pandas._libs.lib.map_infer
File "<stdin>", line 1, in <lambda>
AttributeError: 'float' object has no attribute 'isna'
You could instead use np.isnan to test whether a float is nan, like so:
>>> pd.Series([1, np.nan]).apply(lambda x: True if not np.isnan(x) else False)
0 True
1 False
dtype: bool
So your function would look like this:
def func(df):
try:
test = np.isnan(df['check1']) and np.isnan(df['check2'])
except Exception as e:
if 'not supported for the input types' in str(e):
test = False
else:
raise
return df['colb'] if test else df
You might consider using some other variable name for func besides df as apply applies functions row-wise, not necessarily on an entire dataframe.
[Solved - thanks to DisasterArt]
https://codeshare.io/246gXj
I keep getting this error:
AttributeError: 'float' object has no attribute 'time'
I don't see anything wrong? Thanks!
Try this instead,
print(
"{:.3f}% {} ({} sentences)".format(pcent, gender, nsents)
)
Refer the latest docs for more examples and check the Py version!
You could also use {:.3%} instead of {:.3f}%.
It will transform the value into percentages automatically.
That means "{:.3%}".format(0.3) will print "30%" while you have to write "{:.3f}%".format(0.3 * 100) to get "30%" as well.
Hi,
Error is as follows:
AttributeError: 'str' object has no attribute 'isna' from the following code:
def sup_rank(row): if row['A'].isna(): if row['B'].notna(): return "Paid" else: return "Not Paid"