Skip to content

Commit 7f0eefc

Browse files
jackielengjorisvandenbossche
authored andcommitted
DOC: consistent import timedeltas docs (#14997)
xref #9886
1 parent eecfa88 commit 7f0eefc

File tree

1 file changed

+52
-53
lines changed

1 file changed

+52
-53
lines changed

doc/source/timedeltas.rst

+52-53
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
.. ipython:: python
55
:suppress:
66
7-
from datetime import datetime, timedelta
7+
import datetime
88
import numpy as np
9+
import pandas as pd
910
np.random.seed(123456)
10-
from pandas import *
1111
randn = np.random.randn
1212
randint = np.random.randint
1313
np.set_printoptions(precision=4, suppress=True)
14-
options.display.max_rows=15
14+
pd.options.display.max_rows=15
1515
import dateutil
1616
import pytz
1717
from dateutil.relativedelta import relativedelta
18-
from pandas.tseries.api import *
1918
from pandas.tseries.offsets import *
2019
2120
.. _timedeltas.timedeltas:
@@ -40,41 +39,41 @@ You can construct a ``Timedelta`` scalar through various arguments:
4039
.. ipython:: python
4140
4241
# 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')
4746
4847
# like datetime.timedelta
4948
# note: these MUST be specified as keyword arguments
50-
Timedelta(days=1, seconds=1)
49+
pd.Timedelta(days=1, seconds=1)
5150
5251
# integers with a unit
53-
Timedelta(1, unit='d')
52+
pd.Timedelta(1, unit='d')
5453
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'))
5857
5958
# negative Timedeltas have this string repr
6059
# to be more consistent with datetime.timedelta conventions
61-
Timedelta('-1us')
60+
pd.Timedelta('-1us')
6261
6362
# a NaT
64-
Timedelta('nan')
65-
Timedelta('nat')
63+
pd.Timedelta('nan')
64+
pd.Timedelta('nat')
6665
6766
:ref:`DateOffsets<timeseries.offsets>` (``Day, Hour, Minute, Second, Milli, Micro, Nano``) can also be used in construction.
6867

6968
.. ipython:: python
7069
71-
Timedelta(Second(2))
70+
pd.Timedelta(Second(2))
7271
7372
Further, operations among the scalars yield another scalar ``Timedelta``.
7473

7574
.. ipython:: python
7675
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')
7877
7978
to_timedelta
8079
~~~~~~~~~~~~
@@ -93,21 +92,21 @@ You can parse a single string to a Timedelta:
9392

9493
.. ipython:: python
9594
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')
9897
9998
or a list/array of strings:
10099

101100
.. ipython:: python
102101
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'])
104103
105104
The ``unit`` keyword argument specifies the unit of the Timedelta:
106105

107106
.. ipython:: python
108107
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')
111110
112111
.. _timedeltas.limitations:
113112

@@ -133,17 +132,17 @@ subtraction operations on ``datetime64[ns]`` Series, or ``Timestamps``.
133132

134133
.. ipython:: python
135134
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))
139138
df
140139
df['C'] = df['A'] + df['B']
141140
df
142141
df.dtypes
143142
144143
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)
147146
s + Minute(5)
148147
s + Minute(5) + Milli(5)
149148
@@ -173,17 +172,17 @@ Operands can also appear in a reversed order (a singular object operated with a
173172
.. ipython:: python
174173
175174
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
178177
179178
``min, max`` and the corresponding ``idxmin, idxmax`` operations are supported on frames:
180179

181180
.. ipython:: python
182181
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'))
185184
186-
df = DataFrame(dict(A=A, B=B))
185+
df = pd.DataFrame(dict(A=A, B=B))
187186
df
188187
189188
df.min()
@@ -209,13 +208,13 @@ pass a timedelta to get a particular value.
209208
210209
y.fillna(0)
211210
y.fillna(10)
212-
y.fillna(Timedelta('-1 days, 00:00:05'))
211+
y.fillna(pd.Timedelta('-1 days, 00:00:05'))
213212
214213
You can also negate, multiply and use ``abs`` on ``Timedeltas``:
215214

216215
.. ipython:: python
217216
218-
td1 = Timedelta('-1 days 2 hours 3 seconds')
217+
td1 = pd.Timedelta('-1 days 2 hours 3 seconds')
219218
td1
220219
-1 * td1
221220
- td1
@@ -231,7 +230,7 @@ Numeric reduction operation for ``timedelta64[ns]`` will return ``Timedelta`` ob
231230

232231
.. ipython:: python
233232
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']))
235234
y2
236235
y2.mean()
237236
y2.median()
@@ -251,9 +250,9 @@ Note that division by the numpy scalar is true division, while astyping is equiv
251250

252251
.. ipython:: python
253252
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)
257256
td[3] = np.nan
258257
td
259258
@@ -274,7 +273,7 @@ yields another ``timedelta64[ns]`` dtypes Series.
274273
.. ipython:: python
275274
276275
td * -1
277-
td * Series([1, 2, 3, 4])
276+
td * pd.Series([1, 2, 3, 4])
278277
279278
Attributes
280279
----------
@@ -298,7 +297,7 @@ You can access the value of the fields for a scalar ``Timedelta`` directly.
298297

299298
.. ipython:: python
300299
301-
tds = Timedelta('31 days 5 min 3 sec')
300+
tds = pd.Timedelta('31 days 5 min 3 sec')
302301
tds.days
303302
tds.seconds
304303
(-tds).seconds
@@ -326,15 +325,15 @@ or ``np.timedelta64`` objects. Passing ``np.nan/pd.NaT/nat`` will represent miss
326325

327326
.. ipython:: python
328327
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)])
331330
332331
Similarly to ``date_range``, you can construct regular ranges of a ``TimedeltaIndex``:
333332

334333
.. ipython:: python
335334
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')
338337
339338
Using the TimedeltaIndex
340339
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -344,8 +343,8 @@ Similarly to other of the datetime-like indices, ``DatetimeIndex`` and ``PeriodI
344343

345344
.. ipython:: python
346345
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'))
349348
s
350349
351350
Selections work similarly, with coercion on string-likes and slices:
@@ -354,7 +353,7 @@ Selections work similarly, with coercion on string-likes and slices:
354353
355354
s['1 day':'2 day']
356355
s['1 day 01:00:00']
357-
s[Timedelta('1 day 1h')]
356+
s[pd.Timedelta('1 day 1h')]
358357
359358
Furthermore you can use partial string selection and the range will be inferred:
360359

@@ -369,9 +368,9 @@ Finally, the combination of ``TimedeltaIndex`` with ``DatetimeIndex`` allow cert
369368

370369
.. ipython:: python
371370
372-
tdi = TimedeltaIndex(['1 days', pd.NaT, '2 days'])
371+
tdi = pd.TimedeltaIndex(['1 days', pd.NaT, '2 days'])
373372
tdi.tolist()
374-
dti = date_range('20130101', periods=3)
373+
dti = pd.date_range('20130101', periods=3)
375374
dti.tolist()
376375
(dti + tdi).tolist()
377376
(dti - tdi).tolist()
@@ -391,14 +390,14 @@ Scalars type ops work as well. These can potentially return a *different* type o
391390
.. ipython:: python
392391
393392
# adding or timedelta and date -> datelike
394-
tdi + Timestamp('20130101')
393+
tdi + pd.Timestamp('20130101')
395394
396395
# subtraction of a date and a timedelta -> datelike
397396
# 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()
399398
400399
# timedelta + timedelta -> timedelta
401-
tdi + Timedelta('10 days')
400+
tdi + pd.Timedelta('10 days')
402401
403402
# division can result in a Timedelta if the divisor is an integer
404403
tdi / 2

0 commit comments

Comments
 (0)