-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Add divmod to series. #14208
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
ENH: Add divmod to series. #14208
Conversation
c93a4bd
to
40ffb59
Compare
@@ -2952,3 +2952,19 @@ def __init__(self, *args, **kwargs): | |||
# Add arithmetic! | |||
ops.add_flex_arithmetic_methods(Series, **ops.series_flex_funcs) | |||
ops.add_special_arithmetic_methods(Series, **ops.series_special_funcs) | |||
|
|||
|
|||
def _construct_divmod_result(left, result, index, name, dtype): |
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.
this needs to be in ops.py
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.
just _construct_divmod_result
or the line below as well?
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.
all of the ops are defined there
40ffb59
to
b380693
Compare
Current coverage is 85.24% (diff: 100%)@@ master #14208 diff @@
==========================================
Files 140 140
Lines 50559 50567 +8
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 43099 43107 +8
Misses 7460 7460
Partials 0 0
|
I moved this into |
5a0845b
to
3ea06c5
Compare
@@ -168,6 +169,9 @@ def add_special_arithmetic_methods(cls, arith_method=None, | |||
f(op, name, str_rep, default_axis=None, fill_zeros=None, **eval_kwargs) | |||
comp_method : function, optional, | |||
factory for rich comparison - signature: f(op, name, str_rep) | |||
divmod_method : function, (optional) |
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.
why do we need a separate method here? cannot this be piggy-packed on arithmetic?
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 wasn't sure if we wanted the API for dataframes to be a return of a tuple with the div result and the mod result but for consistency it could work. Also, there is no reflected or inplace divmod. I construct this function from _arith_method_SERIES
right now but could move this into _create_methods
3ea06c5
to
ea802d5
Compare
I added |
ea802d5
to
7d792ce
Compare
ping |
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.
@jorisvandenbossche any comments
@@ -3478,6 +3478,15 @@ def _evaluate_numeric_binop(self, other): | |||
cls.__rdiv__ = _make_evaluate_binop( | |||
operator.div, '__div__', reversed=True) | |||
|
|||
def divmod_constructor(result, **attrs): | |||
return Index(result[0], **attrs), Index(result[1], **attrs) | |||
|
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 think put the function in-line (as a lambda), since they are grouped might be more clear (but if not, then go with what you have)
@@ -1328,6 +1328,9 @@ Other API Changes | |||
- ``pd.read_csv()`` in the C engine will now issue a ``ParserWarning`` or raise a ``ValueError`` when ``sep`` encoded is more than one character long (:issue:`14065`) | |||
- ``DataFrame.values`` will now return ``float64`` with a ``DataFrame`` of mixed ``int64`` and ``uint64`` dtypes, conforming to ``np.find_common_type`` (:issue:`10364`, :issue:`13917`) | |||
- ``pd.read_stata()`` can now handle some format 111 files, which are produced by SAS when generating Stata dta files (:issue:`11526`) | |||
- ``Series`` and ``Index`` now support ``divmod`` which will return a tuple of | |||
series or indices. This behaves like a standard binary operator with regards |
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.
might need a mention in the docs somewhere (maybe in binary ops section) of basics?
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.
added
7d792ce
to
08db83f
Compare
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.
minor typo, for the rest looks good!
|
||
.. ipython:: python | ||
|
||
div, rem = divmod(s, [2, 2, 3, 3, 4, 4, 5, 5, 6, 6] |
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.
missing trailing parenthesis here
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.
updated. I also built the docs locally and it seemed correct.
08db83f
to
08b3939
Compare
The clang build seemed to hang here, can we rerun it? |
Thank you for rerunning, looks to be passing now |
ping |
thanks @llllllllll |
Thank you! |
* commit 'v0.19.0rc1-25-ga7469cf': (471 commits) ENH: Add divmod to series and index. (pandas-dev#14208) Fix generator tests to run (pandas-dev#14245) BUG: GH13629 Binned groupby median function with empty bins (pandas-dev#14225) TST/TEMP: fix pyqt to 4.x for plotting tests (pandas-dev#14240) DOC: added example to Series.map showing use of na_action parameter (GH14231) DOC: split docstring into multiple lines in excel.py (pandas-dev#14073) MAINT: Use __module__ in _DeprecatedModule. (pandas-dev#14181) ENH: Allow true_values and false_values options in read_excel (pandas-dev#14002) DOC: fix incorrect example in unstack docstring (GH14206) (pandas-dev#14211) BUG: iloc fails with non lex-sorted MultiIndex pandas-dev#13797 BUG: add check for infinity in __call__ of EngFormatter In gbq.to_gbq allow the DataFrame column order to differ from schema BLD: require cython if tempita is needed DOC: add source links to api docs (pandas-dev#14200) BUG: compat with Stata ver 111 Fix: F999 dictionary key '2000q4' repeated with different values (pandas-dev#14198) BLD: Test for Python 3.5 with C locale BUG: DatetimeTZBlock can't assign values near dst boundary BUG: union_categorical with Series and cat idx BUG: fix str.contains for series containing only nan values ...
git diff upstream/master | flake8 --diff
edit: in the issue it said this is uncommon; however, I recently ran into an issue where I was doing arithmetic with fiscal quarters and using divmod seemed more natural.