-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DOC: Fixed timedelta docstring and examples #23259
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
DOC: Fixed timedelta docstring and examples #23259
Conversation
Hello @ryankarlos! Thanks for updating the PR.
Comment last updated on October 27, 2018 at 13:50 Hours UTC |
I've added in a few examples to illustrate 'M' and 'Y' outputs along with generating new columns in a dataframe using timedelta. Also corrected some linting issues and added a better summary. Any feedback would be welcome. |
Travis failing in feather --- ive seen other PRs having the same problem - not sure why it is ? |
pandas/core/tools/timedeltas.py
Outdated
@@ -64,6 +69,31 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'): | |||
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'], | |||
dtype='timedelta64[ns]', freq=None) | |||
|
|||
For `M` and `Y` units, `1M = 30D` and 1Y = 365D: |
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.
So there's an issue to deprecate 'M'
and 'Y'
for Timedelta, so I don't think it's a good idea to advertise these argument that are slated for deprecation.
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.
@mroeschke sorry wasn't aware about the issue - ill remove it. Im thinking giving example for all the other units would be overkill - maybe a couple more ?
Maybe a reference to DateOffset in the description or examples would help ?
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.
If you're up for it, you could address #16344 first and then add a note here saying "Day frequencies and higher are only supported.
Otherwise, I think this docstring has a good number of already examples, but demonstrating box
would be a nice to have.
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 sent an initial PR for #16344 , still have work to do as its my first time fixing deprecation issues before.
What is the protocol for displaying deprecation warnings within docstrings - does it go under summary section or within a separate Notes section ? I guess it would make more sense to have a line under 'units' stating M an Y are not supported and then reference to pd.DateOffset or similar for M and Y ?
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.
You can mention the deprecated parameters in the Notes section
pandas/core/tools/timedeltas.py
Outdated
Convert argument to timedelta | ||
Convert argument to timedelta. | ||
|
||
Timedeltas are differences in times, expressed in difference units |
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 mention they are absolute differences in time.
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.
Fixed
f112c2e
to
88ff298
Compare
pandas/core/tools/timedeltas.py
Outdated
>>> pd.to_timedelta(np.arange(5), box=False) | ||
array([0, 1, 2, 3, 4], dtype='timedelta64[ns]') | ||
|
||
Add new column of dates from existing dates in a `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.
I'd remove this example as well; it demonstrates assignment more than to_timedelta
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.
Do you mean the box
example or the one below it ?
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 one below it demonstrating DataFrame column assignment
Codecov Report
@@ Coverage Diff @@
## master #23259 +/- ##
=======================================
Coverage 92.28% 92.28%
=======================================
Files 161 161
Lines 51451 51451
=======================================
Hits 47483 47483
Misses 3968 3968
Continue to review full report at Codecov.
|
cc @datapythonista if you could have a look. |
@mroeschke I haven't added the notes section for deprecated units yet - i was working concurrently on #23264 so wasn't sure if i should add that in before finishing that |
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.
Thanks for the work on this. Added some comments about the formatting.
pandas/core/tools/timedeltas.py
Outdated
- If True returns a Timedelta/TimedeltaIndex of the results | ||
- if False returns a np.timedelta64 or ndarray of values of dtype | ||
timedelta64[ns] | ||
arg : String, timedelta, list, tuple, 1-d array, or Series |
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.
str instead of String
pandas/core/tools/timedeltas.py
Outdated
timedelta64[ns] | ||
arg : String, timedelta, list, tuple, 1-d array, or Series | ||
The argument which needs to be converted to timedelta. | ||
unit : Integer or float, default ns |
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.
int instead of Integer. But doesn't make sense, seemsl like it should be a string.
Quotes around 'ns'
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.
Oh Sorry yes - my bad - thanks for spotting this.
pandas/core/tools/timedeltas.py
Outdated
The argument which needs to be converted to timedelta. | ||
unit : Integer or float, default ns | ||
Denotes the unit (D,h,m,s,ms,us,ns) of the arg. | ||
box : Boolean, default True |
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.
bool instead of Boolean
pandas/core/tools/timedeltas.py
Outdated
Denotes the unit (D,h,m,s,ms,us,ns) of the arg. | ||
box : Boolean, default True | ||
If True returns a Timedelta/TimedeltaIndex of the results. | ||
if False returns a np.timedelta64 or ndarray of values of 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.
Use a list, it'll render better. Ignore the validation errors.
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.
Thanks, thats why i removed them below to get rid of the validation errors - will include them.
pandas/core/tools/timedeltas.py
Outdated
- If 'ignore', then invalid parsing will return the input | ||
If 'raise', then invalid parsing will raise an exception. | ||
If 'coerce', then invalid parsing will be set as NaT. | ||
If 'ignore', then invalid parsing will return the input. |
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.
Keep the list.
pandas/core/tools/timedeltas.py
Outdated
|
||
>>> pd.to_timedelta(np.arange(5), box=False) | ||
array([0, 1, 2, 3, 4], dtype='timedelta64[ns]') | ||
|
||
See also |
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.
capital A
pandas/core/tools/timedeltas.py
Outdated
|
||
>>> pd.to_timedelta(np.arange(5), box=False) | ||
array([0, 1, 2, 3, 4], dtype='timedelta64[ns]') | ||
|
||
See also | ||
-------- | ||
pandas.DataFrame.astype : Cast argument to a specified 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.
remove the pandas.
prefix
@datapythonista Thanks for the review. Added changes. Also rendered the docstring below: |
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.
looking good, added a few more comments.
pandas/core/tools/timedeltas.py
Outdated
Convert argument to timedelta. | ||
|
||
Timedeltas are absolute differences in times, expressed in difference | ||
units e.g. days, hours, minutes, seconds. This method converts an argument |
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 it'd look better to have the e.g.
in brackets.
pandas/core/tools/timedeltas.py
Outdated
arg : str, timedelta, list, tuple, 1-d array, or Series | ||
The argument which needs to be converted to timedelta. | ||
unit : str, default 'ns' | ||
Denotes the unit (D,h,m,s,ms,us,ns) of the arg. |
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'd add spaces after the commas.
pandas/core/tools/timedeltas.py
Outdated
- If 'ignore', then invalid parsing will return the input | ||
- If 'raise', then invalid parsing will raise an exception. | ||
- If 'coerce', then invalid parsing will be set as NaT. | ||
- If 'ignore', then invalid parsing will return the input. | ||
|
||
Returns | ||
------- | ||
ret : timedelta64/arrays of timedelta64 if parsing succeeded |
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.
you can get rid of the ret
, and leave here just the types timedelta64 or numpy.array of timedelta64
. We should be able to automatically parse the line with the types, so no description should go there. A description on what is being returned, and clarification on when every type is being returned should go in the next line (indented).
pandas/core/tools/timedeltas.py
Outdated
unit : str, default 'ns' | ||
Denotes the unit (D,h,m,s,ms,us,ns) of the arg. | ||
box : bool, default True | ||
- If True returns a Timedelta/TimedeltaIndex of the results. | ||
- if False returns a np.timedelta64 or ndarray of values of 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.
Capital I
@datapythonista Thanks, added an update. Im working concurrently on a related PR #23264 -- not sure if i should add the notes section on deprecated units here or in that PR ? |
pandas/core/tools/timedeltas.py
Outdated
from a recognized timedelta format / value into a Timedelta type. | ||
|
||
Parameters | ||
---------- | ||
arg : str, timedelta, list, tuple, 1-d array, or Series | ||
arg : str, timedelta, list, tuple, 1-d array, or Series |
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 missing spaces were down, in the (D,h,m,s...)
, which should be (D, h, m, s,...)
.
Just leave a space after the commas 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.
Just spotted couple of last things. If the examples are now working, looks good to me after these last fixes.
pandas/core/tools/timedeltas.py
Outdated
Convert argument to timedelta. | ||
|
||
Timedeltas are absolute differences in times, expressed in difference | ||
units e.g. (days, hours, minutes, seconds). This method converts an argument |
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'd move the e.g.
inside the parenthesis I think that makes more sense.
pandas/core/tools/timedeltas.py
Outdated
- If True returns a Timedelta/TimedeltaIndex of the results | ||
- if False returns a np.timedelta64 or ndarray of values of dtype | ||
timedelta64[ns] | ||
arg : str, timedelta, list, tuple, 1-d array, or Series |
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'd replace list, tuple, 1-d array by list-like
. I'd also remove the comma vefore the or.
@datapythonista done :) |
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.
lgtm, thanks!
@datapythonista There is a now a conflict with the most recent merge #23439 - with more additions in the units parameters. Should I revert to the version we have in this - or do you prefer to include all those units ('Y', 'M' shouldn't be in there for eg ?). Let me know and I can fix. |
Unlucky this was not merged earlier. You need to fix the conflict in a way that you get the lastest changes (from the conflicting PR in this case), and you make any fixes to that. This PR is a diff of what you have and master, if you take your changes, you'll be deleting the work in that PR here. I'm not sure of how the long set of units in that PR renders. If it doesn't render correctly, you can fix it here (instead of having the units in the type, just say |
@datapythonista ok thanks - ill fetch the latest changes from master - so is it ok for me to list the units as the short format |
Yes, if they were added that's for a reason. Avoid changing the content unless you really know what you're doing. But make the format look as good as possible. And in this case, I'm not sure how sphinx handles multilines in the types, we had problems with that in the past, so may be the current format is displayed broken. |
0c5129f
to
e6d45a7
Compare
@datapythonista Ive fixed the merge conflict to include changes from upstream. The units were not rendering properly when including in the type (the format was getting broken after the first line) so I shifted it to the description as you had suggested. |
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.
Sorry for the delay in reviewing.
Looks good, just couple of minor things, and I think it's ready to be merged.
55b095a
to
8777629
Compare
8777629
to
a67649a
Compare
@datapythonista over to you. merge when satisfied. |
Thanks for fixing this @ryankarlos |
…fixed * upstream/master: DOC: Removing rpy2 dependencies, and converting examples using it to regular code blocks (pandas-dev#23737) BUG: Fix dtype=str converts NaN to 'n' (pandas-dev#22564) DOC: update pandas.core.resample.Resampler.nearest docstring (pandas-dev#20381) REF/TST: Add more pytest idiom to parsers tests (pandas-dev#23810) Added support for Fraction and Number (PEP 3141) to pandas.api.types.is_scalar (pandas-dev#22952) DOC: Updating to_timedelta docstring (pandas-dev#23259)
git diff upstream/master --name-only -- "*.py" | grep "pandas/" | xargs flake8
python scripts/validate_docstrings.py pandas.to_timedelta