-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
GH 9016: Bitwise operation weirdness #9338
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
Conversation
This PR intends to resolve #9016 |
other = other.reindex_like(self).fillna(False).astype(bool) | ||
return self._constructor(na_op(self.values, other.values), | ||
if other.dtype.kind not in ['u', 'i']: | ||
other = other.reindex_like(self).fillna(False).astype(bool) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the other.reindex_like(self)
bit is repeated twice here -- can you consolidate it?
can u move the tests near the other boolean tests (right below) in test_series also need some test for ops with NaN and series containing nan and ops with other dtypes |
@shoyer, @jreback thanks, I am working on the suggested changes. In the meantime I noticed this. (using master without this patch).
I was expecting output to be
In the Series example, should the behavior be preserved? In that case should the output dtype be bool or int64? Otherwise it looks confusing. |
Looks like if changed, |
made the following changes:
|
@@ -107,6 +107,8 @@ Enhancements | |||
|
|||
- ``Timedelta`` will now accept nanoseconds keyword in constructor (:issue:`9273`) | |||
|
|||
- ``Series`` now supports bitwise operation for integral types (:issue:`9016`) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a mini-example here
@tvyomkesh this seems like a lot of branching for just slightly different dtype handling. see what you can do about that. |
@jreback thanks for pointing to I still think this is confusing.
Either both the inputs should be converted to dtype boolean in which case output value should be True or if both the inputs are converted to dtype int64 implictly and bitwise & is carried out per current behavior, then the output dtype should remain int64 and not be coerced to bool. The current patch can be modified to fix this. Thoughts? |
PR review changes are done. Good to merge? |
is_other_int_dtype = com.is_integer_dtype(other.dtype) | ||
|
||
if is_other_int_dtype: | ||
other = other.fillna(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can collapse a lot of this if you define functions like this:
fill_bool = lambda x: x.fillna(False).astype(bool)
fill_int = lambda x: x.fillna(0)
filler = fill_int if is_self_int_dtype and is_other_int_dtype else fill_bool
other = filler(other)
res = filler(self._constructor(.......))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@jreback thanks. I have made the changes. |
|
||
- ``Series`` now supports bitwise operation for integral types (:issue:`9016`) | ||
.. code-block:: python | ||
In [2]: pd.Series([0,1,2,3], list('abcd')) | pd.Series([4,4,4,4], list('abcd')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indent 2 spaces here (the code under the directive), otherwise won't render correctly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh didn't notice this one. thanks.
@@ -117,6 +117,21 @@ Enhancements | |||
- Added ``StringMethods.ljust()`` and ``rjust()`` which behave as the same as standard ``str`` (:issue:`9352`) | |||
- ``StringMethods.pad()`` and ``center()`` now accept `fillchar` option to specify filling character (:issue:`9352`) | |||
|
|||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok move this to the api section snd show s code block of the existing behavior (and then put this after)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
Series now supports bitwise op for integral types
GH 9016: Bitwise operation weirdness
thanks @tvyomkesh |
Series now supports bitwise op for integral types.
I have made the changes in wrapper() itself instead of na_op() because it looked to me like wrapper is controlling the input and output fill and dtype. Once that is taken care of, na_op() seems to be doing the right thing by itself and so I did not have to change anything to get back the expected behavior. Happy to change if things need to be altered toward better design etc.