Skip to content

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

Merged
merged 1 commit into from
Sep 19, 2016
Merged

Conversation

llllllllll
Copy link
Contributor

@llllllllll llllllllll commented Sep 12, 2016

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.

@@ -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):
Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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

@codecov-io
Copy link

codecov-io commented Sep 12, 2016

Current coverage is 85.24% (diff: 100%)

Merging #14208 into master will increase coverage by <.01%

@@             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          

Powered by Codecov. Last update 5e2f9da...08b3939

@llllllllll
Copy link
Contributor Author

I moved this into add_special_arithmetic_methods

@llllllllll llllllllll force-pushed the series-divmod branch 3 times, most recently from 5a0845b to 3ea06c5 Compare September 13, 2016 22:03
@jreback jreback added API Design Numeric Operations Arithmetic, Comparison, and Logical operations labels Sep 13, 2016
@@ -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)
Copy link
Contributor

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?

Copy link
Contributor Author

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

@llllllllll
Copy link
Contributor Author

I added divmod to indices and made it less special for series. I don't see a simple way to get divmod on dataframe right now though.

@llllllllll
Copy link
Contributor Author

ping

Copy link
Contributor

@jreback jreback left a 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)

Copy link
Contributor

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
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a 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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing trailing parenthesis here

Copy link
Contributor Author

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.

@llllllllll
Copy link
Contributor Author

The clang build seemed to hang here, can we rerun it?

@llllllllll
Copy link
Contributor Author

Thank you for rerunning, looks to be passing now

@llllllllll
Copy link
Contributor Author

ping

@jreback jreback added this to the 0.19.0 milestone Sep 19, 2016
@jreback jreback merged commit a7469cf into pandas-dev:master Sep 19, 2016
@jreback
Copy link
Contributor

jreback commented Sep 19, 2016

thanks @llllllllll

@llllllllll
Copy link
Contributor Author

Thank you!

@llllllllll llllllllll deleted the series-divmod branch September 19, 2016 21:09
@jreback jreback mentioned this pull request Sep 21, 2016
yarikoptic added a commit to neurodebian/pandas that referenced this pull request Oct 13, 2016
* 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
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression: divmod(my_series, some_integer) no longer works since version 0.13.
4 participants