Skip to content

DOC: Update the pandas.DataFrame.abs docstring #20194

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

Conversation

myles
Copy link
Contributor

@myles myles commented Mar 10, 2018

Improve description and returns, and added notes and examples.

Checklist for the pandas documentation sprint (ignore this if you are doing
an unrelated PR):

  • PR title is "DOC: update the docstring"
  • The validation script passes: scripts/validate_docstrings.py <your-function-or-method>
  • The PEP8 style check passes: git diff upstream/master -u -- "*.py" | flake8 --diff
  • The html version looks good: python doc/make.py --single <your-function-or-method>
  • It has been proofread on language by another sprint participant

Please include the output of the validation script below between the "```" ticks:

################################################################################
####################### Docstring (pandas.DataFrame.abs) #######################
################################################################################

Return a Series/DataFrame with absolute numeric value of each element.

This function only applies to elements that are all numeric.

Returns
-------
abs
    Series/DataFrame containing the absolute value of each element.

Notes
-----
For ``complex`` inputs, ``1.2 + 1j``, the absolute value is
:math:`\sqrt{ a^2 + b^2 }`. See the Python

Examples
--------
Absolute numeric values in a ``Series``.

>>> s = pd.Series([-1.10, 2, -3.33, 4])
>>> s.abs()
0    1.10
1    2.00
2    3.33
3    4.00
dtype: float64

Absolute numeric values in a ``Series`` with ``complex`` numbers.

>>> s = pd.Series([1.2 + 1j])
>>> s.abs()
0    1.56205
dtype: float64

Select rows with data closest to certian value using argsort (from
`StackOverflow <https://stackoverflow.com/a/17758115>`__).

>>> df = pd.DataFrame({
...     'a': [4, 5, 6, 7],
...     'b': [10, 20, 30, 40],
...     'c': [100, 50, -30, -50]
... })
>>> df
     a    b    c
0    4   10  100
1    5   20   50
2    6   30  -30
3    7   40  -50
>>> df.loc[(df.c - 43).abs().argsort()]
     a    b    c
1    5   20   50
0    4   10  100
2    6   30  -30
3    7   40  -50

See Also
--------
numpy.absolute : calculate the absolute value element-wise.

################################################################################
################################## Validation ##################################
################################################################################

Docstring for "pandas.DataFrame.abs" correct. :)

I feel there aren't any similar methods for the See Also section.

myles added 2 commits March 10, 2018 11:06
@TomAugspurger TomAugspurger added this to the 0.23.0 milestone Mar 10, 2018

Select rows with data closest to certian value using argsort (from
`StackOverflow
<http://stackoverflow.com/questions/17758023/return-rows-in-a-dataframe-closest-to-a-user-defined-number>`__).
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably too long of a line. SO should have a "share" URL that is shorter.

@@ -7115,12 +7115,61 @@ def _tz_localize(ax, tz, ambiguous):
# Numeric Methods
def abs(self):
"""
Return an object with absolute value taken--only applicable to objects
that are all numeric.
Return a Series/DataFrame with absolute numeric value of each object.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you repalce "object" with "element"? That's our usual noun for items in a frame.

that are all numeric.
Return a Series/DataFrame with absolute numeric value of each object.

This function only applies to objects that are all numeric.
Copy link
Contributor

Choose a reason for hiding this comment

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

objects -> elements

2 6 30 -30
3 7 40 -50
>>> a_value = 43.0
>>> df.loc[(df.c - a_value).abs().argsort()]
Copy link
Contributor

@TomAugspurger TomAugspurger Mar 10, 2018

Choose a reason for hiding this comment

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

No reason to assign a_value I think? Just subtract 43? I also don't know if the argsort helps. It seems a bit complicated.

Copy link
Contributor Author

@myles myles Mar 10, 2018

Choose a reason for hiding this comment

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

The example fails if argsort() is removed. Maybe it's not a good idea to provided that example. I thought it was a good idea because abs() is commonly used with other functions.

@jreback jreback added the Numeric Operations Arithmetic, Comparison, and Logical operations label Mar 10, 2018
@jreback jreback removed this from the 0.23.0 milestone Mar 10, 2018
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.

needs to merge in master. you have a lot of other commits here.

@myles myles force-pushed the doc-update-the-pandas-DataFrame-abs branch from 6e2badf to 4165872 Compare March 10, 2018 17:17
@myles
Copy link
Contributor Author

myles commented Mar 10, 2018

@jreback sorry I was playing around with Git. I fixed the issue.

Notes
-----
For ``complex`` inputs, ``1.2 + 1j``, the absolute value is
:math:`\\sqrt{ a^2 + b^2 }`. See the Python
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like something missing from the end 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.

Sorry I was going to referer to the Python documentation on complex inputs but then realised that the user would already know about them.


Examples
--------
Absolute numeric values in a ``Series``.
Copy link
Contributor

Choose a reason for hiding this comment

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

no need for the quotes on Series

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

3 4.00
dtype: float64

Absolute numeric values in a ``Series`` with ``complex`` numbers.
Copy link
Contributor

Choose a reason for hiding this comment

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

could add an example for a Series of timedelta64

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 a pd.Timedelta example.

@codecov
Copy link

codecov bot commented Mar 11, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@5b0caf4). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master   #20194   +/-   ##
=========================================
  Coverage          ?   91.72%           
=========================================
  Files             ?      150           
  Lines             ?    49162           
  Branches          ?        0           
=========================================
  Hits              ?    45096           
  Misses            ?     4066           
  Partials          ?        0
Flag Coverage Δ
#multiple 90.11% <100%> (?)
#single 41.86% <0%> (?)
Impacted Files Coverage Δ
pandas/core/generic.py 95.84% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5b0caf4...5e83e23. Read the comment docs.

@TomAugspurger TomAugspurger added this to the 0.23.0 milestone Mar 13, 2018
@TomAugspurger
Copy link
Contributor

Thanks @myles

@TomAugspurger TomAugspurger merged commit 823b5d3 into pandas-dev:master Mar 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants