-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
Bug: adds support for unary plus #19297
Conversation
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.
can you add a similar set of tests for Series?
doc/source/whatsnew/v0.23.0.txt
Outdated
@@ -273,6 +273,7 @@ Build Changes | |||
Other API Changes | |||
^^^^^^^^^^^^^^^^^ | |||
|
|||
- Unary ``+`` operator now permitted for ``Series`` and ``DataFrame`` (:issue:`16073`) |
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.
move to Other Enhancements, add the word numeric operations at the end.
pandas/core/generic.py
Outdated
|
||
def __pos__(self): | ||
values = _values_from_object(self) | ||
if values.dtype == np.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.
is_bool_dtype (and can you change in __neg__
as well
pandas/tests/frame/test_operators.py
Outdated
@@ -389,12 +389,42 @@ def test_logical_with_nas(self): | |||
assert_series_equal(result, expected) | |||
|
|||
def test_neg(self): | |||
# what to do? | |||
assert_frame_equal(-self.frame, -1 * self.frame) | |||
numeric = pd.DataFrame({ |
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.
can you parameterize this
pandas/tests/frame/test_operators.py
Outdated
|
||
def test_invert(self): | ||
assert_frame_equal(-(self.frame < 0), ~(self.frame < 0)) | ||
|
||
def test_pos(self): |
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.
same
pandas/tests/frame/test_operators.py
Outdated
assert_frame_equal(+numeric, +1 * numeric) | ||
assert_frame_equal(+boolean, (+1 * boolean).astype(bool)) | ||
assert_series_equal(+timedelta, pd.to_timedelta(+1 * timedelta)) | ||
with pytest.raises(TypeError): |
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.
move the TypeError check (e.g. passing in invalid dtypes) to a separate test, and parameterize with all the invalid dtypes.
From the CI logs, it looks like the new operator trips some of the computational tests for other dtypes. I can clean these up, but would like to ask for some guidance on how we would like to handle operations on the timeperiod dtypes. |
Hello @deniederhut! Thanks for updating the PR. Cheers ! There are no PEP8 issues in this Pull Request. 🍻 Comment last updated on February 08, 2018 at 01:18 Hours UTC |
@jbrockmendel has been working a lot on the ops recently. what cases are in question? can you show (alternativley, you can xfail them and then point them out in your diff). |
tm.assert_series_equal(s - p, -exp) Do we want to allow these?
|
c61a108
to
047f62e
Compare
@jreback would it be better to defer the decision about TimePeriods, and xfail the problematic tests for now? |
yes, ok with fixing Timedelta / periods after. pls put in the appopriate xfails. and rebase. |
7145fe4
to
3d0844a
Compare
@jreback It's not clear to me why the Travis build is failing -- there are some warnings about IPython versions and SparseBlocks, but no failed tests. Is there something obvious that I am missing? |
you are failing linting see the very end |
🤦♀️ yup there it is. Thanks! |
3d0844a
to
1bcd080
Compare
Codecov Report
@@ Coverage Diff @@
## master #19297 +/- ##
==========================================
+ Coverage 91.62% 91.62% +<.01%
==========================================
Files 150 150
Lines 48773 48790 +17
==========================================
+ Hits 44687 44704 +17
Misses 4086 4086
Continue to review full report at Codecov.
|
@@ -111,6 +111,8 @@ def test_df_radd_str(self): | |||
|
|||
class TestPeriodFrameArithmetic(object): | |||
|
|||
@pytest.mark.xfail(reason="Unary negative is currently not permitted " |
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.
@deniederhut so period - period is now going to raise? this is #13077.
I am not actually sure what we should do here. I think we can re-enable these though, at least for now.
(you can use is_period_arraylike
to detect this)
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 test is marked xfail because of the -exp
in this assertion tm.assert_frame_equal(df - p, -exp)
, not the initial subtraction. Happy to allow it and unmark the tests.
Should the expected return values in the tests be scalars? I think period
- period
currently returns an integer.
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.
i c
the return value should be integers
Adds special method for unary plus to ndframe generics, and restricts the allowed dtypes for the unary negative. Closes pandas-dev#16073.
1bcd080
to
5203027
Compare
5203027
to
2b8072e
Compare
pd.DataFrame({'a': pd.Series(pd.to_timedelta([1, -1]))})) | ||
]) | ||
def test_neg_numeric(self, df, expected): | ||
assert_frame_equal(-df, expected) |
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.
@jbrockmendel I think on your list is to rationalize locations of arithmetic tests between test_arithmetic and test_operations
thanks @deniederhut nice patch! sometimes little issues can morph, nice job! |
Thanks so much for your help @jreback ! |
git diff upstream/master -u -- "*.py" | flake8 --diff
Adds missing unary plus operator from #16073. Adds typechecking behavior to unary neg, along with tests for both.