0
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?

Trajan
  • 1,380
  • 6
  • 20
  • 41
  • 1
    when you run only the function **definition** no error will be raised, can you provide a minimal example code that **calls** the function to throw an error? the issue is clearly with the format of the input data which is not shown. – Tadhg McDonald-Jensen Dec 10 '19 at 21:00
  • 1
    How do you call the method ? – azro Dec 10 '19 at 21:00
  • What are some example values for the `windows` variable? – damon Dec 10 '19 at 21:04
  • @azro any better? – Trajan Dec 10 '19 at 21:07
  • @TadhgMcDonald-Jensen any better? – Trajan Dec 10 '19 at 21:07
  • what ?? don't understand what you mean^^ – azro Dec 10 '19 at 21:07
  • @azro to answer your question (2nd comment) – Trajan Dec 10 '19 at 21:12
  • does calling `o.function1(3,10,20,40,80)` do what you are expecting? right now `windows` variable in your method is a list with one element, the only element is a tuple which is probably where your confusion is coming in. you might want to be doing `o.function1(*windows)` – Tadhg McDonald-Jensen Dec 10 '19 at 21:16
  • @TadhgMcDonald-Jensen yes. sorry i had mixed up my code in the question which I have fixed now. – Trajan Dec 10 '19 at 21:21
  • let me repeat: does calling `o.function1(3,10,20,40,80)` do what you are expecting? does calling `o.function1(*windows)` do what you are expecting? you say that without using `*rest` parameters works so I highly suspect these will both work. I just don't understand why you need rest syntax here if you are going to be passing it lists anyway. – Tadhg McDonald-Jensen Dec 10 '19 at 21:32
  • Does this answer your question? [What does \*\* (double star/asterisk) and \* (star/asterisk) do for parameters?](https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters) – Tadhg McDonald-Jensen Dec 10 '19 at 21:54

1 Answers1

2

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.

little_birdie
  • 5,600
  • 3
  • 23
  • 28