DOC: Fixed timedelta docstring and examples#23259
DOC: Fixed timedelta docstring and examples#23259datapythonista merged 8 commits intopandas-dev:masterfrom
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
| 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.
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.
@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.
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.
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.
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.
Can you mention they are absolute differences in time.
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.
I'd remove this example as well; it demonstrates assignment more than to_timedelta
There was a problem hiding this comment.
Do you mean the box example or the one below it ?
There was a problem hiding this comment.
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 |
datapythonista
left a comment
There was a problem hiding this comment.
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 |
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.
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.
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 |
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.
Use a list, it'll render better. Ignore the validation errors.
There was a problem hiding this comment.
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. |
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/core/tools/timedeltas.py
Outdated
|
|
||
| See also | ||
| -------- | ||
| pandas.DataFrame.astype : Cast argument to a specified dtype. |
There was a problem hiding this comment.
remove the pandas. prefix
|
@datapythonista Thanks for the review. Added changes. Also rendered the docstring below: |
datapythonista
left a comment
There was a problem hiding this comment.
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.
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.
I'd add spaces after the commas.
pandas/core/tools/timedeltas.py
Outdated
|
|
||
| Returns | ||
| ------- | ||
| ret : timedelta64/arrays of timedelta64 if parsing succeeded |
There was a problem hiding this comment.
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
| 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 |
|
@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
| 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.
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.
datapythonista
left a comment
There was a problem hiding this comment.
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.
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.
I'd replace list, tuple, 1-d array by list-like. I'd also remove the comma vefore the or.
|
@datapythonista done :) |
|
@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. |
datapythonista
left a comment
There was a problem hiding this comment.
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 |



git diff upstream/master --name-only -- "*.py" | grep "pandas/" | xargs flake8python scripts/validate_docstrings.py pandas.to_timedelta