Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR: invalid error reporting when comparing vs. None #12689

Closed
ghost opened this issue Mar 22, 2016 · 3 comments · Fixed by #30327
Closed

ERR: invalid error reporting when comparing vs. None #12689

ghost opened this issue Mar 22, 2016 · 3 comments · Fixed by #30327
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@ghost
Copy link

ghost commented Mar 22, 2016

The "in" function does not work as expected in regard to a data frame. Let's say we have the following data frame :

df = pd.DataFrame(np.random.randn(6,4), columns=list('ABCD'))

We can test this data frame :

df is None
False

However we cannot test its presence in a list :

df in [None]
TypeError: Could not compare [None] with block values

We would expect to get just False in this case.

(Pandas version 0.17.1)

@jreback
Copy link
Contributor

jreback commented Mar 22, 2016

This is coming from this, while useful (should simply be using df.isnull()) to handle all dtypes.

In [3]: df==None
TypeError: Could not compare [None] with block values

is a bug and causes __nonzero__ to fail I think.

This is an inherently ambiguous operation, see docs here

In [22]: df['A'] in [None]
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

@jreback jreback added Bug Error Reporting Incorrect or improved errors from pandas Difficulty Intermediate labels Mar 22, 2016
@jreback jreback added this to the 0.18.1 milestone Mar 22, 2016
@jreback jreback changed the title Find data frame in a list ERR: invalid error reporting when comparing vs. None Mar 22, 2016
@jreback jreback modified the milestones: 0.18.1, 0.18.2 Apr 26, 2016
@jorisvandenbossche jorisvandenbossche modified the milestones: Next Major Release, 0.19.0 Aug 21, 2016
@mroeschke
Copy link
Member

This is raising the correct exception on master. Could use a test.

In [33]: df = pd.DataFrame(np.random.randn(6,4), columns=list('ABCD'))
    ...:
    ...:

In [34]: df in [None]
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

In [35]: pd.__version__
Out[35]: '0.26.0.dev0+684.g953757a3e'

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Error Reporting Incorrect or improved errors from pandas labels Oct 27, 2019
jbrockmendel added a commit to jbrockmendel/pandas that referenced this issue Dec 18, 2019
@jreback jreback modified the milestones: Contributions Welcome, 1.0 Dec 20, 2019
@eddy-geek
Copy link

eddy-geek commented Apr 16, 2020

This seems to have unintended side-effects -- i.e. why should this seemingly innocuous code raise ValueError? (this is a from a real use-case around providing default values for a dataframe of plot text annotations):

df_or_string = pd.DataFrame()
if df_or_string == 'foo':
    print('bar')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants