I am running a code to select chunks from a big file. I am getting some strange error that is
"Invalid literal for float(): E-135"
Does anybody know how to fix this? Thanks in advance.
Actually this is the statement that is giving me error
float (line_temp[line(line_temp)-1])
This statement produces error
line_temp is a string
'line' is any line in an open and file also a string.
You need a number in front of the E to make it a valid string representation of a float number
>>> float('1E-135')
1e-135
>>> float('E-135')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): E-135
In fact, which number is E-135 supposed to represent? 1x10^-135?
Valid literal forms for floats are here.
Looks like you are trying to convert a string to a float. If the string is E-135, then it is indeed an invalid value to be converted to a float. Perhaps you are chopping off a digit in the beginning of the string and it really ought to be something like 1E-135? That would be a valid float.
May I suggest you replace
float(x-y)
with
float(x) - float(y)
Ronald, kindly check the answers again. They are right.
What you are doing is: float(EXPRESSION), where the result of EXPRESSION is E-135. E-135 is not valid input into the float() function. I have no idea what the "line_temp[line(line_temp)-1]" does, but it returns incorrect data for the float() function.
Related
I'm struggling to convert an object to a float.
df_final['INBCS'] = df_final['INBCS'].astype(float)
It keeps saying: ValueError: could not convert string to float: '1,620,000'
If I try a different approace, I get mostly NAN results.
print(pd.to_numeric(df_final['INBCS'], errors='coerce'))
I tried one more approach, and I still get errors.
df_final = df_final[df_final['INBCS'].apply(lambda x: x.isnumeric())]
There are no NANs in the data; I already converted them to zeros. When I print the data, it shows commas, but there are no commas at all. I even did ran a replace function to get rid of any potential commas, but again, there are no commas in the data. Any idea what's wrong here? Thanks.
The reason you can't convert that string to a float is that Python doesn't know what to do with the commas. You can reproduce this easily:
>>> float('1,000')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '1,000'
It's tempting to just remove the commas and parse the number, but there's an internationalization concern. In some countres, a comma separates thousands (eg, "1,000,000" is one million). In other countries, commas separate decimals (eg, "1,05" is one and five one-hundredths).
For that reason, it's best to use localization to parse a number like that if you can't get it in a native form. See this answer for details on that.
The reason is because you have , there, you can do:
df_final['INBCS'] = df_final['INBCS'].replace(',','')
df_final['INBCS'] = df_final['INBCS'].astype(float)
should work.
Try this:
string = '1,620,000'
decimal = float(''.join(string.split(',')))
print(type(decimal), decimal)
# Prints (<type 'float'>, 1620000.0)
This first gets rid of all the commas using split(','), then recreates the string using ''.join(). Finally, it converts the whole thing to a float using float().
I have some fairly hairy unicode strings with numbers in them that I'd like to test the value of. Normally, I'd just use str.isnumeric to test for whether it could be converted via int() but I'm encountering cases where isnumeric returns True but int() raises an exception.
Here's an example program:
>>> s = '⒍'
>>> s.isnumeric()
True
>>> int(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '⒍'
Unicode is always full of surprises, so I'm happy to just be robust to this case and use a try/except block to catch unusual numbers. However, I'd be happier if I could still convert them to integers. Is there a consistent way to do this?
If you want to test if a string can be passed to int, use str.isdecimal. Both str.isnumeric and str.isdigit include decimal-like characters that aren't compatible with int.
And as #abarnert has mentioned in the comments, the most guaranteed way to test if a string can be passed to int is to simply do it in a try block.
On the other hand, '⒍' can be converted to an actual digit with the help of the unicodedata module, e.g.
print(unicodedata.digit('⒍'))
would output 6.
I don't know how much luck you'll have, but unicodedata may handle some cases (python 3 code):
>>> import unicodedata
>>> unicodedata.normalize('NFKC', '⒍')
'6.'
Slightly better. As to testing, if you want an int you could just int() it and catch the exception.
The best way to find out if a string can be converted to int is to just try it:
s = '⒍'
try:
num = int(s)
except ValueError:
# handle it
Sure, you can try to figure out the right way to test the string in advance, but why? If the rule you want is "whatever int accepts", just use int.
If you want to convert something that is a digit, but isn't a decimal, use the unicodedata module:
s = '⒍'
num = unicodedata.digit(s) # 6
num = unicodedata.numeric(s) # 6.0
num = unicodedata.decimal(s) # ValueError: not a decimal
The DIGIT SIX FULL STOP character's entry in the database has Digit and Numeric values, despite being a Number, Other rather than a Number, Decimal Digit (and therefore not being compatible with int).
I have a list with values that should be number. Right now they are an object however:
later object
opstarten object
dtype: object
I have tried to change the column to a str type by doing:
df_analyse_num[["later"]] = df_analyse_num[["later"]].astype(str)
This does not seem to work however cause when I analyse my types it still says object.
Also when I try to convert it to a string something goes wrong. If I do:
df_analyse_num[["later"]] = df_analyse_num[["later"]].astype(str).astype(int)
It gives me the following error:
File "pandas\lib.pyx", line 937, in pandas.lib.astype_intsafe (pandas\lib.c:16667)
File "pandas\src\util.pxd", line 60, in util.set_value_at (pandas\lib.c:67540)
ValueError: invalid literal for int() with base 10: '30.0'
Any thoughts where this goes wrong?
Not an expert on pandas, but try float first to handle the decimal point which indicates a float, then int:
something.astype(str).astype(float).astype(int)
Here is the problem in "native" python:
int('30.0')
Which fails similarly:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '30.0'
If we use float first it works since converting float to int is possible:
int(float('30.0'))
Expected result:
30
i am new in python, i just try to play a video in avg player through python. All the videos are played successfully, but one video has through this value error. i am not sure why this error was happened . if you know describe me.
The specific problem arises because the software tries to interpret 107.24 as an integer number, which it is not.
Why it does this, or where this number is coming from, is really hard to tell from the little information given in your question.
'107.24' is a float string and int() can't convert a float string, use float().
>>> a='107.24'
>>> int(a)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
int(a)
ValueError: invalid literal for int() with base 10: '107.24'
>>> float(a)
107.24
I have a large file with numbers in the form of 6,52353753563E-7. So there's an exponent in that string. float() dies on this.
While I could write custom code to pre-process the string into something float() can eat, I'm looking for the pythonic way of converting these into a float (something like a format string passed somewhere). I must say I'm surprised float() can't handle strings with such an exponent, this is pretty common stuff.
I'm using python 2.6, but 3.1 is an option if need be.
Nothing to do with exponent. Problem is comma instead of decimal point.
>>> float("6,52353753563E-7")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): 6,52353753563E-7
>>> float("6.52353753563E-7")
6.5235375356299998e-07
For a general approach, see locale.atof()
Your problem is not in the exponent but in the comma.
with python 3.1:
>>> a = "6.52353753563E-7"
>>> float(a)
6.52353753563e-07