Skip to content

Arithmetic operation between Series and bool fails #7011

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

Closed
fonnesbeck opened this issue Apr 30, 2014 · 3 comments · Fixed by #7015
Closed

Arithmetic operation between Series and bool fails #7011

fonnesbeck opened this issue Apr 30, 2014 · 3 comments · Fixed by #7015
Labels
Dtype Conversions Unexpected or buggy dtype conversions Error Reporting Incorrect or improved errors from pandas
Milestone

Comments

@fonnesbeck
Copy link

In the current master, I am no longer able to add or subtract bool scalars from Series. For example:

In [63]: (covariates.isnull().sum(axis=1).astype(bool))
Out[63]:
case_id
A0001      False
A0002      False
A0003      False
A0004      False
A0005      False
A0006      False
A0007      False
A0008      False
A0009      False
A0010      False
A0011      False
A0012      False
A0013      False
A0014      False
A0015      False
...
D3760       True
D3761      False
D3762      False
D3763      False
D3764      False
D3765      False
D3766      False
D3767      False
D3768      False
D3769      False
D3770      False
D3771      False
D3772      False
D3773      False
D3774      False
Length: 3169, dtype: bool

In [65]: (covariates.isnull().sum(axis=1).astype(bool)).value_counts()
Out[65]:
False    2677
True      492
dtype: int64

When I subtract True to flip the trues and falses, I get:

In [66]: (covariates.isnull().sum(axis=1).astype(bool)) - True
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-66-6db12b7f95bf> in <module>()
----> 1 (covariates.isnull().sum(axis=1).astype(bool)) - True

/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas-0.13.1_780_gdc098d8-py2.7-macosx-10.9-x86_64.egg/pandas/core/ops.pyc in wrapper(left, right, name)
    501             if hasattr(lvalues, 'values'):
    502                 lvalues = lvalues.values
--> 503             return left._constructor(wrap_results(na_op(lvalues, rvalues)),
    504                                      index=left.index, name=left.name,
    505                                      dtype=dtype)

/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas-0.13.1_780_gdc098d8-py2.7-macosx-10.9-x86_64.egg/pandas/core/ops.pyc in na_op(x, y)
    441         try:
    442             result = expressions.evaluate(op, str_rep, x, y,
--> 443                                           raise_on_error=True, **eval_kwargs)
    444         except TypeError:
    445             if isinstance(y, (pa.Array, pd.Series)):

/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas-0.13.1_780_gdc098d8-py2.7-macosx-10.9-x86_64.egg/pandas/computation/expressions.pyc in evaluate(op, op_str, a, b, raise_on_error, use_numexpr, **eval_kwargs)
    185         use_numexpr : whether to try to use numexpr (default True)
    186         """
--> 187     _bool_arith_check(op_str, a, b)
    188     if use_numexpr:
    189         return _evaluate(op, op_str, a, b, raise_on_error=raise_on_error,

/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas-0.13.1_780_gdc098d8-py2.7-macosx-10.9-x86_64.egg/pandas/computation/expressions.pyc in _bool_arith_check(op_str, a, b, not_allowed)
    164 def _bool_arith_check(op_str, a, b, not_allowed=frozenset(('+', '*', '-', '/',
    165                                                            '//', '**'))):
--> 166     if op_str in not_allowed and _has_bool_dtype(a) and _has_bool_dtype(b):
    167         raise NotImplementedError("operator %r not implemented for bool "
    168                                   "dtypes" % op_str)

/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas-0.13.1_780_gdc098d8-py2.7-macosx-10.9-x86_64.egg/pandas/computation/expressions.pyc in _has_bool_dtype(x)
    159         return x.dtype == bool
    160     except AttributeError:
--> 161         return 'bool' in x.blocks
    162 
    163 

AttributeError: 'bool' object has no attribute 'blocks'

Running current master on Python 2.7.5 (OS X 10.9.2).

@fonnesbeck fonnesbeck changed the title Arithmetic operations between Series and bool fails Arithmetic operation between Series and bool fails Apr 30, 2014
@jreback
Copy link
Contributor

jreback commented Apr 30, 2014

should be a better error message, but just do this

In [6]: s = Series([True,False,True])

In [7]: s ^ True
Out[7]: 
0    False
1     True
2    False
dtype: bool

In [8]: s & True
Out[8]: 
0     True
1    False
2     True
dtype: bool

In [12]: ~s
Out[12]: 
0    False
1     True
2    False
dtype: bool

@jreback
Copy link
Contributor

jreback commented Apr 30, 2014

@cpcloud looks like trivial fix in pandas/computation/expressions/_has_bool_dtype

@jreback jreback modified the milestones: 0.14.1, 0.14.0 Apr 30, 2014
@cpcloud cpcloud self-assigned this Apr 30, 2014
@cpcloud
Copy link
Member

cpcloud commented Apr 30, 2014

pr on the way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants