This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
timedeltas.html
680 lines (600 loc) · 68.2 KB
/
timedeltas.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
<span id="timedeltas-timedeltas"></span><span id="timedeltas"></span><h1><span class="yiyi-st" id="yiyi-59">Time Deltas</span></h1>
<blockquote>
<p>原文:<a href="http://pandas.pydata.org/pandas-docs/stable/timedeltas.html">http://pandas.pydata.org/pandas-docs/stable/timedeltas.html</a></p>
<p>译者:<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
<p>校对:(虚位以待)</p>
</blockquote>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-60">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-61">从v0.15.0开始,我们引入了一个新的标量类型<code class="docutils literal"><span class="pre">Timedelta</span></code>,它是<code class="docutils literal"><span class="pre">datetime.timedelta</span></code>的子类,并且行为类似,但允许与<code class="docutils literal"><span class="pre">np.timedelta64</span></code>类型以及主机的自定义表示,解析和属性。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-62">Timedeltas是时间差,以差单位表示,例如。天,小时,分钟,秒。</span><span class="yiyi-st" id="yiyi-63">他们可以是积极的和消极的。</span></p>
<div class="section" id="parsing">
<h2><span class="yiyi-st" id="yiyi-64">Parsing</span></h2>
<p><span class="yiyi-st" id="yiyi-65">您可以通过各种参数构造<code class="docutils literal"><span class="pre">Timedelta</span></code>标量:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># strings</span>
<span class="gp">In [1]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days'</span><span class="p">)</span>
<span class="gr">Out[1]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 00:00:00'</span><span class="p">)</span>
<span class="gp">In [2]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 00:00:00'</span><span class="p">)</span>
<span class="gr">Out[2]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 00:00:00'</span><span class="p">)</span>
<span class="gp">In [3]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 2 hours'</span><span class="p">)</span>
<span class="gr">Out[3]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 02:00:00'</span><span class="p">)</span>
<span class="gp">In [4]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-1 days 2 min 3us'</span><span class="p">)</span>
<span class="gr">Out[4]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-2 days +23:57:59.999997'</span><span class="p">)</span>
<span class="c"># like datetime.timedelta</span>
<span class="c"># note: these MUST be specified as keyword arguments</span>
<span class="gp">In [5]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">seconds</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[5]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 00:00:01'</span><span class="p">)</span>
<span class="c"># integers with a unit</span>
<span class="gp">In [6]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">unit</span><span class="o">=</span><span class="s1">'d'</span><span class="p">)</span>
<span class="gr">Out[6]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 00:00:00'</span><span class="p">)</span>
<span class="c"># from a timedelta/np.timedelta64</span>
<span class="gp">In [7]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">seconds</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
<span class="gr">Out[7]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 00:00:01'</span><span class="p">)</span>
<span class="gp">In [8]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s1">'ms'</span><span class="p">))</span>
<span class="gr">Out[8]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'0 days 00:00:00.001000'</span><span class="p">)</span>
<span class="c"># negative Timedeltas have this string repr</span>
<span class="c"># to be more consistent with datetime.timedelta conventions</span>
<span class="gp">In [9]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-1us'</span><span class="p">)</span>
<span class="gr">Out[9]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-1 days +23:59:59.999999'</span><span class="p">)</span>
<span class="c"># a NaT</span>
<span class="gp">In [10]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'nan'</span><span class="p">)</span>
<span class="gr">Out[10]: </span><span class="n">NaT</span>
<span class="gp">In [11]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'nat'</span><span class="p">)</span>
<span class="gr">Out[11]: </span><span class="n">NaT</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-66"><a class="reference internal" href="timeseries.html#timeseries-offsets"><span class="std std-ref">DateOffsets</span></a>(<code class="docutils literal"><span class="pre">日,</span> <span class="pre">小时,</span> <span class="pre">分钟,</span> <span class="pre">第二,</span> <span class="pre"> Milli,</span> <span class="pre">Micro,</span> <span class="pre">Nano</span></code>)也可用于建筑。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [12]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="n">Second</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
<span class="gr">Out[12]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'0 days 00:00:02'</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-67">此外,标量中的操作产生另一个标量<code class="docutils literal"><span class="pre">Timedelta</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [13]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="n">Day</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span> <span class="o">+</span> <span class="n">Timedelta</span><span class="p">(</span><span class="n">Second</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span> <span class="o">+</span> <span class="n">Timedelta</span><span class="p">(</span><span class="s1">'00:00:00.000123'</span><span class="p">)</span>
<span class="gr">Out[13]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'2 days 00:00:02.000123'</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="to-timedelta">
<h3><span class="yiyi-st" id="yiyi-68">to_timedelta</span></h3>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-69">警告</span></p>
<p><span class="yiyi-st" id="yiyi-70">在0.15.0 <code class="docutils literal"><span class="pre">pd.to_timedelta</span></code>之前,将为标量输入返回<code class="docutils literal"><span class="pre">Series</span></code>,对于标量输入返回<code class="docutils literal"><span class="pre">np.timedelta64</span></code>。</span><span class="yiyi-st" id="yiyi-71">现在,对于类列输入,将返回<code class="docutils literal"><span class="pre">TimedeltaIndex</span></code>,对于系列输入,返回<code class="docutils literal"><span class="pre">Series</span></code>,对于标量输入,返回<code class="docutils literal"><span class="pre">Timedelta</span></code>。</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-72"><code class="docutils literal"><span class="pre">pd.to_timedelta</span></code>的参数现在为<code class="docutils literal"><span class="pre">(arg,</span> <span class="pre">unit ='ns',</span> <span class="pre">box = True) t5></span></code>,之前是<code class="docutils literal"><span class="pre">(arg,</span> <span class="pre">box = True,</span> <span class="pre">unit ='ns')</span>因为这些更合乎逻辑。</code></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-73">使用顶层<code class="docutils literal"><span class="pre">pd.to_timedelta</span></code>,您可以将标量,数组,列表或系列从已识别的timedelta格式/值转换为<code class="docutils literal"><span class="pre">Timedelta</span></code>类型。</span><span class="yiyi-st" id="yiyi-74">如果输入是一个系列,它将构造系列,如果输入是类标量,则构造系列,否则输出<code class="docutils literal"><span class="pre">TimedeltaIndex</span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-75">您可以将单个字符串解析为Timedelta:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [14]: </span><span class="n">to_timedelta</span><span class="p">(</span><span class="s1">'1 days 06:05:01.00003'</span><span class="p">)</span>
<span class="gr">Out[14]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 06:05:01.000030'</span><span class="p">)</span>
<span class="gp">In [15]: </span><span class="n">to_timedelta</span><span class="p">(</span><span class="s1">'15.5us'</span><span class="p">)</span>
<span class="gr">Out[15]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'0 days 00:00:00.000015'</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-76">或字符串的列表/数组:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [16]: </span><span class="n">to_timedelta</span><span class="p">([</span><span class="s1">'1 days 06:05:01.00003'</span><span class="p">,</span> <span class="s1">'15.5us'</span><span class="p">,</span> <span class="s1">'nan'</span><span class="p">])</span>
<span class="gr">Out[16]: </span><span class="n">TimedeltaIndex</span><span class="p">([</span><span class="s1">'1 days 06:05:01.000030'</span><span class="p">,</span> <span class="s1">'0 days 00:00:00.000015'</span><span class="p">,</span> <span class="n">NaT</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'timedelta64[ns]'</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-77"><code class="docutils literal"><span class="pre">unit</span></code>关键字参数指定Timedelta的单位:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [17]: </span><span class="n">to_timedelta</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">unit</span><span class="o">=</span><span class="s1">'s'</span><span class="p">)</span>
<span class="gr">Out[17]: </span><span class="n">TimedeltaIndex</span><span class="p">([</span><span class="s1">'00:00:00'</span><span class="p">,</span> <span class="s1">'00:00:01'</span><span class="p">,</span> <span class="s1">'00:00:02'</span><span class="p">,</span> <span class="s1">'00:00:03'</span><span class="p">,</span> <span class="s1">'00:00:04'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'timedelta64[ns]'</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
<span class="gp">In [18]: </span><span class="n">to_timedelta</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">unit</span><span class="o">=</span><span class="s1">'d'</span><span class="p">)</span>
<span class="gr">Out[18]: </span><span class="n">TimedeltaIndex</span><span class="p">([</span><span class="s1">'0 days'</span><span class="p">,</span> <span class="s1">'1 days'</span><span class="p">,</span> <span class="s1">'2 days'</span><span class="p">,</span> <span class="s1">'3 days'</span><span class="p">,</span> <span class="s1">'4 days'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'timedelta64[ns]'</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="timedelta-limitations">
<span id="timedeltas-limitations"></span><h3><span class="yiyi-st" id="yiyi-78">Timedelta limitations</span></h3>
<p><span class="yiyi-st" id="yiyi-79">Pandas表示使用64位整数的纳秒分辨率的<code class="docutils literal"><span class="pre">Timedeltas</span></code>。</span><span class="yiyi-st" id="yiyi-80">因此,64位整数限制确定<code class="docutils literal"><span class="pre">Timedelta</span></code>限制。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [19]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timedelta</span><span class="o">.</span><span class="n">min</span>
<span class="gr">Out[19]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-106752 days +00:12:43.145224'</span><span class="p">)</span>
<span class="gp">In [20]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timedelta</span><span class="o">.</span><span class="n">max</span>
<span class="gr">Out[20]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'106751 days 23:47:16.854775'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="operations">
<span id="timedeltas-operations"></span><h2><span class="yiyi-st" id="yiyi-81">Operations</span></h2>
<p><span class="yiyi-st" id="yiyi-82">您可以操作系列/数据帧,并通过在<code class="docutils literal"><span class="pre">datetime64[ns]</span></code>系列或<code class="docutils literal"><span class="pre">Timestamps</span></code>上的减法运算构造<code class="docutils literal"><span class="pre">timedelta64[ns]</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [21]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">Series</span><span class="p">(</span><span class="n">date_range</span><span class="p">(</span><span class="s1">'2012-1-1'</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">'D'</span><span class="p">))</span>
<span class="gp">In [22]: </span><span class="n">td</span> <span class="o">=</span> <span class="n">Series</span><span class="p">([</span> <span class="n">Timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="p">])</span>
<span class="gp">In [23]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">DataFrame</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">A</span> <span class="o">=</span> <span class="n">s</span><span class="p">,</span> <span class="n">B</span> <span class="o">=</span> <span class="n">td</span><span class="p">))</span>
<span class="gp">In [24]: </span><span class="n">df</span>
<span class="gr">Out[24]: </span>
<span class="go"> A B</span>
<span class="go">0 2012-01-01 0 days</span>
<span class="go">1 2012-01-02 1 days</span>
<span class="go">2 2012-01-03 2 days</span>
<span class="gp">In [25]: </span><span class="n">df</span><span class="p">[</span><span class="s1">'C'</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">'A'</span><span class="p">]</span> <span class="o">+</span> <span class="n">df</span><span class="p">[</span><span class="s1">'B'</span><span class="p">]</span>
<span class="gp">In [26]: </span><span class="n">df</span>
<span class="gr">Out[26]: </span>
<span class="go"> A B C</span>
<span class="go">0 2012-01-01 0 days 2012-01-01</span>
<span class="go">1 2012-01-02 1 days 2012-01-03</span>
<span class="go">2 2012-01-03 2 days 2012-01-05</span>
<span class="gp">In [27]: </span><span class="n">df</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[27]: </span>
<span class="go">A datetime64[ns]</span>
<span class="go">B timedelta64[ns]</span>
<span class="go">C datetime64[ns]</span>
<span class="go">dtype: object</span>
<span class="gp">In [28]: </span><span class="n">s</span> <span class="o">-</span> <span class="n">s</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
<span class="gr">Out[28]: </span>
<span class="go">0 -2 days</span>
<span class="go">1 -1 days</span>
<span class="go">2 0 days</span>
<span class="go">dtype: timedelta64[ns]</span>
<span class="gp">In [29]: </span><span class="n">s</span> <span class="o">-</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2011</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="gr">Out[29]: </span>
<span class="go">0 364 days 20:55:00</span>
<span class="go">1 365 days 20:55:00</span>
<span class="go">2 366 days 20:55:00</span>
<span class="go">dtype: timedelta64[ns]</span>
<span class="gp">In [30]: </span><span class="n">s</span> <span class="o">+</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="gr">Out[30]: </span>
<span class="go">0 2012-01-01 00:05:00</span>
<span class="go">1 2012-01-02 00:05:00</span>
<span class="go">2 2012-01-03 00:05:00</span>
<span class="go">dtype: datetime64[ns]</span>
<span class="gp">In [31]: </span><span class="n">s</span> <span class="o">+</span> <span class="n">Minute</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gr">Out[31]: </span>
<span class="go">0 2012-01-01 00:05:00</span>
<span class="go">1 2012-01-02 00:05:00</span>
<span class="go">2 2012-01-03 00:05:00</span>
<span class="go">dtype: datetime64[ns]</span>
<span class="gp">In [32]: </span><span class="n">s</span> <span class="o">+</span> <span class="n">Minute</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="o">+</span> <span class="n">Milli</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gr">Out[32]: </span>
<span class="go">0 2012-01-01 00:05:00.005</span>
<span class="go">1 2012-01-02 00:05:00.005</span>
<span class="go">2 2012-01-03 00:05:00.005</span>
<span class="go">dtype: datetime64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-83">使用<code class="docutils literal"><span class="pre">timedelta64[ns]</span></code>系列的标量操作:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [33]: </span><span class="n">y</span> <span class="o">=</span> <span class="n">s</span> <span class="o">-</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="gp">In [34]: </span><span class="n">y</span>
<span class="gr">Out[34]: </span>
<span class="go">0 0 days</span>
<span class="go">1 1 days</span>
<span class="go">2 2 days</span>
<span class="go">dtype: timedelta64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-84">支持具有<code class="docutils literal"><span class="pre">NaT</span></code>值的timedeltas系列:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [35]: </span><span class="n">y</span> <span class="o">=</span> <span class="n">s</span> <span class="o">-</span> <span class="n">s</span><span class="o">.</span><span class="n">shift</span><span class="p">()</span>
<span class="gp">In [36]: </span><span class="n">y</span>
<span class="gr">Out[36]: </span>
<span class="go">0 NaT</span>
<span class="go">1 1 days</span>
<span class="go">2 1 days</span>
<span class="go">dtype: timedelta64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-85">可以使用<code class="docutils literal"><span class="pre">np.nan</span></code>将元素设置为<code class="docutils literal"><span class="pre">NaT</span></code>:类似于数据时间:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [37]: </span><span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="gp">In [38]: </span><span class="n">y</span>
<span class="gr">Out[38]: </span>
<span class="go">0 NaT</span>
<span class="go">1 NaT</span>
<span class="go">2 1 days</span>
<span class="go">dtype: timedelta64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-86">操作数也可以以相反的顺序出现(使用Series操作的单个对象):</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [39]: </span><span class="n">s</span><span class="o">.</span><span class="n">max</span><span class="p">()</span> <span class="o">-</span> <span class="n">s</span>
<span class="gr">Out[39]: </span>
<span class="go">0 2 days</span>
<span class="go">1 1 days</span>
<span class="go">2 0 days</span>
<span class="go">dtype: timedelta64[ns]</span>
<span class="gp">In [40]: </span><span class="n">datetime</span><span class="p">(</span><span class="mi">2011</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="o">-</span> <span class="n">s</span>
<span class="gr">Out[40]: </span>
<span class="go">0 -365 days +03:05:00</span>
<span class="go">1 -366 days +03:05:00</span>
<span class="go">2 -367 days +03:05:00</span>
<span class="go">dtype: timedelta64[ns]</span>
<span class="gp">In [41]: </span><span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span> <span class="o">+</span> <span class="n">s</span>
<span class="gr">Out[41]: </span>
<span class="go">0 2012-01-01 00:05:00</span>
<span class="go">1 2012-01-02 00:05:00</span>
<span class="go">2 2012-01-03 00:05:00</span>
<span class="go">dtype: datetime64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-87"><code class="docutils literal"><span class="pre">min,</span> <span class="pre">max</span></code>和对应的<code class="docutils literal"><span class="pre">idxmin,</span> <span class="pre">idxmax</span> t3 >在帧上支持操作:</code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [42]: </span><span class="n">A</span> <span class="o">=</span> <span class="n">s</span> <span class="o">-</span> <span class="n">Timestamp</span><span class="p">(</span><span class="s1">'20120101'</span><span class="p">)</span> <span class="o">-</span> <span class="n">Timedelta</span><span class="p">(</span><span class="s1">'00:05:05'</span><span class="p">)</span>
<span class="gp">In [43]: </span><span class="n">B</span> <span class="o">=</span> <span class="n">s</span> <span class="o">-</span> <span class="n">Series</span><span class="p">(</span><span class="n">date_range</span><span class="p">(</span><span class="s1">'2012-1-2'</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">'D'</span><span class="p">))</span>
<span class="gp">In [44]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">DataFrame</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">A</span><span class="o">=</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="o">=</span><span class="n">B</span><span class="p">))</span>
<span class="gp">In [45]: </span><span class="n">df</span>
<span class="gr">Out[45]: </span>
<span class="go"> A B</span>
<span class="go">0 -1 days +23:54:55 -1 days</span>
<span class="go">1 0 days 23:54:55 -1 days</span>
<span class="go">2 1 days 23:54:55 -1 days</span>
<span class="gp">In [46]: </span><span class="n">df</span><span class="o">.</span><span class="n">min</span><span class="p">()</span>
<span class="gr">Out[46]: </span>
<span class="go">A -1 days +23:54:55</span>
<span class="go">B -1 days +00:00:00</span>
<span class="go">dtype: timedelta64[ns]</span>
<span class="gp">In [47]: </span><span class="n">df</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[47]: </span>
<span class="go">0 -1 days</span>
<span class="go">1 -1 days</span>
<span class="go">2 -1 days</span>
<span class="go">dtype: timedelta64[ns]</span>
<span class="gp">In [48]: </span><span class="n">df</span><span class="o">.</span><span class="n">idxmin</span><span class="p">()</span>
<span class="gr">Out[48]: </span>
<span class="go">A 0</span>
<span class="go">B 0</span>
<span class="go">dtype: int64</span>
<span class="gp">In [49]: </span><span class="n">df</span><span class="o">.</span><span class="n">idxmax</span><span class="p">()</span>
<span class="gr">Out[49]: </span>
<span class="go">A 2</span>
<span class="go">B 0</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-88">在系列上也支持<code class="docutils literal"><span class="pre">min,</span> <span class="pre">max,</span> <span class="pre">idxmin,</span> <span class="pre">idxmax</span> </code></span><span class="yiyi-st" id="yiyi-89">标量结果将是<code class="docutils literal"><span class="pre">Timedelta</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [50]: </span><span class="n">df</span><span class="o">.</span><span class="n">min</span><span class="p">()</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
<span class="gr">Out[50]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-1 days +23:54:55'</span><span class="p">)</span>
<span class="gp">In [51]: </span><span class="n">df</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">min</span><span class="p">()</span>
<span class="gr">Out[51]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-1 days +00:00:00'</span><span class="p">)</span>
<span class="gp">In [52]: </span><span class="n">df</span><span class="o">.</span><span class="n">min</span><span class="p">()</span><span class="o">.</span><span class="n">idxmax</span><span class="p">()</span>
<span class="gr">Out[52]: </span><span class="s1">'A'</span>
<span class="gp">In [53]: </span><span class="n">df</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">idxmin</span><span class="p">()</span>
<span class="gr">Out[53]: </span><span class="mi">0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-90">你可以填写timedeltas。</span><span class="yiyi-st" id="yiyi-91">整数将被解释为秒。</span><span class="yiyi-st" id="yiyi-92">你可以传递timedelta来获得一个特定的值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [54]: </span><span class="n">y</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[54]: </span>
<span class="go">0 0 days</span>
<span class="go">1 0 days</span>
<span class="go">2 1 days</span>
<span class="go">dtype: timedelta64[ns]</span>
<span class="gp">In [55]: </span><span class="n">y</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="gr">Out[55]: </span>
<span class="go">0 0 days 00:00:10</span>
<span class="go">1 0 days 00:00:10</span>
<span class="go">2 1 days 00:00:00</span>
<span class="go">dtype: timedelta64[ns]</span>
<span class="gp">In [56]: </span><span class="n">y</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-1 days, 00:00:05'</span><span class="p">))</span>
<span class="gr">Out[56]: </span>
<span class="go">0 -1 days +00:00:05</span>
<span class="go">1 -1 days +00:00:05</span>
<span class="go">2 1 days 00:00:00</span>
<span class="go">dtype: timedelta64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-93">您还可以对<code class="docutils literal"><span class="pre">Timedeltas</span></code>进行否定,乘法和使用<code class="docutils literal"><span class="pre">abs</span></code>:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [57]: </span><span class="n">td1</span> <span class="o">=</span> <span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-1 days 2 hours 3 seconds'</span><span class="p">)</span>
<span class="gp">In [58]: </span><span class="n">td1</span>
<span class="gr">Out[58]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-2 days +21:59:57'</span><span class="p">)</span>
<span class="gp">In [59]: </span><span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="n">td1</span>
<span class="gr">Out[59]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 02:00:03'</span><span class="p">)</span>
<span class="gp">In [60]: </span><span class="o">-</span> <span class="n">td1</span>
<span class="gr">Out[60]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 02:00:03'</span><span class="p">)</span>
<span class="gp">In [61]: </span><span class="nb">abs</span><span class="p">(</span><span class="n">td1</span><span class="p">)</span>
<span class="gr">Out[61]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 02:00:03'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="reductions">
<span id="timedeltas-timedeltas-reductions"></span><h2><span class="yiyi-st" id="yiyi-94">Reductions</span></h2>
<p><span class="yiyi-st" id="yiyi-95">对<code class="docutils literal"><span class="pre">timedelta64[ns]</span></code>的数值缩减操作将返回<code class="docutils literal"><span class="pre">Timedelta</span></code>对象。</span><span class="yiyi-st" id="yiyi-96">像评估时一样,跳过<code class="docutils literal"><span class="pre">NaT</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [62]: </span><span class="n">y2</span> <span class="o">=</span> <span class="n">Series</span><span class="p">(</span><span class="n">to_timedelta</span><span class="p">([</span><span class="s1">'-1 days +00:00:05'</span><span class="p">,</span> <span class="s1">'nat'</span><span class="p">,</span> <span class="s1">'-1 days +00:00:05'</span><span class="p">,</span> <span class="s1">'1 days'</span><span class="p">]))</span>
<span class="gp">In [63]: </span><span class="n">y2</span>
<span class="gr">Out[63]: </span>
<span class="go">0 -1 days +00:00:05</span>
<span class="go">1 NaT</span>
<span class="go">2 -1 days +00:00:05</span>
<span class="go">3 1 days 00:00:00</span>
<span class="go">dtype: timedelta64[ns]</span>
<span class="gp">In [64]: </span><span class="n">y2</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[64]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-1 days +16:00:03.333333'</span><span class="p">)</span>
<span class="gp">In [65]: </span><span class="n">y2</span><span class="o">.</span><span class="n">median</span><span class="p">()</span>
<span class="gr">Out[65]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-1 days +00:00:05'</span><span class="p">)</span>
<span class="gp">In [66]: </span><span class="n">y2</span><span class="o">.</span><span class="n">quantile</span><span class="p">(</span><span class="o">.</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[66]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-1 days +00:00:05'</span><span class="p">)</span>
<span class="gp">In [67]: </span><span class="n">y2</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[67]: </span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'-1 days +00:00:10'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="frequency-conversion">
<span id="timedeltas-timedeltas-convert"></span><h2><span class="yiyi-st" id="yiyi-97">Frequency Conversion</span></h2>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-98"><span class="versionmodified">版本0.13中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-99">Timedelta系列,<code class="docutils literal"><span class="pre">TimedeltaIndex</span></code>和<code class="docutils literal"><span class="pre">Timedelta</span></code>标量可以通过除以另一个timedelta或由特定timedelta类型的astyping转换为其他“频率”。</span><span class="yiyi-st" id="yiyi-100">这些操作产生系列并传播<code class="docutils literal"><span class="pre">NaT</span></code> - > <code class="docutils literal"><span class="pre">nan</span></code>。</span><span class="yiyi-st" id="yiyi-101">注意,numpy标量除以真正除法,而astyping等效于floor除法。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [68]: </span><span class="n">td</span> <span class="o">=</span> <span class="n">Series</span><span class="p">(</span><span class="n">date_range</span><span class="p">(</span><span class="s1">'20130101'</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span> <span class="o">-</span> \
<span class="gp"> ....:</span> <span class="n">Series</span><span class="p">(</span><span class="n">date_range</span><span class="p">(</span><span class="s1">'20121201'</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="gp"> ....:</span>
<span class="gp">In [69]: </span><span class="n">td</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">seconds</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">In [70]: </span><span class="n">td</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="gp">In [71]: </span><span class="n">td</span>
<span class="gr">Out[71]: </span>
<span class="go">0 31 days 00:00:00</span>
<span class="go">1 31 days 00:00:00</span>
<span class="go">2 31 days 00:05:03</span>
<span class="go">3 NaT</span>
<span class="go">dtype: timedelta64[ns]</span>
<span class="c"># to days</span>
<span class="gp">In [72]: </span><span class="n">td</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s1">'D'</span><span class="p">)</span>
<span class="gr">Out[72]: </span>
<span class="go">0 31.000000</span>
<span class="go">1 31.000000</span>
<span class="go">2 31.003507</span>
<span class="go">3 NaN</span>
<span class="go">dtype: float64</span>
<span class="gp">In [73]: </span><span class="n">td</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">'timedelta64[D]'</span><span class="p">)</span>
<span class="gr">Out[73]: </span>
<span class="go">0 31.0</span>
<span class="go">1 31.0</span>
<span class="go">2 31.0</span>
<span class="go">3 NaN</span>
<span class="go">dtype: float64</span>
<span class="c"># to seconds</span>
<span class="gp">In [74]: </span><span class="n">td</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s1">'s'</span><span class="p">)</span>
<span class="gr">Out[74]: </span>
<span class="go">0 2678400.0</span>
<span class="go">1 2678400.0</span>
<span class="go">2 2678703.0</span>
<span class="go">3 NaN</span>
<span class="go">dtype: float64</span>
<span class="gp">In [75]: </span><span class="n">td</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">'timedelta64[s]'</span><span class="p">)</span>
<span class="gr">Out[75]: </span>
<span class="go">0 2678400.0</span>
<span class="go">1 2678400.0</span>
<span class="go">2 2678703.0</span>
<span class="go">3 NaN</span>
<span class="go">dtype: float64</span>
<span class="c"># to months (these are constant months)</span>
<span class="gp">In [76]: </span><span class="n">td</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s1">'M'</span><span class="p">)</span>
<span class="gr">Out[76]: </span>
<span class="go">0 1.018501</span>
<span class="go">1 1.018501</span>
<span class="go">2 1.018617</span>
<span class="go">3 NaN</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-102">将<code class="docutils literal"><span class="pre">timedelta64[ns]</span></code>系列除以整数或整数系列产生另一个<code class="docutils literal"><span class="pre">timedelta64[ns]</span></code> dtypes系列。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [77]: </span><span class="n">td</span> <span class="o">*</span> <span class="o">-</span><span class="mi">1</span>
<span class="gr">Out[77]: </span>
<span class="go">0 -31 days +00:00:00</span>
<span class="go">1 -31 days +00:00:00</span>
<span class="go">2 -32 days +23:54:57</span>
<span class="go">3 NaT</span>
<span class="go">dtype: timedelta64[ns]</span>
<span class="gp">In [78]: </span><span class="n">td</span> <span class="o">*</span> <span class="n">Series</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>
<span class="gr">Out[78]: </span>
<span class="go">0 31 days 00:00:00</span>
<span class="go">1 62 days 00:00:00</span>
<span class="go">2 93 days 00:15:09</span>
<span class="go">3 NaT</span>
<span class="go">dtype: timedelta64[ns]</span>
</pre></div>
</div>
</div>
<div class="section" id="attributes">
<h2><span class="yiyi-st" id="yiyi-103">Attributes</span></h2>
<p><span class="yiyi-st" id="yiyi-104">您可以使用属性<code class="docutils literal"><span class="pre">days,seconds,microseconds,nanoseconds</span></code>直接访问<code class="docutils literal"><span class="pre">Timedelta</span></code>或<code class="docutils literal"><span class="pre">TimedeltaIndex</span></code>的各种组件。</span><span class="yiyi-st" id="yiyi-105">这些与<code class="docutils literal"><span class="pre">datetime.timedelta</span></code>返回的值相同,例如,<code class="docutils literal"><span class="pre">.seconds</span></code>属性表示秒数> = 0,</span><span class="yiyi-st" id="yiyi-106">这些是根据<code class="docutils literal"><span class="pre">Timedelta</span></code>是否有符号来签名的。</span></p>
<p><span class="yiyi-st" id="yiyi-107">这些操作也可以通过<code class="docutils literal"><span class="pre">Series</span></code>的<code class="docutils literal"><span class="pre">.dt</span></code>属性直接访问。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-108">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-109">请注意,属性不是<code class="docutils literal"><span class="pre">Timedelta</span></code>的显示值。</span><span class="yiyi-st" id="yiyi-110">使用<code class="docutils literal"><span class="pre">.components</span></code>可检索显示的值。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-111">对于<code class="docutils literal"><span class="pre">Series</span></code>:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [79]: </span><span class="n">td</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">days</span>
<span class="gr">Out[79]: </span>
<span class="go">0 31.0</span>
<span class="go">1 31.0</span>
<span class="go">2 31.0</span>
<span class="go">3 NaN</span>
<span class="go">dtype: float64</span>
<span class="gp">In [80]: </span><span class="n">td</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">seconds</span>
<span class="gr">Out[80]: </span>
<span class="go">0 0.0</span>
<span class="go">1 0.0</span>
<span class="go">2 303.0</span>
<span class="go">3 NaN</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-112">您可以直接访问标量<code class="docutils literal"><span class="pre">Timedelta</span></code>的字段值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [81]: </span><span class="n">tds</span> <span class="o">=</span> <span class="n">Timedelta</span><span class="p">(</span><span class="s1">'31 days 5 min 3 sec'</span><span class="p">)</span>
<span class="gp">In [82]: </span><span class="n">tds</span><span class="o">.</span><span class="n">days</span>
<span class="gr">Out[82]: </span><span class="mi">31</span>
<span class="gp">In [83]: </span><span class="n">tds</span><span class="o">.</span><span class="n">seconds</span>
<span class="gr">Out[83]: </span><span class="mi">303</span>
<span class="gp">In [84]: </span><span class="p">(</span><span class="o">-</span><span class="n">tds</span><span class="p">)</span><span class="o">.</span><span class="n">seconds</span>
<span class="gr">Out[84]: </span><span class="mi">86097</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-113">您可以使用<code class="docutils literal"><span class="pre">.components</span></code>属性访问缩小形式的timedelta。</span><span class="yiyi-st" id="yiyi-114">这会返回类似于<code class="docutils literal"><span class="pre">Series</span></code>的<code class="docutils literal"><span class="pre">DataFrame</span></code>索引。</span><span class="yiyi-st" id="yiyi-115">这些是<code class="docutils literal"><span class="pre">Timedelta</span></code>的<em>显示的</em>值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [85]: </span><span class="n">td</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">components</span>
<span class="gr">Out[85]: </span>
<span class="go"> days hours minutes seconds milliseconds microseconds nanoseconds</span>
<span class="go">0 31.0 0.0 0.0 0.0 0.0 0.0 0.0</span>
<span class="go">1 31.0 0.0 0.0 0.0 0.0 0.0 0.0</span>
<span class="go">2 31.0 0.0 5.0 3.0 0.0 0.0 0.0</span>
<span class="go">3 NaN NaN NaN NaN NaN NaN NaN</span>
<span class="gp">In [86]: </span><span class="n">td</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">components</span><span class="o">.</span><span class="n">seconds</span>
<span class="gr">Out[86]: </span>
<span class="go">0 0.0</span>
<span class="go">1 0.0</span>
<span class="go">2 3.0</span>
<span class="go">3 NaN</span>
<span class="go">Name: seconds, dtype: float64</span>
</pre></div>
</div>
</div>
<div class="section" id="timedeltaindex">
<span id="timedeltas-index"></span><h2><span class="yiyi-st" id="yiyi-116">TimedeltaIndex</span></h2>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-117"><span class="versionmodified">版本0.15.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-118">要生成具有时间delta的索引,可以使用<code class="docutils literal"><span class="pre">TimedeltaIndex</span></code>或<code class="docutils literal"><span class="pre">timedelta_range</span></code>构造函数。</span></p>
<p><span class="yiyi-st" id="yiyi-119">使用<code class="docutils literal"><span class="pre">TimedeltaIndex</span></code>,您可以传递类似字符串,<code class="docutils literal"><span class="pre">Timedelta</span></code>,<code class="docutils literal"><span class="pre">timedelta</span></code>或<code class="docutils literal"><span class="pre">np.timedelta64</span></code>对象。</span><span class="yiyi-st" id="yiyi-120">传递<code class="docutils literal"><span class="pre">np.nan/pd.NaT/nat</span></code>将表示缺少的值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [87]: </span><span class="n">TimedeltaIndex</span><span class="p">([</span><span class="s1">'1 days'</span><span class="p">,</span> <span class="s1">'1 days, 00:00:05'</span><span class="p">,</span>
<span class="gp"> ....:</span> <span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="s1">'D'</span><span class="p">),</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span><span class="n">seconds</span><span class="o">=</span><span class="mi">2</span><span class="p">)])</span>
<span class="gp"> ....:</span>
<span class="gr">Out[87]: </span>
<span class="go">TimedeltaIndex(['1 days 00:00:00', '1 days 00:00:05', '2 days 00:00:00',</span>
<span class="go"> '2 days 00:00:02'],</span>
<span class="go"> dtype='timedelta64[ns]', freq=None)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-121">与<code class="docutils literal"><span class="pre">date_range</span></code>类似,您可以构建<code class="docutils literal"><span class="pre">TimedeltaIndex</span></code>的正常范围:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [88]: </span><span class="n">timedelta_range</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s1">'1 days'</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">'D'</span><span class="p">)</span>
<span class="gr">Out[88]: </span><span class="n">TimedeltaIndex</span><span class="p">([</span><span class="s1">'1 days'</span><span class="p">,</span> <span class="s1">'2 days'</span><span class="p">,</span> <span class="s1">'3 days'</span><span class="p">,</span> <span class="s1">'4 days'</span><span class="p">,</span> <span class="s1">'5 days'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'timedelta64[ns]'</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">'D'</span><span class="p">)</span>
<span class="gp">In [89]: </span><span class="n">timedelta_range</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s1">'1 days'</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">'2 days'</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">'30T'</span><span class="p">)</span>
<span class="gr">Out[89]: </span>
<span class="go">TimedeltaIndex(['1 days 00:00:00', '1 days 00:30:00', '1 days 01:00:00',</span>
<span class="go"> '1 days 01:30:00', '1 days 02:00:00', '1 days 02:30:00',</span>
<span class="go"> '1 days 03:00:00', '1 days 03:30:00', '1 days 04:00:00',</span>
<span class="go"> '1 days 04:30:00', '1 days 05:00:00', '1 days 05:30:00',</span>
<span class="go"> '1 days 06:00:00', '1 days 06:30:00', '1 days 07:00:00',</span>
<span class="go"> '1 days 07:30:00', '1 days 08:00:00', '1 days 08:30:00',</span>
<span class="go"> '1 days 09:00:00', '1 days 09:30:00', '1 days 10:00:00',</span>
<span class="go"> '1 days 10:30:00', '1 days 11:00:00', '1 days 11:30:00',</span>
<span class="go"> '1 days 12:00:00', '1 days 12:30:00', '1 days 13:00:00',</span>
<span class="go"> '1 days 13:30:00', '1 days 14:00:00', '1 days 14:30:00',</span>
<span class="go"> '1 days 15:00:00', '1 days 15:30:00', '1 days 16:00:00',</span>
<span class="go"> '1 days 16:30:00', '1 days 17:00:00', '1 days 17:30:00',</span>
<span class="go"> '1 days 18:00:00', '1 days 18:30:00', '1 days 19:00:00',</span>
<span class="go"> '1 days 19:30:00', '1 days 20:00:00', '1 days 20:30:00',</span>
<span class="go"> '1 days 21:00:00', '1 days 21:30:00', '1 days 22:00:00',</span>
<span class="go"> '1 days 22:30:00', '1 days 23:00:00', '1 days 23:30:00',</span>
<span class="go"> '2 days 00:00:00'],</span>
<span class="go"> dtype='timedelta64[ns]', freq='30T')</span>
</pre></div>
</div>
<div class="section" id="using-the-timedeltaindex">
<h3><span class="yiyi-st" id="yiyi-122">Using the TimedeltaIndex</span></h3>
<p><span class="yiyi-st" id="yiyi-123">类似于其他类datetime索引,<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>和<code class="docutils literal"><span class="pre">PeriodIndex</span></code>,您可以使用<code class="docutils literal"><span class="pre">TimedeltaIndex</span></code>作为pandas对象的索引。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [90]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">100</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="n">index</span><span class="o">=</span><span class="n">timedelta_range</span><span class="p">(</span><span class="s1">'1 days'</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">'h'</span><span class="p">))</span>
<span class="gp"> ....:</span>
<span class="gp">In [91]: </span><span class="n">s</span>
<span class="gr">Out[91]: </span>
<span class="go">1 days 00:00:00 0</span>
<span class="go">1 days 01:00:00 1</span>
<span class="go">1 days 02:00:00 2</span>
<span class="go">1 days 03:00:00 3</span>
<span class="go">1 days 04:00:00 4</span>
<span class="go">1 days 05:00:00 5</span>
<span class="go">1 days 06:00:00 6</span>
<span class="go"> ..</span>
<span class="go">4 days 21:00:00 93</span>
<span class="go">4 days 22:00:00 94</span>
<span class="go">4 days 23:00:00 95</span>
<span class="go">5 days 00:00:00 96</span>
<span class="go">5 days 01:00:00 97</span>
<span class="go">5 days 02:00:00 98</span>
<span class="go">5 days 03:00:00 99</span>
<span class="go">Freq: H, dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-124">选择工作类似,强制在字符串喜欢和切片:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [92]: </span><span class="n">s</span><span class="p">[</span><span class="s1">'1 day'</span><span class="p">:</span><span class="s1">'2 day'</span><span class="p">]</span>
<span class="gr">Out[92]: </span>
<span class="go">1 days 00:00:00 0</span>
<span class="go">1 days 01:00:00 1</span>
<span class="go">1 days 02:00:00 2</span>
<span class="go">1 days 03:00:00 3</span>
<span class="go">1 days 04:00:00 4</span>
<span class="go">1 days 05:00:00 5</span>
<span class="go">1 days 06:00:00 6</span>
<span class="go"> ..</span>
<span class="go">2 days 17:00:00 41</span>
<span class="go">2 days 18:00:00 42</span>
<span class="go">2 days 19:00:00 43</span>
<span class="go">2 days 20:00:00 44</span>
<span class="go">2 days 21:00:00 45</span>
<span class="go">2 days 22:00:00 46</span>
<span class="go">2 days 23:00:00 47</span>
<span class="go">Freq: H, dtype: int64</span>
<span class="gp">In [93]: </span><span class="n">s</span><span class="p">[</span><span class="s1">'1 day 01:00:00'</span><span class="p">]</span>
<span class="gr">Out[93]: </span><span class="mi">1</span>
<span class="gp">In [94]: </span><span class="n">s</span><span class="p">[</span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 day 1h'</span><span class="p">)]</span>
<span class="gr">Out[94]: </span><span class="mi">1</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-125">此外,您可以使用部分字符串选择,范围将被推断:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [95]: </span><span class="n">s</span><span class="p">[</span><span class="s1">'1 day'</span><span class="p">:</span><span class="s1">'1 day 5 hours'</span><span class="p">]</span>
<span class="gr">Out[95]: </span>
<span class="go">1 days 00:00:00 0</span>
<span class="go">1 days 01:00:00 1</span>
<span class="go">1 days 02:00:00 2</span>
<span class="go">1 days 03:00:00 3</span>
<span class="go">1 days 04:00:00 4</span>
<span class="go">1 days 05:00:00 5</span>
<span class="go">Freq: H, dtype: int64</span>
</pre></div>
</div>
</div>
<div class="section" id="id1">
<h3><span class="yiyi-st" id="yiyi-126">Operations</span></h3>
<p><span class="yiyi-st" id="yiyi-127">最后,<code class="docutils literal"><span class="pre">TimedeltaIndex</span></code>与<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>的组合允许某些NaT保留的组合操作:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [96]: </span><span class="n">tdi</span> <span class="o">=</span> <span class="n">TimedeltaIndex</span><span class="p">([</span><span class="s1">'1 days'</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">NaT</span><span class="p">,</span> <span class="s1">'2 days'</span><span class="p">])</span>
<span class="gp">In [97]: </span><span class="n">tdi</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="gr">Out[97]: </span><span class="p">[</span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">'1 days 00:00:00'</span><span class="p">),</span> <span class="n">NaT</span><span class="p">,</span> <span class="n">Timedelta</span><span class="p">(</span><span class="s1">'2 days 00:00:00'</span><span class="p">)]</span>
<span class="gp">In [98]: </span><span class="n">dti</span> <span class="o">=</span> <span class="n">date_range</span><span class="p">(</span><span class="s1">'20130101'</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">In [99]: </span><span class="n">dti</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="gr">Out[99]: </span>
<span class="go">[Timestamp('2013-01-01 00:00:00', freq='D'),</span>
<span class="go"> Timestamp('2013-01-02 00:00:00', freq='D'),</span>
<span class="go"> Timestamp('2013-01-03 00:00:00', freq='D')]</span>
<span class="gp">In [100]: </span><span class="p">(</span><span class="n">dti</span> <span class="o">+</span> <span class="n">tdi</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="gr">Out[100]: </span><span class="p">[</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2013-01-02 00:00:00'</span><span class="p">),</span> <span class="n">NaT</span><span class="p">,</span> <span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2013-01-05 00:00:00'</span><span class="p">)]</span>
<span class="gp">In [101]: </span><span class="p">(</span><span class="n">dti</span> <span class="o">-</span> <span class="n">tdi</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="gr">Out[101]: </span><span class="p">[</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2012-12-31 00:00:00'</span><span class="p">),</span> <span class="n">NaT</span><span class="p">,</span> <span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2013-01-01 00:00:00'</span><span class="p">)]</span>
</pre></div>
</div>
</div>
<div class="section" id="conversions">
<h3><span class="yiyi-st" id="yiyi-128">Conversions</span></h3>
<p><span class="yiyi-st" id="yiyi-129">与上述<code class="docutils literal"><span class="pre">Series</span></code>上的频率转换类似,您可以转换这些索引以产生另一个索引。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [102]: </span><span class="n">tdi</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="s1">'s'</span><span class="p">)</span>
<span class="gr">Out[102]: </span><span class="n">Float64Index</span><span class="p">([</span><span class="mf">86400.0</span><span class="p">,</span> <span class="n">nan</span><span class="p">,</span> <span class="mf">172800.0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'float64'</span><span class="p">)</span>
<span class="gp">In [103]: </span><span class="n">tdi</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">'timedelta64[s]'</span><span class="p">)</span>
<span class="gr">Out[103]: </span><span class="n">Float64Index</span><span class="p">([</span><span class="mf">86400.0</span><span class="p">,</span> <span class="n">nan</span><span class="p">,</span> <span class="mf">172800.0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'float64'</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-130">标量类型ops也工作。</span><span class="yiyi-st" id="yiyi-131">这些可能会返回<em>不同的</em>类型的索引。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># adding or timedelta and date -> datelike</span>
<span class="gp">In [104]: </span><span class="n">tdi</span> <span class="o">+</span> <span class="n">Timestamp</span><span class="p">(</span><span class="s1">'20130101'</span><span class="p">)</span>
<span class="gr">Out[104]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">'2013-01-02'</span><span class="p">,</span> <span class="s1">'NaT'</span><span class="p">,</span> <span class="s1">'2013-01-03'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'datetime64[ns]'</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
<span class="c"># subtraction of a date and a timedelta -> datelike</span>
<span class="c"># note that trying to subtract a date from a Timedelta will raise an exception</span>
<span class="gp">In [105]: </span><span class="p">(</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'20130101'</span><span class="p">)</span> <span class="o">-</span> <span class="n">tdi</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="gr">Out[105]: </span><span class="p">[</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2012-12-31 00:00:00'</span><span class="p">),</span> <span class="n">NaT</span><span class="p">,</span> <span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2012-12-30 00:00:00'</span><span class="p">)]</span>
<span class="c"># timedelta + timedelta -> timedelta</span>
<span class="gp">In [106]: </span><span class="n">tdi</span> <span class="o">+</span> <span class="n">Timedelta</span><span class="p">(</span><span class="s1">'10 days'</span><span class="p">)</span>
<span class="gr">Out[106]: </span><span class="n">TimedeltaIndex</span><span class="p">([</span><span class="s1">'11 days'</span><span class="p">,</span> <span class="n">NaT</span><span class="p">,</span> <span class="s1">'12 days'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'timedelta64[ns]'</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
<span class="c"># division can result in a Timedelta if the divisor is an integer</span>
<span class="gp">In [107]: </span><span class="n">tdi</span> <span class="o">/</span> <span class="mi">2</span>
<span class="gr">Out[107]: </span><span class="n">TimedeltaIndex</span><span class="p">([</span><span class="s1">'0 days 12:00:00'</span><span class="p">,</span> <span class="n">NaT</span><span class="p">,</span> <span class="s1">'1 days 00:00:00'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'timedelta64[ns]'</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
<span class="c"># or a Float64Index if the divisor is a Timedelta</span>
<span class="gp">In [108]: </span><span class="n">tdi</span> <span class="o">/</span> <span class="n">tdi</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="gr">Out[108]: </span><span class="n">Float64Index</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">nan</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'float64'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="resampling">
<span id="timedeltas-resampling"></span><h2><span class="yiyi-st" id="yiyi-132">Resampling</span></h2>
<p><span class="yiyi-st" id="yiyi-133">与<a class="reference internal" href="timeseries.html#timeseries-resampling"><span class="std std-ref">timeseries resampling</span></a>类似,我们可以使用<code class="docutils literal"><span class="pre">TimedeltaIndex</span></code>重新采样。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [109]: </span><span class="n">s</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s1">'D'</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[109]: </span>
<span class="go">1 days 11.5</span>
<span class="go">2 days 35.5</span>
<span class="go">3 days 59.5</span>
<span class="go">4 days 83.5</span>
<span class="go">5 days 97.5</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
</div>