Skip to content

Commit 928f7e4

Browse files
committed
Modified timedelta docstring to include M and Y units and examples
1 parent a08bf3d commit 928f7e4

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

pandas/core/tools/timedeltas.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,31 @@
1616

1717
def to_timedelta(arg, unit='ns', box=True, errors='raise'):
1818
"""
19-
Convert argument to timedelta
19+
Convert argument to timedelta.
20+
21+
Timedeltas are differences in times, expressed in difference units,
22+
for example, years, momths, days, hours, minutes, seconds.
23+
They can be both positive and negative. This method can create Timedelta
24+
objects from pandas objects.
2025
2126
Parameters
2227
----------
2328
arg : string, timedelta, list, tuple, 1-d array, or Series
24-
unit : str, optional
25-
Denote the unit of the input, if input is an integer. Default 'ns'.
29+
unit : str, default 'ns'
2630
Possible values:
2731
{'Y', 'M', 'W', 'D', 'days', 'day', 'hours', hour', 'hr', 'h',
2832
'm', 'minute', 'min', 'minutes', 'T', 'S', 'seconds', 'sec', 'second',
2933
'ms', 'milliseconds', 'millisecond', 'milli', 'millis', 'L',
3034
'us', 'microseconds', 'microsecond', 'micro', 'micros', 'U',
3135
'ns', 'nanoseconds', 'nano', 'nanos', 'nanosecond', 'N'}
32-
box : boolean, default True
33-
- If True returns a Timedelta/TimedeltaIndex of the results
34-
- if False returns a np.timedelta64 or ndarray of values of dtype
35-
timedelta64[ns]
36+
box : Boolean, default True
37+
If True returns a Timedelta/TimedeltaIndex of the results.
38+
if False returns a np.timedelta64 or ndarray of values of dtype
39+
timedelta64[ns].
3640
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
37-
- If 'raise', then invalid parsing will raise an exception
38-
- If 'coerce', then invalid parsing will be set as NaT
39-
- If 'ignore', then invalid parsing will return the input
41+
If 'raise', then invalid parsing will raise an exception.
42+
If 'coerce', then invalid parsing will be set as NaT.
43+
If 'ignore', then invalid parsing will return the input.
4044
4145
Returns
4246
-------
@@ -68,6 +72,31 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'):
6872
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
6973
dtype='timedelta64[ns]', freq=None)
7074
75+
For `M` and `Y` units, `1M = 30D` and 1Y = 365D:
76+
77+
>>> pd.to_timedelta(np.arange(5), unit='M')
78+
TimedeltaIndex([ '0 days 00:00:00', '30 days 10:29:06',
79+
'60 days 20:58:12', '91 days 07:27:18',
80+
'121 days 17:56:24'],
81+
dtype='timedelta64[ns]', freq=None)
82+
>>> pd.to_timedelta(np.arange(5), unit='Y')
83+
TimedeltaIndex([ '0 days 00:00:00', '365 days 05:49:12',
84+
'730 days 11:38:24', '1095 days 17:27:36',
85+
'1460 days 23:16:48'],
86+
dtype='timedelta64[ns]', freq=None)
87+
88+
Add new column of dates from existing dates in a `DataFrame`
89+
using `timedelta`
90+
91+
>>> Dates = pd.to_datetime(['26/10/2018','28/10/2018', '2/11/2018'])
92+
>>> df = pd.DataFrame({'Start': Dates,'Days':[5, 10, 5]})
93+
>>> df['End'] = df['Start'] + pd.to_timedelta(df['Days'], unit='d')
94+
>>> df
95+
Start Days End
96+
0 2018-10-26 5 2018-10-31
97+
1 2018-10-28 10 2018-11-07
98+
2 2018-02-11 5 2018-02-16
99+
71100
See also
72101
--------
73102
pandas.DataFrame.astype : Cast argument to a specified dtype.

0 commit comments

Comments
 (0)