0

In a function, i'm returning a list of two values if the values are there or an empty list, like below:

def func1(prikey):
    try:
      df = somesql
      for index, rW in df.iterrows():
          retvalue = [rW['id_in_int'],rW['time_in_str']]
    except:
        retvalue = []
    return retvalue

In the main code, i'm assigning to the variable:

newdf['newid'],newdf['thistime'] = func1(newdf['prikey'])

But i got the error "not enough values to unpack (expected 2, got 0)" So, in the function, i tried as below

retvalue = [[],[]]

But got the error as "Length of values does not match length of index" And retvalue = [0,0] didnt give any error and the value of newdf['newid'],newdf['thistime'] is zero.

And, when i'm trying to check whether the value is zero in further lines as if(newdf['newid']==0):, which gives the error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

I tried checking len() as well which didnt work too. Any help is appreciated.

Ilyas
  • 181
  • 1
  • 1
  • 8

3 Answers3

0

I guess you can't assign 0 to a column. try assigning ""(empty string) or np.NaN. Also newdf['newid'] is of type Series and you can't compare it with a bool so you can use newdf['newid'].empty or len(newdf['newid']) == 0

shailesh bhagat
  • 149
  • 1
  • 8
  • tried retvalue = [np.NaN,np.NaN] and didnt receive the error. But, while checking if(newdf['newid']==np.NaN):, getting the error as "The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()" – Ilyas Apr 05 '20 at 16:27
  • I guess you have to use newdf['newid'].empty to check if it's empty, as it is a Series object and you can't compare it with a bool. you can also try if len(newdf['newid']) == 0 – shailesh bhagat Apr 05 '20 at 17:28
0

Your code below returns 1 list,

def func1(prikey):
    try:
      df = somesql
      for index, rW in df.iterrows():
          retvalue = [rW['id_in_int'],rW['time_in_str']]  #here 1 list!
    except:
        retvalue = [] #here 1 list!
    return retvalue

This will result in the: "not enough values to unpack (expected 2, got 0)" error in your next statement:

newdf['newid'],newdf['thistime'] = func1(newdf['prikey'])

As there is no example code for the other error you described:

The 'or' and 'and' python statements require truth-values. If these are considered ambiguous you should use "bitwise" | (or) or & (and) operations. Check this one

samy
  • 63
  • 1
  • 3
  • i indeed got through that link earlier, but im not using multiple conditions. Its just not working even for a single operator – Ilyas Apr 05 '20 at 16:31
0

Thanks Shailesh and samy for your responses. Inside the function, i used retvalue = [0,0] to resolve "not enough values to unpack (expected 2, got 0)" error.

The article https://towardsdatascience.com/apply-and-lambda-usage-in-pandas-b13a1ea037f7 helped me to use ".apply" on the series and below is the code.

newdf['newwaittime'] = newdf['newid'].apply(lambda x: 0 if x==0 else get_new_wait_time(x))

where I'm checking whether the value of newdf['newid'] is zero or not.

Ilyas
  • 181
  • 1
  • 1
  • 8