I have a very long list I'm trying to convert as fast as possible.
Currently I'm doing the following which is very fast compared to a 1 by 1 conversion:
alist = [1,2,3,4, ... 100000]
list_with_unit = alist * ureg('meter')
list_converted = list_with_unit.to(ureg('feet'))
The problem is that if alist contains a None value I will get:
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
Has anybody an idea how to resolve this issue so that for None values I get a None returned?
Related
Var = "Jonathan"
new_data = {"premium": + Var}
json_object['classes'][states].update(new_data)
This was the code I used, the error I got was this: TypeError: bad operand type for unary +: 'str'
How can I work around this? In the actual code the string is not Jonathan, it is something that is created by the code.
It should be:
Var = "Jonathan"
new_data = {"premium": Var}
just remove + sign
I'm new and don't understand things. I want to make a new list of distances between every coordinates I have, a list of the distances of point1-point2, point1-point3, point2-point3.
so my code is:
list_of_coords = [(5.55, 95.3175), (3.583333, 98.666667), (-0.95556, 100.36056)]
list_of_distances = [geopy.distance.geodesic(combo).km for combo in combinations(list_of_coords,2)]
anddd when I try to run it, it says:
TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'
How to make it run properly? Thank you!
As I can see in the documentation, geodesic takes multiple arguments, like *args.
So try unpacking:
list_of_distances = [geopy.distance.geodesic(*combo).km for combo in combinations(list_of_coords, 2)]
Or unpack iteration:
list_of_distances = [geopy.distance.geodesic(a, b).km for a, b in combinations(list_of_coords, 2)]
I tried to do calculation by retrieving values by its index and column name for any pair of two rows in a for loop in Python but the TypeError popped up. IRdelta_WS_agg is a named tuple list and ws1.WS and ws2.WS are not read properly. Does anyone have idea to solve this? Many thanks!
for bucket in buckets:
for ws1 in IRdelta_WS_agg:
for ws2 in IRdelta_WS_agg:
correl = get_ir_delta_phi_rho(ws1.RiskType, ws1.Curve, ws1.Tenor,
ws2.RiskType,ws2.Curve, ws2.Tenor)
variance = ws1.WS * ws2.WS * correl
K += variance
variance = ws1.WS * ws2.WS * correl
TypeError: unsupported operand type(s) for *: 'property' and 'property'
first you should check the type of ws1.WS by
type(IRdelta_WS_agg[0])
type(IRdelta_WS_agg[0].WS)
if it is your custom data type,
you should define magic method
def __mul__(self, other):
return int(self.XXX) * int(other.XXX)
or you can use
__repr__ or __str__
to make string of number
then change the type before multiplication like:
int(ws1.WS) * float(ws2.WS)
if you need more help
you should provide more info about their data types or class object
def function1(self,*windows):
xxx_per_win = [[] for _ in windows]
for i in range(max(windows),self.file.shape[0]):
for j in range(len(windows)):
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
...
...
...
o = classX(file)
windows = [3,10,20,40,80]
output = o.function1(windows)
If I run the code above it says:
for i in range(max(windows),self.file.shape[0]):
TypeError: 'list' object cannot be interpreted as an integer
and:
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
TypeError: unsupported operand type(s) for -: 'int' and 'list'
This problem only occurs when windows is variable length (ie *windows and not just windows).
How do I fix this? What is causing this?
The max function expects to be passed multiple arguments, not a tuple containing multiple arguments. Your windows variable is a tuple.
So, not:
for i in range(max(windows),self.file.shape[0]):
Do this instead:
for i in range(max(*windows),self.file.shape[0]):
On your second error, involving the line:
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
# TypeError: unsupported operand type(s) for -: 'int' and 'list'
Ok, you are subtracting, and it's complaining that you can't subtract a list from an integer. And since I have no idea what windows[j] contains, I can't say whether there's a list in there or not.. but if there is, there can't be. You haven't given us a working example to try.
What I suggest you do is to put some debugging output in your code, eg:
print('i=%s, j=%s, windows[j]=%s' % (i, j, windows[j]))
and thus see what your data looks like.
So I am learning python for fun and I have come across an error that completely stumps me. When I am running my code I get the following error:
TypeError: unsupported operand type(s) for /: 'int' and 'type'
this error is triggered by the division in:
for i in items:
print i[1]
multiplier = WeightLeft / i[1]
the thing that has me so confused is that when i print i[1] it prints
<type 'int>
I tried to force the denominator to be an int by putting int(i[1]) as the denominator but i get a new error:
TypeError: descriptor '__trunc__' of 'int' object needs an argument
Any advice someone could give would be greatly appreciated.
i[1] is the type object int, not an instance of this type. Trying to convert this type object to an integer is like calling int(int).
<type 'int'> is what you'd get if you do i[1] = int, so I'm guessing that somewhere you have i[1] = int instead of i[1] = int(...).