4
4
.. ipython :: python
5
5
:suppress:
6
6
7
- from datetime import datetime, timedelta
7
+ import datetime
8
8
import numpy as np
9
+ import pandas as pd
9
10
np.random.seed(123456 )
10
- from pandas import *
11
11
randn = np.random.randn
12
12
randint = np.random.randint
13
13
np.set_printoptions(precision = 4 , suppress = True )
14
- options.display.max_rows= 15
14
+ pd. options.display.max_rows= 15
15
15
import dateutil
16
16
import pytz
17
17
from dateutil.relativedelta import relativedelta
18
- from pandas.tseries.api import *
19
18
from pandas.tseries.offsets import *
20
19
21
20
.. _timedeltas.timedeltas :
@@ -40,41 +39,41 @@ You can construct a ``Timedelta`` scalar through various arguments:
40
39
.. ipython :: python
41
40
42
41
# strings
43
- Timedelta(' 1 days' )
44
- Timedelta(' 1 days 00:00:00' )
45
- Timedelta(' 1 days 2 hours' )
46
- Timedelta(' -1 days 2 min 3us' )
42
+ pd. Timedelta(' 1 days' )
43
+ pd. Timedelta(' 1 days 00:00:00' )
44
+ pd. Timedelta(' 1 days 2 hours' )
45
+ pd. Timedelta(' -1 days 2 min 3us' )
47
46
48
47
# like datetime.timedelta
49
48
# note: these MUST be specified as keyword arguments
50
- Timedelta(days = 1 , seconds = 1 )
49
+ pd. Timedelta(days = 1 , seconds = 1 )
51
50
52
51
# integers with a unit
53
- Timedelta(1 , unit = ' d' )
52
+ pd. Timedelta(1 , unit = ' d' )
54
53
55
- # from a timedelta/np.timedelta64
56
- Timedelta(timedelta(days = 1 , seconds = 1 ))
57
- Timedelta(np.timedelta64(1 , ' ms' ))
54
+ # from a datetime. timedelta/np.timedelta64
55
+ pd. Timedelta(datetime. timedelta(days = 1 , seconds = 1 ))
56
+ pd. Timedelta(np.timedelta64(1 , ' ms' ))
58
57
59
58
# negative Timedeltas have this string repr
60
59
# to be more consistent with datetime.timedelta conventions
61
- Timedelta(' -1us' )
60
+ pd. Timedelta(' -1us' )
62
61
63
62
# a NaT
64
- Timedelta(' nan' )
65
- Timedelta(' nat' )
63
+ pd. Timedelta(' nan' )
64
+ pd. Timedelta(' nat' )
66
65
67
66
:ref: `DateOffsets<timeseries.offsets> ` (``Day, Hour, Minute, Second, Milli, Micro, Nano ``) can also be used in construction.
68
67
69
68
.. ipython :: python
70
69
71
- Timedelta(Second(2 ))
70
+ pd. Timedelta(Second(2 ))
72
71
73
72
Further, operations among the scalars yield another scalar ``Timedelta ``.
74
73
75
74
.. ipython :: python
76
75
77
- Timedelta(Day(2 )) + Timedelta(Second(2 )) + Timedelta(' 00:00:00.000123' )
76
+ pd. Timedelta(Day(2 )) + pd. Timedelta(Second(2 )) + pd. Timedelta(' 00:00:00.000123' )
78
77
79
78
to_timedelta
80
79
~~~~~~~~~~~~
@@ -93,21 +92,21 @@ You can parse a single string to a Timedelta:
93
92
94
93
.. ipython :: python
95
94
96
- to_timedelta(' 1 days 06:05:01.00003' )
97
- to_timedelta(' 15.5us' )
95
+ pd. to_timedelta(' 1 days 06:05:01.00003' )
96
+ pd. to_timedelta(' 15.5us' )
98
97
99
98
or a list/array of strings:
100
99
101
100
.. ipython :: python
102
101
103
- to_timedelta([' 1 days 06:05:01.00003' , ' 15.5us' , ' nan' ])
102
+ pd. to_timedelta([' 1 days 06:05:01.00003' , ' 15.5us' , ' nan' ])
104
103
105
104
The ``unit `` keyword argument specifies the unit of the Timedelta:
106
105
107
106
.. ipython :: python
108
107
109
- to_timedelta(np.arange(5 ), unit = ' s' )
110
- to_timedelta(np.arange(5 ), unit = ' d' )
108
+ pd. to_timedelta(np.arange(5 ), unit = ' s' )
109
+ pd. to_timedelta(np.arange(5 ), unit = ' d' )
111
110
112
111
.. _timedeltas.limitations :
113
112
@@ -133,17 +132,17 @@ subtraction operations on ``datetime64[ns]`` Series, or ``Timestamps``.
133
132
134
133
.. ipython :: python
135
134
136
- s = Series(date_range(' 2012-1-1' , periods = 3 , freq = ' D' ))
137
- td = Series([ Timedelta(days = i) for i in range (3 ) ])
138
- df = DataFrame(dict (A = s, B = td))
135
+ s = pd. Series(pd. date_range(' 2012-1-1' , periods = 3 , freq = ' D' ))
136
+ td = pd. Series([ pd. Timedelta(days = i) for i in range (3 ) ])
137
+ df = pd. DataFrame(dict (A = s, B = td))
139
138
df
140
139
df[' C' ] = df[' A' ] + df[' B' ]
141
140
df
142
141
df.dtypes
143
142
144
143
s - s.max()
145
- s - datetime(2011 , 1 , 1 , 3 , 5 )
146
- s + timedelta(minutes = 5 )
144
+ s - datetime.datetime (2011 , 1 , 1 , 3 , 5 )
145
+ s + datetime. timedelta(minutes = 5 )
147
146
s + Minute(5 )
148
147
s + Minute(5 ) + Milli(5 )
149
148
@@ -173,17 +172,17 @@ Operands can also appear in a reversed order (a singular object operated with a
173
172
.. ipython :: python
174
173
175
174
s.max() - s
176
- datetime(2011 , 1 , 1 , 3 , 5 ) - s
177
- timedelta(minutes = 5 ) + s
175
+ datetime.datetime (2011 , 1 , 1 , 3 , 5 ) - s
176
+ datetime. timedelta(minutes = 5 ) + s
178
177
179
178
``min, max `` and the corresponding ``idxmin, idxmax `` operations are supported on frames:
180
179
181
180
.. ipython :: python
182
181
183
- A = s - Timestamp(' 20120101' ) - Timedelta(' 00:05:05' )
184
- B = s - Series(date_range(' 2012-1-2' , periods = 3 , freq = ' D' ))
182
+ A = s - pd. Timestamp(' 20120101' ) - pd. Timedelta(' 00:05:05' )
183
+ B = s - pd. Series(pd. date_range(' 2012-1-2' , periods = 3 , freq = ' D' ))
185
184
186
- df = DataFrame(dict (A = A, B = B))
185
+ df = pd. DataFrame(dict (A = A, B = B))
187
186
df
188
187
189
188
df.min()
@@ -209,13 +208,13 @@ pass a timedelta to get a particular value.
209
208
210
209
y.fillna(0 )
211
210
y.fillna(10 )
212
- y.fillna(Timedelta(' -1 days, 00:00:05' ))
211
+ y.fillna(pd. Timedelta(' -1 days, 00:00:05' ))
213
212
214
213
You can also negate, multiply and use ``abs `` on ``Timedeltas ``:
215
214
216
215
.. ipython :: python
217
216
218
- td1 = Timedelta(' -1 days 2 hours 3 seconds' )
217
+ td1 = pd. Timedelta(' -1 days 2 hours 3 seconds' )
219
218
td1
220
219
- 1 * td1
221
220
- td1
@@ -231,7 +230,7 @@ Numeric reduction operation for ``timedelta64[ns]`` will return ``Timedelta`` ob
231
230
232
231
.. ipython :: python
233
232
234
- y2 = Series(to_timedelta([' -1 days +00:00:05' , ' nat' , ' -1 days +00:00:05' , ' 1 days' ]))
233
+ y2 = pd. Series(pd. to_timedelta([' -1 days +00:00:05' , ' nat' , ' -1 days +00:00:05' , ' 1 days' ]))
235
234
y2
236
235
y2.mean()
237
236
y2.median()
@@ -251,9 +250,9 @@ Note that division by the numpy scalar is true division, while astyping is equiv
251
250
252
251
.. ipython :: python
253
252
254
- td = Series(date_range(' 20130101' , periods = 4 )) - \
255
- Series(date_range(' 20121201' , periods = 4 ))
256
- td[2 ] += timedelta(minutes = 5 , seconds = 3 )
253
+ td = pd. Series(pd. date_range(' 20130101' , periods = 4 )) - \
254
+ pd. Series(pd. date_range(' 20121201' , periods = 4 ))
255
+ td[2 ] += datetime. timedelta(minutes = 5 , seconds = 3 )
257
256
td[3 ] = np.nan
258
257
td
259
258
@@ -274,7 +273,7 @@ yields another ``timedelta64[ns]`` dtypes Series.
274
273
.. ipython :: python
275
274
276
275
td * - 1
277
- td * Series([1 , 2 , 3 , 4 ])
276
+ td * pd. Series([1 , 2 , 3 , 4 ])
278
277
279
278
Attributes
280
279
----------
@@ -298,7 +297,7 @@ You can access the value of the fields for a scalar ``Timedelta`` directly.
298
297
299
298
.. ipython :: python
300
299
301
- tds = Timedelta(' 31 days 5 min 3 sec' )
300
+ tds = pd. Timedelta(' 31 days 5 min 3 sec' )
302
301
tds.days
303
302
tds.seconds
304
303
(- tds).seconds
@@ -326,15 +325,15 @@ or ``np.timedelta64`` objects. Passing ``np.nan/pd.NaT/nat`` will represent miss
326
325
327
326
.. ipython :: python
328
327
329
- TimedeltaIndex([' 1 days' , ' 1 days, 00:00:05' ,
330
- np.timedelta64(2 ,' D' ), timedelta(days = 2 ,seconds = 2 )])
328
+ pd. TimedeltaIndex([' 1 days' , ' 1 days, 00:00:05' ,
329
+ np.timedelta64(2 ,' D' ), datetime. timedelta(days = 2 ,seconds = 2 )])
331
330
332
331
Similarly to ``date_range ``, you can construct regular ranges of a ``TimedeltaIndex ``:
333
332
334
333
.. ipython :: python
335
334
336
- timedelta_range(start = ' 1 days' , periods = 5 , freq = ' D' )
337
- timedelta_range(start = ' 1 days' , end = ' 2 days' , freq = ' 30T' )
335
+ pd. timedelta_range(start = ' 1 days' , periods = 5 , freq = ' D' )
336
+ pd. timedelta_range(start = ' 1 days' , end = ' 2 days' , freq = ' 30T' )
338
337
339
338
Using the TimedeltaIndex
340
339
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -344,8 +343,8 @@ Similarly to other of the datetime-like indices, ``DatetimeIndex`` and ``PeriodI
344
343
345
344
.. ipython :: python
346
345
347
- s = Series(np.arange(100 ),
348
- index = timedelta_range(' 1 days' , periods = 100 , freq = ' h' ))
346
+ s = pd. Series(np.arange(100 ),
347
+ index = pd. timedelta_range(' 1 days' , periods = 100 , freq = ' h' ))
349
348
s
350
349
351
350
Selections work similarly, with coercion on string-likes and slices:
@@ -354,7 +353,7 @@ Selections work similarly, with coercion on string-likes and slices:
354
353
355
354
s[' 1 day' :' 2 day' ]
356
355
s[' 1 day 01:00:00' ]
357
- s[Timedelta(' 1 day 1h' )]
356
+ s[pd. Timedelta(' 1 day 1h' )]
358
357
359
358
Furthermore you can use partial string selection and the range will be inferred:
360
359
@@ -369,9 +368,9 @@ Finally, the combination of ``TimedeltaIndex`` with ``DatetimeIndex`` allow cert
369
368
370
369
.. ipython :: python
371
370
372
- tdi = TimedeltaIndex([' 1 days' , pd.NaT, ' 2 days' ])
371
+ tdi = pd. TimedeltaIndex([' 1 days' , pd.NaT, ' 2 days' ])
373
372
tdi.tolist()
374
- dti = date_range(' 20130101' , periods = 3 )
373
+ dti = pd. date_range(' 20130101' , periods = 3 )
375
374
dti.tolist()
376
375
(dti + tdi).tolist()
377
376
(dti - tdi).tolist()
@@ -391,14 +390,14 @@ Scalars type ops work as well. These can potentially return a *different* type o
391
390
.. ipython :: python
392
391
393
392
# adding or timedelta and date -> datelike
394
- tdi + Timestamp(' 20130101' )
393
+ tdi + pd. Timestamp(' 20130101' )
395
394
396
395
# subtraction of a date and a timedelta -> datelike
397
396
# note that trying to subtract a date from a Timedelta will raise an exception
398
- (Timestamp(' 20130101' ) - tdi).tolist()
397
+ (pd. Timestamp(' 20130101' ) - tdi).tolist()
399
398
400
399
# timedelta + timedelta -> timedelta
401
- tdi + Timedelta(' 10 days' )
400
+ tdi + pd. Timedelta(' 10 days' )
402
401
403
402
# division can result in a Timedelta if the divisor is an integer
404
403
tdi / 2
0 commit comments