Skip to content

DOC: Improve the docstring of String.str.zfill() #20864

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 8 commits into from
Jul 7, 2018
Merged

DOC: Improve the docstring of String.str.zfill() #20864

merged 8 commits into from
Jul 7, 2018

Conversation

alkamid
Copy link
Contributor

@alkamid alkamid commented Apr 29, 2018

  • closes #xxxx
  • tests added / passed
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

Result of the Pandas sprint at PyData London 2018. Includes work by @owlas.

@owlas owlas mentioned this pull request Apr 29, 2018
4 tasks
@codecov
Copy link

codecov bot commented Apr 29, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@876e568). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##             master   #20864   +/-   ##
=========================================
  Coverage          ?   91.81%           
=========================================
  Files             ?      153           
  Lines             ?    49478           
  Branches          ?        0           
=========================================
  Hits              ?    45429           
  Misses            ?     4049           
  Partials          ?        0
Flag Coverage Δ
#multiple 90.21% <ø> (?)
#single 41.85% <ø> (?)
Impacted Files Coverage Δ
pandas/core/strings.py 98.63% <ø> (ø)

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 876e568...706688f. Read the comment docs.

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

Great job, added some comments with typos and ideas.

Equivalent to :meth:`str.zfill`.
Pad strings in the Series/Index by prepending '0' characters.

Strings in the Series/Index are padded with prepending '0' characeters
Copy link
Member

Choose a reason for hiding this comment

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

typo in characters

Strings in the Series/Index are padded with prepending '0' characeters
(i.e. on the left of the string) to reach a total string length
of `width`. in the Series/Index with length greater than `width`
are unchanged.
Copy link
Member

Choose a reason for hiding this comment

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

I think something went wrong with that last sentence, two spaces, not starting with a capital letter, and I think it's not that clear, a word or something is missing I guess.

are unchanged.

Note: Differs from :meth:`str.zfill` which has special handling
for '+'/'-' in the string.
Copy link
Member

Choose a reason for hiding this comment

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

There is a Notes section in the numpy docstring convention that we can use for this:

Notes
-------

https://python-sprints.github.io/pandas/guide/pandas_docstring.html#section-6-notes

Minimum width of resulting string; additional characters will be
filled with 0
Minimum length of resulting string; strings with length less
than `width` be prepended with '0' characters.

Returns
-------
filled : Series/Index of objects
Copy link
Member

Choose a reason for hiding this comment

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

you can get rid of the filled and just leave the type (it was usual to name the return, but it was decided to not name them anymore as it doesn't add much value).

"""

Copy link
Member

Choose a reason for hiding this comment

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

no need to add this blank line here

>>> s.str.zfill(5)
0 000-2
1 00005
dtype: object
Copy link
Member

Choose a reason for hiding this comment

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

the example looks cool, but I think we can make it a bit more compact and clear by:

  • Just creating a single Series (I'd get rid of one of 10 or 127 as they show the same user case, and I'd add a NaN and a number (as number, not as string).
  • After creating the Series I'd show it without modification (i.e. >>> s). This makes the result easier to compare with the original data.
  • We can get rid of the second example if we add the numbers in the first.

@pep8speaks
Copy link

pep8speaks commented Apr 30, 2018

Hello @alkamid! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on July 07, 2018 at 14:20 Hours UTC

@alkamid
Copy link
Contributor Author

alkamid commented Apr 30, 2018

@datapythonista thanks a lot for your valuable feedback! I've addressed all your comments, does it look better to you now?

4 127
5 NaN
dtype: object
>>> s.str.zfill(5)
Copy link
Member

Choose a reason for hiding this comment

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

Looks great now. Just couple of ideas, feel free to ignore them if you prefer the way it is now.

I think it could be useful to add a short paragraph before this line explaining that the non-string values (127 and NaN) become NaN, and may be reemphasizing what you already have in the body of the docstring, that the values with sign (-2 and +5) get the zeros in the left of the sign, and that 423523 keeps unchanged as it's longer than width.

I also think that it would make things easier/faster to understand if the values look a bit less arbitrary. For example ['-1', '1', 10, np.nan, '1000'] (with a width of 3 in this example).

"""

Copy link
Member

Choose a reason for hiding this comment

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

No need to add this blank line.

@alkamid
Copy link
Contributor Author

alkamid commented May 2, 2018

@datapythonista thanks again, suggestions incorporated into PR. Let me know what you think now.

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

lgtm

@jreback jreback added Docs Strings String extension data type and string data labels May 4, 2018
@TomAugspurger TomAugspurger added this to the 0.24.0 milestone Jul 7, 2018
@TomAugspurger TomAugspurger merged commit 3344918 into pandas-dev:master Jul 7, 2018
@alkamid alkamid deleted the docstring_series_str_zfill branch July 7, 2018 14:59
Sup3rGeo pushed a commit to Sup3rGeo/pandas that referenced this pull request Oct 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants