|
18 | 18 |
|
19 | 19 | def to_timedelta(arg, unit='ns', box=True, errors='raise'):
|
20 | 20 | """
|
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. |
22 | 27 |
|
23 | 28 | Parameters
|
24 | 29 | ----------
|
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]. |
32 | 38 | 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. |
36 | 42 |
|
37 | 43 | Returns
|
38 | 44 | -------
|
@@ -64,6 +70,31 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'):
|
64 | 70 | TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
|
65 | 71 | dtype='timedelta64[ns]', freq=None)
|
66 | 72 |
|
| 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 | +
|
67 | 98 | See also
|
68 | 99 | --------
|
69 | 100 | pandas.DataFrame.astype : Cast argument to a specified dtype.
|
|
0 commit comments