For non-negative (unsigned) integers only, use isdigit():
>>> a = "03523"
>>> a.isdigit()
True
>>> b = "963spam"
>>> b.isdigit()
False
Documentation for isdigit(): Python2, Python3
For Python 2 Unicode strings:
isnumeric().
For non-negative (unsigned) integers only, use isdigit():
>>> a = "03523"
>>> a.isdigit()
True
>>> b = "963spam"
>>> b.isdigit()
False
Documentation for isdigit(): Python2, Python3
For Python 2 Unicode strings:
isnumeric().
Which, not only is ugly and slow
I'd dispute both.
A regex or other string parsing method would be uglier and slower.
I'm not sure that anything much could be faster than the above. It calls the function and returns. Try/except doesn't introduce much overhead because the most common exception is caught without an extensive search of stack frames.
The issue across programming languages is that any numeric conversion function has two kinds of results:
- A number, if the number is valid;
- A status code (e.g., via errno) or exception to show that no valid number could be parsed.
C (as an example) hacks around this a number of ways. Python lays it out clearly and explicitly.
I think your code for doing this is just fine. The only thing that could be cleaner is moving the return True into an else block, to be clear that it's not part of the code under test – not that there's much ambiguity.
def is_number(s):
try:
float(s)
except ValueError: # Failed
return False
else: # Succeeded
return True
I want to tell my code that if the user enters anything rather than numbers (either int or float) print that it's not a number. Do we have a method for it?
try:
a = int(input('Enter a number: '))
b = int(input('Enter another number: '))
except ValueError:
print('Please enter numbers.')
else:
add = a + b
print(add)As Imran says, your code is absolutely fine as shown.
However, it does encourage clients of isFloat down the "Look Before You Leap" path instead of the more Pythonic "Easier to Ask Forgiveness than Permission" path.
It's more Pythonic for clients to assume they have a string representing a float but be ready to handle the exception that will be thrown if it isn't.
This approach also has the nice side-effect of converting the string to a float once instead of twice.
A more complete generalization:
def strType(xstr):
try:
int(xstr)
return 'int'
except:
try:
float(xstr)
return 'float'
except:
try:
complex(xstr)
return 'complex'
except:
return 'str'
print("4", strType("4"))
print("12345678901234567890", strType("12345678901234567890"))
print("4.1", strType("4.1"))
print("4.1+3j", strType("4.1+3j"))
print("a", strType("a"))
This is the expected behaviour as str.isnumeric() checks "whether all characters in each string are numeric" (docs).
But the question is interesting, because np.nan is a float, so if you wish to remove bear but keep the nan, pd.to_numeric(test_num['col1'], errors='coerce') will not work, as it converts bear to a float (nan).
So you could first filter out all values that cannot be converted to float prior to running pd.to_numeric on the Series:
def check_numeric(x):
try:
float(x)
return True
except:
return False
test_num = test_num[test_num['col1'].apply(check_numeric)]
test_num['col1'] = pd.to_numeric(test_num['col1'])
This should work for you.
pd.Series(np.where(test_num['col1'].isna(), np.nan,
pd.to_numeric(test_num['col1'], errors='coerce').notnull()
)).replace({1:True, 0:False})
0 True
1 True
2 True
3 NaN
4 False