Python Forum
Python ValueError The value of a Series is abmbiguous
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python ValueError The value of a Series is abmbiguous
#1
I have a function that loops through a dataframe and then loops through the Booking Statuses (there are 6 possible) and looks to see if the value of the Booking Status matches one of the values in the array. If it matches, it sets the value of the row for the dollar amount column for that Booking Status to the value of the TotalRent column. This has been running for over a month and all of the sudden started giving me this error.

It is specifically pointing to the if df.loc[i, "BookingStatus"] == y line.
Here is the error message:

File "c:\python bc projects\eventforecastfysqlspyder.py", line 311, in calculate_rent_fields
if df.loc[i, "BookingStatus"] == y:

File "C:\Users\BCCENTER\anaconda3\envs\convention_center\lib\site-packages\pandas\core\generic.py", line 1327, in __nonzero__
f"The truth value of a {type(self).__name__} is ambiguous. "

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

#loop through each row and set value of individual rent fields (Contract, Definite, T2, etc) 
#based on booking status - use array_booking_status (only contains those statuses in dataframe)
def calculate_rent_fields(df):
    for i in df.index:
        for y in array_booking_status:
            if df.loc[i, "BookingStatus"] == y:
                df.loc[i, y] = df.loc[i, "TotalRent"]
                break       
    return df
Reply
#2
FYI, I was getting really frustrated and now I believe this may not even be a real error. I was adding code to a calendar widget to allow a user to select a fiscal year from a drop down box and that's when the error I mentioned above started happening.

I finally decided to scrap the new code and retrieve a copy of the code from yesterday. It wasn't completely up to where I had it when I started adding the new code but it was close. I ran that original code and it runs perfectly again. I didn't change anything to the code that was being highlighted in the error above.

If anyone can explain this, I would be extremely grateful. I've been a programmer for a long time but only started learning python in July.

Thanks.
Reply
#3
I would not loop through the array, rather use 'in'
#loop through each row and set value of individual rent fields (Contract, Definite, T2, etc) 
#based on booking status - use array_booking_status (only contains those statuses in dataframe)
def calculate_rent_fields(df):
    for i in df.index:
        if df.loc[i,'BookingStatus'] in array_booking_status:
            df.loc[i, df.loc[i,'BookingStatus']] = df.loc[i, "TotalRent"]       
    return df
Does this work better for you?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fourier series in Python Armandito 3 2,061 Mar-18-2022, 10:26 AM
Last Post: Larz60+
  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() Rejoice 6 3,285 Aug-25-2020, 03:50 PM
Last Post: Rejoice
  Tableau Time Series Prediction using Python Integration tobimarsh43 0 1,915 Jul-24-2020, 10:38 AM
Last Post: tobimarsh43
  Convert quarterly time series to monthly time series donnertrud 1 5,177 May-22-2020, 10:16 AM
Last Post: pyzyx3qwerty
  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() bongielondy 2 14,136 Nov-27-2019, 03:25 PM
Last Post: ThomasL
  Why this python ValueError is not happening aurquiel 2 3,035 Aug-20-2018, 07:17 PM
Last Post: aurquiel
  Python- Help with try: input() except ValueError: Loop code to start of sequence Aldi 2 6,326 Mar-08-2018, 03:46 AM
Last Post: Larz60+
  tutorial series python directx hsunteik 2 5,799 Jan-12-2017, 09:06 AM
Last Post: j.crater
  Python Error (Traceback & ValueError) Help! helpplease 4 5,963 Dec-19-2016, 06:07 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020