|
16 | 16 |
|
17 | 17 | def to_timedelta(arg, unit='ns', box=True, errors='raise'):
|
18 | 18 | """
|
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. |
20 | 25 |
|
21 | 26 | Parameters
|
22 | 27 | ----------
|
23 | 28 | 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' |
26 | 30 | Possible values:
|
27 | 31 | {'Y', 'M', 'W', 'D', 'days', 'day', 'hours', hour', 'hr', 'h',
|
28 | 32 | 'm', 'minute', 'min', 'minutes', 'T', 'S', 'seconds', 'sec', 'second',
|
29 | 33 | 'ms', 'milliseconds', 'millisecond', 'milli', 'millis', 'L',
|
30 | 34 | 'us', 'microseconds', 'microsecond', 'micro', 'micros', 'U',
|
31 | 35 | '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]. |
36 | 40 | 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. |
40 | 44 |
|
41 | 45 | Returns
|
42 | 46 | -------
|
@@ -68,6 +72,31 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'):
|
68 | 72 | TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
|
69 | 73 | dtype='timedelta64[ns]', freq=None)
|
70 | 74 |
|
| 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 | +
|
71 | 100 | See also
|
72 | 101 | --------
|
73 | 102 | pandas.DataFrame.astype : Cast argument to a specified dtype.
|
|
0 commit comments