Float required Error while using logarithm - python

Hi I'm getting TypeError and i just don't know why...
x=float(40)
base=float(10)
math.log(x, [base])
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
TypeError: a float is required

math.log(x, [base]) doesn't literally mean "put base in brackets". This is what the documentation uses to denote an optional argument.
Remove them and it'll work
math.log(x, base)
Also, you don't need to use the float builtin to declare floats. Just add on a decimal component to your number and it will become a float:
x = 40.0
math.log(x, 10)

Related

Cant convert an object into an 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

How can I create a Decimal with a large exponent in Python 3?

Python 3 seems to have some arbitrary limit to Decimal sizes that Python 2 does not. The following code works on Python 2:
Decimal('1e+100000000000000000000')
But on Python 3 I get:
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
Increasing the precision does not help. Why is this happening? Is there something I can do about it?
It would appear that Decimals actually can't hold arbitrarily long numbers:
>>> d = Decimal('10') ** Decimal('100000000000000000000')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.Overflow: [<class 'decimal.Overflow'>]
Indeed, I never heard that arbitrarily long numbers was the point of Decimal - just proper rounding and decimals of arbitrary precision. If you want an arbitrarily long number, that's what longs are for, and in Python3 that's just what you've got.
>>> d = 10 ** 100000000000000000000
(Though it takes a long long while to compute this. My Mac book with I believe a core i5 still hasn't finished after a couple of minutes. Heck, even the string 1, followed by all those zeroes, is going to be really really big.)
For further kicks and grins, I discovered that you can configure the overflow value, apparently, though you still can't get such a whopping big number:
>>> from decimal import getcontext
>>> getcontext().Emax = 100000000000000000000
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C ssize_t

Python string interpolation using dynamic length strings (using *) with keyword arguments (not `string.format`)

For a legacy system that's in use in quite a few places with the string interpolation system I need to implement some code that formats a string with a specific length. I am aware that a rjust or ljust can solve this, but I'm trying to answer the question whether this is possible with the standard string interpolation system.
Examples:
>>> '%0*d' % (5, 3)
'00003'
>>> '%(value)05d' % dict(value=3)
'00003'
Now the question is, how can I combine these two?
My failed attempts:
>>> '%(value)*d' % dict(value=(5, 3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: * wants int
>>> '%(value)*d' % dict(value=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> '%(value)*d' % {'*': 5, 'value': 3}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> '%(value)*d' % {'*value': 5, 'value': 3}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
The question is: how can the asterisk and keyword arguments be combined using string interpolation?
Note: str.format is not an answer to this question, I'm not looking for alternatives but simply to answer the question whether this is possible or not. Replacing string interpolation with str.format would require many users of the current library to modify the function calls which is a unfavourable option for the near future.
No, you can't do this. Per the documentation (emphasis mine):
Minimum field width (optional). If specified as an '*' (asterisk), the actual width is read from the next element of the
tuple in values, and the object to convert comes after the minimum
field width and optional precision.
You need to be passing a tuple, not a dictionary, as values to use the * field width. This is then made explicit:
When the right argument is a dictionary (or other mapping type), ...
no * specifiers may occur in a format (since they require a sequential
parameter list).

TypeError with ufunc bitwise_xor

In my program which traces out the path of a particle, I get the following error:
Traceback (most recent call last):
File "C:\Users\Felix\Google Drive\Research\particles.py", line 154, in <module>
bfield += b_X(r_p(r,pos[2]))*(r_p(r,pos[2])/r)
*((r-r_p(r,pos[2]))**2+pos[2]**2)^(-1/2)*np.array
([(1-r_p(r,pos[2])/r)*pos[0],(1-r_p(r,pos[2])/r)*pos[1],pos[2]])
TypeError: ufunc 'bitwise_xor' not supported for the input types,
and the inputs could not be safely coerced to any supported types
according to the casting rule ''safe''
I can't seem to find what's going on. I don't have any instances of xor (although I suppose it might be encoded in an if/else statement).
In the offending line you are using a ^ when you want a ** to raise a value to a power. Python interprets this as an xor:
bfield += b_X(r_p(r,pos[2]))*(r_p(r,pos[2])/r)*((r-r_p(r,pos[2]))**2+
pos[2]**2)^(-1/2)*np.array([(1-r_p(r,pos[2])/r)*pos[0],
(1-r_p(r,pos[2])/r)*pos[1],pos[2]])
See:
http://docs.python.org/2/reference/expressions.html#binary-bitwise-operations
Use ** rather than ^ for raising a value to a power in python.

Parsing a string representing a float *with an exponent* in Python

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

Categories

Resources