You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update the reStructuredText tutorial on timedeltas to include
context, similar to edits suggested for #11785.
Also, add a space after various commas (PEP8), and periods/colons
at the end of sentences for consistency.
Copy file name to clipboardExpand all lines: doc/source/timedeltas.rst
+46-34
Original file line number
Diff line number
Diff line change
@@ -47,14 +47,14 @@ You can construct a ``Timedelta`` scalar through various arguments:
47
47
48
48
# like datetime.timedelta
49
49
# note: these MUST be specified as keyword arguments
50
-
Timedelta(days=1,seconds=1)
50
+
Timedelta(days=1,seconds=1)
51
51
52
52
# integers with a unit
53
-
Timedelta(1,unit='d')
53
+
Timedelta(1,unit='d')
54
54
55
55
# 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'))
58
58
59
59
# negative Timedeltas have this string repr
60
60
# to be more consistent with datetime.timedelta conventions
@@ -70,7 +70,7 @@ You can construct a ``Timedelta`` scalar through various arguments:
70
70
71
71
Timedelta(Second(2))
72
72
73
-
Further, operations among the scalars yield another scalar ``Timedelta``
73
+
Further, operations among the scalars yield another scalar ``Timedelta``.
74
74
75
75
.. ipython:: python
76
76
@@ -84,18 +84,30 @@ to_timedelta
84
84
Prior to 0.15.0 ``pd.to_timedelta`` would return a ``Series`` for list-like/Series input, and a ``np.timedelta64`` for scalar input.
85
85
It will now return a ``TimedeltaIndex`` for list-like input, ``Series`` for Series input, and ``Timedelta`` for scalar input.
86
86
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.
88
88
89
89
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:
91
93
92
94
.. ipython:: python
93
95
94
96
to_timedelta('1 days 06:05:01.00003')
95
97
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')
99
111
100
112
.. _timedeltas.operations:
101
113
@@ -116,41 +128,41 @@ subtraction operations on ``datetime64[ns]`` Series, or ``Timestamps``.
116
128
df.dtypes
117
129
118
130
s - s.max()
119
-
s - datetime(2011,1,1,3,5)
131
+
s - datetime(2011,1, 1, 3, 5)
120
132
s + timedelta(minutes=5)
121
133
s + Minute(5)
122
134
s + Minute(5) + Milli(5)
123
135
124
-
Operations with scalars from a ``timedelta64[ns]`` series
136
+
Operations with scalars from a ``timedelta64[ns]`` series:
125
137
126
138
.. ipython:: python
127
139
128
140
y = s - s[0]
129
141
y
130
142
131
-
Series of timedeltas with ``NaT`` values are supported
143
+
Series of timedeltas with ``NaT`` values are supported:
132
144
133
145
.. ipython:: python
134
146
135
147
y = s - s.shift()
136
148
y
137
149
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:
139
151
140
152
.. ipython:: python
141
153
142
154
y[1] = np.nan
143
155
y
144
156
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):
146
158
147
159
.. ipython:: python
148
160
149
161
s.max() - s
150
-
datetime(2011,1,1,3,5) - s
162
+
datetime(2011,1, 1, 3, 5) - s
151
163
timedelta(minutes=5) + s
152
164
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:
154
166
155
167
.. ipython:: python
156
168
@@ -185,7 +197,7 @@ pass a timedelta to get a particular value.
185
197
y.fillna(10)
186
198
y.fillna(Timedelta('-1 days, 00:00:05'))
187
199
188
-
You can also negate, multiply and use ``abs`` on ``Timedeltas``
200
+
You can also negate, multiply and use ``abs`` on ``Timedeltas``:
189
201
190
202
.. ipython:: python
191
203
@@ -205,7 +217,7 @@ Numeric reduction operation for ``timedelta64[ns]`` will return ``Timedelta`` ob
205
217
206
218
.. ipython:: python
207
219
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']))
209
221
y2
210
222
y2.mean()
211
223
y2.median()
@@ -225,30 +237,30 @@ Note that division by the numpy scalar is true division, while astyping is equiv
225
237
226
238
.. ipython:: python
227
239
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)
231
243
td[3] = np.nan
232
244
td
233
245
234
246
# to days
235
-
td / np.timedelta64(1,'D')
247
+
td / np.timedelta64(1,'D')
236
248
td.astype('timedelta64[D]')
237
249
238
250
# to seconds
239
-
td / np.timedelta64(1,'s')
251
+
td / np.timedelta64(1,'s')
240
252
td.astype('timedelta64[s]')
241
253
242
254
# to months (these are constant months)
243
-
td / np.timedelta64(1,'M')
255
+
td / np.timedelta64(1,'M')
244
256
245
257
Dividing or multiplying a ``timedelta64[ns]`` Series by an integer or integer Series
246
258
yields another ``timedelta64[ns]`` dtypes Series.
247
259
248
260
.. ipython:: python
249
261
250
262
td *-1
251
-
td * Series([1,2,3,4])
263
+
td * Series([1,2, 3, 4])
252
264
253
265
Attributes
254
266
----------
@@ -261,7 +273,7 @@ These operations can also be directly accessed via the ``.dt`` property of the `
261
273
262
274
Note that the attributes are NOT the displayed values of the ``Timedelta``. Use ``.components`` to retrieve the displayed values.
263
275
264
-
For a ``Series``
276
+
For a ``Series``:
265
277
266
278
.. ipython:: python
267
279
@@ -300,15 +312,15 @@ or ``np.timedelta64`` objects. Passing ``np.nan/pd.NaT/nat`` will represent miss
0 commit comments