Skip to content

Commit 072277c

Browse files
committed
Modified timedelta docstring to include M and Y units and examples
1 parent 145c227 commit 072277c

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

pandas/core/tools/timedeltas.py

+42-11
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,27 @@
1818

1919
def to_timedelta(arg, unit='ns', box=True, errors='raise'):
2020
"""
21-
Convert argument to timedelta
21+
Convert argument to timedelta.
22+
23+
Timedeltas are differences in times, expressed in difference units,
24+
for example, years, momths, days, hours, minutes, seconds.
25+
They can be both positive and negative. This method can create Timedelta
26+
objects from pandas objects.
2227
2328
Parameters
2429
----------
25-
arg : string, timedelta, list, tuple, 1-d array, or Series
26-
unit : unit of the arg (D,h,m,s,ms,us,ns) denote the unit, which is an
27-
integer/float number
28-
box : boolean, default True
29-
- If True returns a Timedelta/TimedeltaIndex of the results
30-
- if False returns a np.timedelta64 or ndarray of values of dtype
31-
timedelta64[ns]
30+
arg : String, timedelta, list, tuple, 1-d array, or Series
31+
The argument which needs to be converted to timedelta.
32+
unit : Integer or float
33+
Denotes the unit (Y,M,D,h,m,s,ms,us,ns) of the arg.
34+
box : Boolean, default True
35+
If True returns a Timedelta/TimedeltaIndex of the results.
36+
if False returns a np.timedelta64 or ndarray of values of dtype
37+
timedelta64[ns].
3238
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
33-
- If 'raise', then invalid parsing will raise an exception
34-
- If 'coerce', then invalid parsing will be set as NaT
35-
- If 'ignore', then invalid parsing will return the input
39+
If 'raise', then invalid parsing will raise an exception.
40+
If 'coerce', then invalid parsing will be set as NaT.
41+
If 'ignore', then invalid parsing will return the input.
3642
3743
Returns
3844
-------
@@ -64,6 +70,31 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'):
6470
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
6571
dtype='timedelta64[ns]', freq=None)
6672
73+
For `M` and `Y` units, `1M = 30D` and 1Y = 365D:
74+
75+
>>> pd.to_timedelta(np.arange(5), unit='M')
76+
TimedeltaIndex([ '0 days 00:00:00', '30 days 10:29:06',
77+
'60 days 20:58:12', '91 days 07:27:18',
78+
'121 days 17:56:24'],
79+
dtype='timedelta64[ns]', freq=None)
80+
>>> pd.to_timedelta(np.arange(5), unit='Y')
81+
TimedeltaIndex([ '0 days 00:00:00', '365 days 05:49:12',
82+
'730 days 11:38:24', '1095 days 17:27:36',
83+
'1460 days 23:16:48'],
84+
dtype='timedelta64[ns]', freq=None)
85+
86+
Add new column of dates from existing dates in a `DataFrame`
87+
using `timedelta`
88+
89+
>>> Dates = pd.to_datetime(['26/10/2018','28/10/2018', '2/11/2018'])
90+
>>> df = pd.DataFrame({'Start': Dates,'Days':[5, 10, 5]})
91+
>>> df['End'] = df['Start'] + pd.to_timedelta(df['Days'], unit='d')
92+
>>> df
93+
Start Days End
94+
0 2018-10-26 5 2018-10-31
95+
1 2018-10-28 10 2018-11-07
96+
2 2018-02-11 5 2018-02-16
97+
6798
See also
6899
--------
69100
pandas.DataFrame.astype : Cast argument to a specified dtype.

0 commit comments

Comments
 (0)