Problem with iterating when using variable length python function arguments - python

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.

Related

geopy.distance.geodesic() but it says TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

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)]

How can I solve TypeError: unsupported operand type(s) for *: 'property' and 'property'

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

Pint list conversion

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?

TypeError: unsupported operand type(s) for -: 'str' and 'str'

a'$'
money=1000000;
portfolio=0;
value=0;
value=(yahoostock.get_price('RIL.BO'));
portfolio=(16*(value));
print id(portfolio);
print id(value);
money= (money-portfolio);
'''
I am getting the error:
Traceback (most recent call last):
File "/home/dee/dee.py", line 12, in <module>
money= (value-portfolio);
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Since money is integer and so is portfolio, I cant solve this problem..anyone can help???
value=(yahoostock.get_price('RIL.BO'));
Apparently returns a string not a number. Convert it to a number:
value=int(yahoostock.get_price('RIL.BO'));
Also the signal-to-noise ratio isn't very high. You've lots of (,), and ; you don't need. You assign variable only to replace them on the next line. You can make your code nicer like so:
money = 1000000
value = int(yahoostock.get_price('RIL.BO'));
portfolio = 16 * value;
print id(portfolio);
print id(value);
money -= portfolio;
money and portfolio are apparently strings, so cast them to ints:
money= int( float(money)-float(portfolio) )
As the error message clearly states, both are string, cast with int(var).
Note:
Let's see what can we decude from the error message:
portfolio must be string(str), which means value is also a string. Like this:
>>> 16*"a"
'aaaaaaaaaaaaaaaa'
and apparently you missed to post relevant code because the error message tells you that money is str as well.
I think the problem here is assuming that because you have initialised variables with integer values they will remain as integers. Python doesn't work this way. Assigning a value with = only binds the name to the value without paying any attention to type. For example:
a = 1 # a is an int
a = "Spam!" # a is now a str
I assume yahoostock.getprice(), like many functions that get data from websites, returns a string. You need to convert this using int() before doing your maths.

Python interpeter that randomly changes type or varrible

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(...).

Categories

Resources