Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-pandas: the truth value of a series is ambiguous

Tags:

python

pandas

I am currently trying to compare values from a json file(on which I can already work on) to values from a csv file(which might be the issue). My current code looks like this:

for data in trades['timestamp']:
    data = pd.to_datetime(data)
    print(data)
       if data == ask_minute['lastUpdated']:
           #....'do something'

Which gives:

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

My current print(data) looks like this:

2018-10-03 18:03:38.067000
2018-10-03 18:03:38.109000
2018-10-03 18:04:28
2018-10-03 18:04:28.685000

However, I am still unable to compare these timestamps from my CSV file to those of my Json file. Does someone have an idea?

like image 546
Viktor.w Avatar asked Dec 18 '18 09:12

Viktor.w


People also ask

What is truth value of array?

ValueError: The truth value of an array with more than one element is ambiguous. If the number of elements is one, the value of the element is evaluated as a bool value. For example, if the element is an integer int , it is False if it is 0 and True otherwise.

What is value error in Python pandas?

ValueError in Python is raised when a user gives an invalid value to a function but is of a valid argument. It usually occurs in mathematical operations that will require a certain kind of value, even when the value is the correct argument. Imagine telling Python to take the square root of a negative integer.

What is a empty in Python?

Empty lists are considered False in Python, hence the bool() function would return False if the list was passed as an argument.

Are pandas null?

isnull. Detect missing values for an array-like object. This function takes a scalar or array-like object and indicates whether values are missing ( NaN in numeric arrays, None or NaN in object arrays, NaT in datetimelike).


1 Answers

Let's reduce it to a simpler example. By doing for instance the following comparison:

3 == pd.Series([3,2,4,1])

0     True
1    False
2    False
3    False
dtype: bool

The result you get is a Series of booleans, equal in size to the pd.Series in the right hand side of the expression. So really what's happening here is that the integer is being broadcast across the series, and then they are compared. So when you do:

if 3 == pd.Series([3,2,4,1]):
    pass

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

You get an error. The problem here is that you are comparing a pd.Series with a value, so you'll have multiple True and multiple False values, as in the case above. This of course is ambiguous, since the condition is neither True or False.

So you need to further aggregate the result so that a single boolean value results from the operation. For that you'll have to use either any or all depending on whether you want at least one (any) or all values to satisfy the condition.

(3 == pd.Series([3,2,4,1])).all()
# False

or

(3 == pd.Series([3,2,4,1])).any()
# True
like image 111
yatu Avatar answered Sep 17 '22 13:09

yatu