Skip to content

Commit b1caf01

Browse files
committed
Merge pull request #11789 from proinsias/doc_timedeltas
DOC: Add context to pd.to_timedelta examples
2 parents cbf7043 + 5ac1aed commit b1caf01

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

doc/source/timedeltas.rst

+46-34
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ You can construct a ``Timedelta`` scalar through various arguments:
4747
4848
# like datetime.timedelta
4949
# note: these MUST be specified as keyword arguments
50-
Timedelta(days=1,seconds=1)
50+
Timedelta(days=1, seconds=1)
5151
5252
# integers with a unit
53-
Timedelta(1,unit='d')
53+
Timedelta(1, unit='d')
5454
5555
# from a timedelta/np.timedelta64
56-
Timedelta(timedelta(days=1,seconds=1))
57-
Timedelta(np.timedelta64(1,'ms'))
56+
Timedelta(timedelta(days=1, seconds=1))
57+
Timedelta(np.timedelta64(1, 'ms'))
5858
5959
# negative Timedeltas have this string repr
6060
# to be more consistent with datetime.timedelta conventions
@@ -70,7 +70,7 @@ You can construct a ``Timedelta`` scalar through various arguments:
7070
7171
Timedelta(Second(2))
7272
73-
Further, operations among the scalars yield another scalar ``Timedelta``
73+
Further, operations among the scalars yield another scalar ``Timedelta``.
7474

7575
.. ipython:: python
7676
@@ -84,18 +84,30 @@ to_timedelta
8484
Prior to 0.15.0 ``pd.to_timedelta`` would return a ``Series`` for list-like/Series input, and a ``np.timedelta64`` for scalar input.
8585
It will now return a ``TimedeltaIndex`` for list-like input, ``Series`` for Series input, and ``Timedelta`` for scalar input.
8686

87-
The arguments to ``pd.to_timedelta`` are now ``(arg,unit='ns',box=True)``, previously were ``(arg,box=True,unit='ns')`` as these are more logical.
87+
The arguments to ``pd.to_timedelta`` are now ``(arg, unit='ns', box=True)``, previously were ``(arg, box=True, unit='ns')`` as these are more logical.
8888

8989
Using the top-level ``pd.to_timedelta``, you can convert a scalar, array, list, or Series from a recognized timedelta format / value into a ``Timedelta`` type.
90-
It will construct Series if the input is a Series, a scalar if the input is scalar-like, otherwise will output a ``TimedeltaIndex``
90+
It will construct Series if the input is a Series, a scalar if the input is scalar-like, otherwise will output a ``TimedeltaIndex``.
91+
92+
You can parse a single string to a Timedelta:
9193

9294
.. ipython:: python
9395
9496
to_timedelta('1 days 06:05:01.00003')
9597
to_timedelta('15.5us')
96-
to_timedelta(['1 days 06:05:01.00003','15.5us','nan'])
97-
to_timedelta(np.arange(5),unit='s')
98-
to_timedelta(np.arange(5),unit='d')
98+
99+
or a list/array of strings:
100+
101+
.. ipython:: python
102+
103+
to_timedelta(['1 days 06:05:01.00003', '15.5us', 'nan'])
104+
105+
The ``unit`` keyword argument specifies the unit of the Timedelta:
106+
107+
.. ipython:: python
108+
109+
to_timedelta(np.arange(5), unit='s')
110+
to_timedelta(np.arange(5), unit='d')
99111
100112
.. _timedeltas.operations:
101113

@@ -116,41 +128,41 @@ subtraction operations on ``datetime64[ns]`` Series, or ``Timestamps``.
116128
df.dtypes
117129
118130
s - s.max()
119-
s - datetime(2011,1,1,3,5)
131+
s - datetime(2011, 1, 1, 3, 5)
120132
s + timedelta(minutes=5)
121133
s + Minute(5)
122134
s + Minute(5) + Milli(5)
123135
124-
Operations with scalars from a ``timedelta64[ns]`` series
136+
Operations with scalars from a ``timedelta64[ns]`` series:
125137

126138
.. ipython:: python
127139
128140
y = s - s[0]
129141
y
130142
131-
Series of timedeltas with ``NaT`` values are supported
143+
Series of timedeltas with ``NaT`` values are supported:
132144

133145
.. ipython:: python
134146
135147
y = s - s.shift()
136148
y
137149
138-
Elements can be set to ``NaT`` using ``np.nan`` analogously to datetimes
150+
Elements can be set to ``NaT`` using ``np.nan`` analogously to datetimes:
139151

140152
.. ipython:: python
141153
142154
y[1] = np.nan
143155
y
144156
145-
Operands can also appear in a reversed order (a singular object operated with a Series)
157+
Operands can also appear in a reversed order (a singular object operated with a Series):
146158

147159
.. ipython:: python
148160
149161
s.max() - s
150-
datetime(2011,1,1,3,5) - s
162+
datetime(2011, 1, 1, 3, 5) - s
151163
timedelta(minutes=5) + s
152164
153-
``min, max`` and the corresponding ``idxmin, idxmax`` operations are supported on frames
165+
``min, max`` and the corresponding ``idxmin, idxmax`` operations are supported on frames:
154166

155167
.. ipython:: python
156168
@@ -185,7 +197,7 @@ pass a timedelta to get a particular value.
185197
y.fillna(10)
186198
y.fillna(Timedelta('-1 days, 00:00:05'))
187199
188-
You can also negate, multiply and use ``abs`` on ``Timedeltas``
200+
You can also negate, multiply and use ``abs`` on ``Timedeltas``:
189201

190202
.. ipython:: python
191203
@@ -205,7 +217,7 @@ Numeric reduction operation for ``timedelta64[ns]`` will return ``Timedelta`` ob
205217

206218
.. ipython:: python
207219
208-
y2 = Series(to_timedelta(['-1 days +00:00:05','nat','-1 days +00:00:05','1 days']))
220+
y2 = Series(to_timedelta(['-1 days +00:00:05', 'nat', '-1 days +00:00:05', '1 days']))
209221
y2
210222
y2.mean()
211223
y2.median()
@@ -225,30 +237,30 @@ Note that division by the numpy scalar is true division, while astyping is equiv
225237

226238
.. ipython:: python
227239
228-
td = Series(date_range('20130101',periods=4)) - \
229-
Series(date_range('20121201',periods=4))
230-
td[2] += timedelta(minutes=5,seconds=3)
240+
td = Series(date_range('20130101', periods=4)) - \
241+
Series(date_range('20121201', periods=4))
242+
td[2] += timedelta(minutes=5, seconds=3)
231243
td[3] = np.nan
232244
td
233245
234246
# to days
235-
td / np.timedelta64(1,'D')
247+
td / np.timedelta64(1, 'D')
236248
td.astype('timedelta64[D]')
237249
238250
# to seconds
239-
td / np.timedelta64(1,'s')
251+
td / np.timedelta64(1, 's')
240252
td.astype('timedelta64[s]')
241253
242254
# to months (these are constant months)
243-
td / np.timedelta64(1,'M')
255+
td / np.timedelta64(1, 'M')
244256
245257
Dividing or multiplying a ``timedelta64[ns]`` Series by an integer or integer Series
246258
yields another ``timedelta64[ns]`` dtypes Series.
247259

248260
.. ipython:: python
249261
250262
td * -1
251-
td * Series([1,2,3,4])
263+
td * Series([1, 2, 3, 4])
252264
253265
Attributes
254266
----------
@@ -261,7 +273,7 @@ These operations can also be directly accessed via the ``.dt`` property of the `
261273

262274
Note that the attributes are NOT the displayed values of the ``Timedelta``. Use ``.components`` to retrieve the displayed values.
263275

264-
For a ``Series``
276+
For a ``Series``:
265277

266278
.. ipython:: python
267279
@@ -300,15 +312,15 @@ or ``np.timedelta64`` objects. Passing ``np.nan/pd.NaT/nat`` will represent miss
300312

301313
.. ipython:: python
302314
303-
TimedeltaIndex(['1 days','1 days, 00:00:05',
304-
np.timedelta64(2,'D'),timedelta(days=2,seconds=2)])
315+
TimedeltaIndex(['1 days', '1 days, 00:00:05',
316+
np.timedelta64(2,'D'), timedelta(days=2,seconds=2)])
305317
306318
Similarly to ``date_range``, you can construct regular ranges of a ``TimedeltaIndex``:
307319

308320
.. ipython:: python
309321
310-
timedelta_range(start='1 days',periods=5,freq='D')
311-
timedelta_range(start='1 days',end='2 days',freq='30T')
322+
timedelta_range(start='1 days', periods=5, freq='D')
323+
timedelta_range(start='1 days', end='2 days', freq='30T')
312324
313325
Using the TimedeltaIndex
314326
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -319,7 +331,7 @@ Similarly to other of the datetime-like indices, ``DatetimeIndex`` and ``PeriodI
319331
.. ipython:: python
320332
321333
s = Series(np.arange(100),
322-
index=timedelta_range('1 days',periods=100,freq='h'))
334+
index=timedelta_range('1 days', periods=100, freq='h'))
323335
s
324336
325337
Selections work similary, with coercion on string-likes and slices:
@@ -343,9 +355,9 @@ Finally, the combination of ``TimedeltaIndex`` with ``DatetimeIndex`` allow cert
343355

344356
.. ipython:: python
345357
346-
tdi = TimedeltaIndex(['1 days',pd.NaT,'2 days'])
358+
tdi = TimedeltaIndex(['1 days', pd.NaT, '2 days'])
347359
tdi.tolist()
348-
dti = date_range('20130101',periods=3)
360+
dti = date_range('20130101', periods=3)
349361
dti.tolist()
350362
(dti + tdi).tolist()
351363
(dti - tdi).tolist()

0 commit comments

Comments
 (0)