0

I have a column with values that are negative and positive. If the values are negative I want to add (24*60) to them. if they are positive they will remain the same. I have written the below code but it is throwing an error: "The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()"

I tried .any() and .all() but it is not working

    if df_da.loc[i,'Time_diff'] < 0 :
        df_da.loc[i, "Time_diff"]= df_da.loc[i, "Time_diff"]+(24*60)
    else:
        df_da.loc[i, "Time_diff"]= df_da.loc[i, "Time_diff"]
2
  • The python if requires one true/false value, not multple. It doesn't evaluate once for each row of the Series. Commented Nov 3, 2019 at 12:03
  • To address your question on the solution by Yash Sharma, the code you shared works just fine a test DataFrame I made. What line specifically is causing the error? It must have something to do with your DataFrame and the variable i. Commented Nov 3, 2019 at 22:09

3 Answers 3

1

Try this

df_da["Time_diff"] = (df_da["Time_diff"] < 0)*(24*60) + df_da["Time_diff"]

This would basically add 24 * 60 to all rows that return True for the inequality. This will not involve a for loop outside, which I'm assuming there is.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, buddy :) It worked. But can you explain why my code didn't work?
0

Similar to Yash Sharma's answer.

df_da['Time_diff'] += 24*60 if df_da['Time_diff'] < 0 else 0

3 Comments

This code throws the same error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
@shravan I'm unsure about the code in your post, but here the cause of the error is quite obvious. df_da['Time_diff'] < 0 is a Series, if takes a single boolean value. The error message The truth value of a Series is ambiguous means "I don't know how to judge whether a whole Series should be considered True or False", which makes sense.
but the funny thing is when I reused the same code in another occasion for the same scenario it worked. That is why the confusion began..if it had not worked in both the cases I would have accepted the above reason :)
0

I can't find the source of the error, it would probably require the code for your DataFrame and for i.

The currently accepted answer relies on coercing booleans to numbers, and the fact that x * 0 = x. It does not work for most cases and isn't particularly easy to understand.

Here is how I would do this:

neg_time_diff_idx = df_da['Time_diff'] < 0
df_da.loc[neg_time_diff_idx, 'Time_diff'] = df_da.loc[neg_time_diff_idx, 'Time_diff'] + (24 * 60) 

Which can be simplified to:

neg_time_diff_idx = df_da['Time_diff'] < 0
df_da.loc[neg_time_diff_idx, 'Time_diff'] += 24 * 60

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.