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
/
timeseries.html
3133 lines (2850 loc) · 341 KB
/
timeseries.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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<span id="timeseries"></span><h1><span class="yiyi-st" id="yiyi-91">Time Series / Date functionality</span></h1>
<blockquote>
<p>原文:<a href="http://pandas.pydata.org/pandas-docs/stable/timeseries.html">http://pandas.pydata.org/pandas-docs/stable/timeseries.html</a></p>
<p>译者:<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
<p>校对:(虚位以待)</p>
</blockquote>
<p><span class="yiyi-st" id="yiyi-92">pandas已经证明非常成功地作为用于处理时间序列数据的工具,特别是在金融数据分析空间中。</span><span class="yiyi-st" id="yiyi-93">使用NumPy <code class="docutils literal"><span class="pre">datetime64</span></code>和<code class="docutils literal"><span class="pre">timedelta64</span></code> dtypes,我们整合了大量来自其他Python库的特性,例如<code class="docutils literal"><span class="pre">scikits.timeseries</span></code>大量的用于操作时间序列数据的新功能。</span></p>
<p><span class="yiyi-st" id="yiyi-94">在处理时间序列数据时,我们将经常寻求:</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-95">生成固定频率日期和时间跨度的序列</span></li>
<li><span class="yiyi-st" id="yiyi-96">将时间序列整合或转换为特定频率</span></li>
<li><span class="yiyi-st" id="yiyi-97">基于各种非标准时间增量(例如,一年的最后一个营业日之前的5个工作日)或“前滚”日期向前或向后计算“相对”日期</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-98">pandas提供用于执行上述任务的相对紧凑和自足的工具集合。</span></p>
<p><span class="yiyi-st" id="yiyi-99">创建日期范围:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># 72 hours starting with midnight Jan 1st, 2011</span>
<span class="gp">In [1]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">'1/1/2011'</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">72</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">In [2]: </span><span class="n">rng</span><span class="p">[:</span><span class="mi">5</span><span class="p">]</span>
<span class="gr">Out[2]: </span>
<span class="go">DatetimeIndex(['2011-01-01 00:00:00', '2011-01-01 01:00:00',</span>
<span class="go"> '2011-01-01 02:00:00', '2011-01-01 03:00:00',</span>
<span class="go"> '2011-01-01 04:00:00'],</span>
<span class="go"> dtype='datetime64[ns]', freq='H')</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-100">使用日期索引pandas对象:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [3]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</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">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">rng</span><span class="p">)),</span> <span class="n">index</span><span class="o">=</span><span class="n">rng</span><span class="p">)</span>
<span class="gp">In [4]: </span><span class="n">ts</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="gr">Out[4]: </span>
<span class="go">2011-01-01 00:00:00 0.469112</span>
<span class="go">2011-01-01 01:00:00 -0.282863</span>
<span class="go">2011-01-01 02:00:00 -1.509059</span>
<span class="go">2011-01-01 03:00:00 -1.135632</span>
<span class="go">2011-01-01 04:00:00 1.212112</span>
<span class="go">Freq: H, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-101">更改频率和填充间隙:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># to 45 minute frequency and forward fill</span>
<span class="gp">In [5]: </span><span class="n">converted</span> <span class="o">=</span> <span class="n">ts</span><span class="o">.</span><span class="n">asfreq</span><span class="p">(</span><span class="s1">'45Min'</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">'pad'</span><span class="p">)</span>
<span class="gp">In [6]: </span><span class="n">converted</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="gr">Out[6]: </span>
<span class="go">2011-01-01 00:00:00 0.469112</span>
<span class="go">2011-01-01 00:45:00 0.469112</span>
<span class="go">2011-01-01 01:30:00 -0.282863</span>
<span class="go">2011-01-01 02:15:00 -1.509059</span>
<span class="go">2011-01-01 03:00:00 -1.135632</span>
<span class="go">Freq: 45T, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-102">重新取样:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># Daily means</span>
<span class="gp">In [7]: </span><span class="n">ts</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[7]: </span>
<span class="go">2011-01-01 -0.319569</span>
<span class="go">2011-01-02 -0.337703</span>
<span class="go">2011-01-03 0.117258</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<div class="section" id="overview">
<span id="timeseries-overview"></span><h2><span class="yiyi-st" id="yiyi-103">Overview</span></h2>
<p><span class="yiyi-st" id="yiyi-104">下表显示了pandas可以处理的时间相关类的类型以及如何创建它们。</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="17%">
<col width="32%">
<col width="51%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-105">类</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-106">备注</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-107">如何创建</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-108"><code class="docutils literal"><span class="pre">Timestamp</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-109">时刻数据</span></td>
<td><span class="yiyi-st" id="yiyi-110"><code class="docutils literal"><span class="pre">to_datetime</span></code>,<code class="docutils literal"><span class="pre">Timestamp</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-111"><code class="docutils literal"><span class="pre">DatetimeIndex</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-112"><code class="docutils literal"><span class="pre">Timestamp</span></code>的索引</span></td>
<td><span class="yiyi-st" id="yiyi-113"><code class="docutils literal"><span class="pre">to_datetime</span></code>,<code class="docutils literal"><span class="pre">date_range</span></code>,<code class="docutils literal"><span class="pre">DatetimeIndex</span></code></span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-114"><code class="docutils literal"><span class="pre">Period</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-115">时期数据</span></td>
<td><span class="yiyi-st" id="yiyi-116"><code class="docutils literal"><span class="pre">Period</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-117"><code class="docutils literal"><span class="pre">PeriodIndex</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-118"><code class="docutils literal"><span class="pre">Period</span></code>的索引</span></td>
<td><span class="yiyi-st" id="yiyi-119"><code class="docutils literal"><span class="pre">period_range</span></code>,<code class="docutils literal"><span class="pre">PeriodIndex</span></code></span></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="time-stamps-vs-time-spans">
<span id="timeseries-representation"></span><h2><span class="yiyi-st" id="yiyi-120">Time Stamps vs. Time Spans</span></h2>
<p><span class="yiyi-st" id="yiyi-121">时间戳数据是将值与时间点相关联的最基本类型的时间序列数据。</span><span class="yiyi-st" id="yiyi-122">对于熊猫对象,它意味着使用时间点。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [8]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="gr">Out[8]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2012-05-01 00:00:00'</span><span class="p">)</span>
<span class="gp">In [9]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2012-05-01'</span><span class="p">)</span>
<span class="gr">Out[9]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2012-05-01 00:00:00'</span><span class="p">)</span>
<span class="gp">In [10]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[10]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2012-05-01 00:00:00'</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-123">但是,在许多情况下,更像是将变化变量与时间范围相关联。</span><span class="yiyi-st" id="yiyi-124">由<code class="docutils literal"><span class="pre">Period</span></code>表示的范围可以明确指定,也可以从datetime字符串格式推断。</span></p>
<p><span class="yiyi-st" id="yiyi-125">例如:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [11]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">'2011-01'</span><span class="p">)</span>
<span class="gr">Out[11]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">'2011-01'</span><span class="p">,</span> <span class="s1">'M'</span><span class="p">)</span>
<span class="gp">In [12]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">'2012-05'</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[12]: </span><span class="n">Period</span><span class="p">(</span><span class="s1">'2012-05-01'</span><span class="p">,</span> <span class="s1">'D'</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-126"><code class="docutils literal"><span class="pre">Timestamp</span></code>和<code class="docutils literal"><span class="pre">Period</span></code>可以是索引。</span><span class="yiyi-st" id="yiyi-127"><code class="docutils literal"><span class="pre">Timestamp</span></code>和<code class="docutils literal"><span class="pre">Period</span></code>的列表将分别自动强制为<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>和<code class="docutils literal"><span class="pre">PeriodIndex</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [13]: </span><span class="n">dates</span> <span class="o">=</span> <span class="p">[</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2012-05-01'</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2012-05-02'</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2012-05-03'</span><span class="p">)]</span>
<span class="gp">In [14]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</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">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">dates</span><span class="p">)</span>
<span class="gp">In [15]: </span><span class="nb">type</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">index</span><span class="p">)</span>
<span class="gr">Out[15]: </span><span class="n">pandas</span><span class="o">.</span><span class="n">tseries</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">DatetimeIndex</span>
<span class="gp">In [16]: </span><span class="n">ts</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[16]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">'2012-05-01'</span><span class="p">,</span> <span class="s1">'2012-05-02'</span><span class="p">,</span> <span class="s1">'2012-05-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="gp">In [17]: </span><span class="n">ts</span>
<span class="gr">Out[17]: </span>
<span class="go">2012-05-01 -0.410001</span>
<span class="go">2012-05-02 -0.078638</span>
<span class="go">2012-05-03 0.545952</span>
<span class="go">dtype: float64</span>
<span class="gp">In [18]: </span><span class="n">periods</span> <span class="o">=</span> <span class="p">[</span><span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">'2012-01'</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">'2012-02'</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">Period</span><span class="p">(</span><span class="s1">'2012-03'</span><span class="p">)]</span>
<span class="gp">In [19]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</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">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">periods</span><span class="p">)</span>
<span class="gp">In [20]: </span><span class="nb">type</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">index</span><span class="p">)</span>
<span class="gr">Out[20]: </span><span class="n">pandas</span><span class="o">.</span><span class="n">tseries</span><span class="o">.</span><span class="n">period</span><span class="o">.</span><span class="n">PeriodIndex</span>
<span class="gp">In [21]: </span><span class="n">ts</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[21]: </span><span class="n">PeriodIndex</span><span class="p">([</span><span class="s1">'2012-01'</span><span class="p">,</span> <span class="s1">'2012-02'</span><span class="p">,</span> <span class="s1">'2012-03'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'period[M]'</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">'M'</span><span class="p">)</span>
<span class="gp">In [22]: </span><span class="n">ts</span>
<span class="gr">Out[22]: </span>
<span class="go">2012-01 -1.219217</span>
<span class="go">2012-02 -1.226825</span>
<span class="go">2012-03 0.769804</span>
<span class="go">Freq: M, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-128">pandas允许你捕获表示和它们之间的转换。</span><span class="yiyi-st" id="yiyi-129">在内部,pandas表示使用<code class="docutils literal"><span class="pre">Timestamp</span></code>的实例的时间戳和使用<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>的实例的时间戳序列。</span><span class="yiyi-st" id="yiyi-130">对于常规时间范围,pandas对于标量值使用<code class="docutils literal"><span class="pre">Period</span></code>对象,对于跨度序列使用<code class="docutils literal"><span class="pre">PeriodIndex</span></code>。</span><span class="yiyi-st" id="yiyi-131">更好地支持具有任意起点和终点的不规则间隔将在未来版本中出现。</span></p>
</div>
<div class="section" id="converting-to-timestamps">
<span id="timeseries-converting"></span><h2><span class="yiyi-st" id="yiyi-132">Converting to Timestamps</span></h2>
<p><span class="yiyi-st" id="yiyi-133">要转换类似日期的对象的系列或列表对象,例如字符串,历元或混合,您可以使用<code class="docutils literal"><span class="pre">to_datetime</span></code>函数。</span><span class="yiyi-st" id="yiyi-134">当传递一个Series时,它返回一个Series(具有相同的索引),而类似列表被转换为DatetimeIndex:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [23]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">'Jul 31, 2009'</span><span class="p">,</span> <span class="s1">'2010-01-10'</span><span class="p">,</span> <span class="bp">None</span><span class="p">]))</span>
<span class="gr">Out[23]: </span>
<span class="go">0 2009-07-31</span>
<span class="go">1 2010-01-10</span>
<span class="go">2 NaT</span>
<span class="go">dtype: datetime64[ns]</span>
<span class="gp">In [24]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="s1">'2005/11/23'</span><span class="p">,</span> <span class="s1">'2010.12.31'</span><span class="p">])</span>
<span class="gr">Out[24]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">'2005-11-23'</span><span class="p">,</span> <span class="s1">'2010-12-31'</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>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-135">如果您使用以日期开头的日期(例如欧式样式),则可以传递<code class="docutils literal"><span class="pre">dayfirst</span></code>旗标:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [25]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="s1">'04-01-2012 10:00'</span><span class="p">],</span> <span class="n">dayfirst</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[25]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">'2012-01-04 10:00:00'</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="gp">In [26]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="s1">'14-01-2012'</span><span class="p">,</span> <span class="s1">'01-14-2012'</span><span class="p">],</span> <span class="n">dayfirst</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[26]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">'2012-01-14'</span><span class="p">,</span> <span class="s1">'2012-01-14'</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>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-136">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-137">您在上面的示例中看到<code class="docutils literal"><span class="pre">dayfirst</span></code>不是严格的,因此如果无法解析日期,那么将解析为<code class="docutils literal"><span class="pre">dayfirst</span></code>为False 。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-138">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-139">指定<code class="docutils literal"><span class="pre">format</span></code>参数可能会显着加快转换速度,而在稍后的版本中,0.13.0明确指定格式字符串'%Y%m%d'需要更快的路径。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-140">如果将单个字符串传递到<code class="docutils literal"><span class="pre">to_datetime</span></code>,则它将返回单个<code class="docutils literal"><span class="pre">Timestamp</span></code>。</span><span class="yiyi-st" id="yiyi-141">此外,<code class="docutils literal"><span class="pre">Timestamp</span></code>可以接受字符串输入。</span><span class="yiyi-st" id="yiyi-142">请注意,<code class="docutils literal"><span class="pre">Timestamp</span></code>不接受像<code class="docutils literal"><span class="pre">dayfirst</span></code>或<code class="docutils literal"><span class="pre">format</span></code>的字符串解析选项,如果需要,请使用<code class="docutils literal"><span class="pre">to_datetime</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [27]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="s1">'2010/11/12'</span><span class="p">)</span>
<span class="gr">Out[27]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2010-11-12 00:00:00'</span><span class="p">)</span>
<span class="gp">In [28]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2010/11/12'</span><span class="p">)</span>
<span class="gr">Out[28]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2010-11-12 00:00:00'</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-143"><span class="versionmodified">版本0.18.1中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-144">您还可以传递整数或字符串列的<code class="docutils literal"><span class="pre">DataFrame</span></code>,以组合到<code class="docutils literal"><span class="pre">Timestamps</span></code>的<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 [29]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">'year'</span><span class="p">:</span> <span class="p">[</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">2016</span><span class="p">],</span>
<span class="gp"> ....:</span> <span class="s1">'month'</span><span class="p">:</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="gp"> ....:</span> <span class="s1">'day'</span><span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span>
<span class="gp"> ....:</span> <span class="s1">'hour'</span><span class="p">:</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="gp"> ....:</span>
<span class="gp">In [30]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="gr">Out[30]: </span>
<span class="go">0 2015-02-04 02:00:00</span>
<span class="go">1 2016-03-05 03:00:00</span>
<span class="go">dtype: datetime64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-145">您只能传递需要汇编的列。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [31]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">df</span><span class="p">[[</span><span class="s1">'year'</span><span class="p">,</span> <span class="s1">'month'</span><span class="p">,</span> <span class="s1">'day'</span><span class="p">]])</span>
<span class="gr">Out[31]: </span>
<span class="go">0 2015-02-04</span>
<span class="go">1 2016-03-05</span>
<span class="go">dtype: datetime64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-146"><code class="docutils literal"><span class="pre">pd.to_datetime</span></code>在列名称中查找datetime组件的标准名称,包括:</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-147">必需:<code class="docutils literal"><span class="pre">year</span></code>,<code class="docutils literal"><span class="pre">month</span></code>,<code class="docutils literal"><span class="pre">day</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-148">optional: <code class="docutils literal"><span class="pre">hour</span></code>, <code class="docutils literal"><span class="pre">minute</span></code>, <code class="docutils literal"><span class="pre">second</span></code>, <code class="docutils literal"><span class="pre">millisecond</span></code>, <code class="docutils literal"><span class="pre">microsecond</span></code>, <code class="docutils literal"><span class="pre">nanosecond</span></code></span></li>
</ul>
<div class="section" id="invalid-data">
<h3><span class="yiyi-st" id="yiyi-149">Invalid Data</span></h3>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-150">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-151">在版本0.17.0中,<code class="docutils literal"><span class="pre">to_datetime</span></code>的默认值现在为<code class="docutils literal"><span class="pre">errors='raise'</span></code>,而不是<code class="docutils literal"><span class="pre">errors='ignore'</span></code>。</span><span class="yiyi-st" id="yiyi-152">这意味着无效的解析将提升,而返回原始输入,如在以前的版本。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-153">通过<code class="docutils literal"><span class="pre">errors='coerce'</span></code>将无效数据转换为<code class="docutils literal"><span class="pre">NaT</span></code>(不是时间):</span></p>
<p><span class="yiyi-st" id="yiyi-154">当不可解析时报错,这是默认值</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [2]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="s1">'2009/07/31'</span><span class="p">,</span> <span class="s1">'asd'</span><span class="p">],</span> <span class="n">errors</span><span class="o">=</span><span class="s1">'raise'</span><span class="p">)</span>
<span class="go">ValueError: Unknown string format</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-155">不可解析时返回原始输入</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="s1">'2009/07/31'</span><span class="p">,</span> <span class="s1">'asd'</span><span class="p">],</span> <span class="n">errors</span><span class="o">=</span><span class="s1">'ignore'</span><span class="p">)</span>
<span class="gr">Out[4]: </span><span class="n">array</span><span class="p">([</span><span class="s1">'2009/07/31'</span><span class="p">,</span> <span class="s1">'asd'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">object</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-156">不可扩展时返回NaT输入</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [6]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="s1">'2009/07/31'</span><span class="p">,</span> <span class="s1">'asd'</span><span class="p">],</span> <span class="n">errors</span><span class="o">=</span><span class="s1">'coerce'</span><span class="p">)</span>
<span class="gr">Out[6]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">'2009-07-31'</span><span class="p">,</span> <span class="s1">'NaT'</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>
</pre></div>
</div>
</div>
<div class="section" id="epoch-timestamps">
<h3><span class="yiyi-st" id="yiyi-157">Epoch Timestamps</span></h3>
<p><span class="yiyi-st" id="yiyi-158">它也可以转换整数或浮点时代。</span><span class="yiyi-st" id="yiyi-159">这些的默认单位是纳秒(因为这些是如何存储<code class="docutils literal"><span class="pre">Timestamp</span></code>的)。</span><span class="yiyi-st" id="yiyi-160">然而,经常将时期存储在另一个可以指定的<code class="docutils literal"><span class="pre">unit</span></code>中:</span></p>
<p><span class="yiyi-st" id="yiyi-161">典型的时期存储单位</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [32]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="mi">1349720105</span><span class="p">,</span> <span class="mi">1349806505</span><span class="p">,</span> <span class="mi">1349892905</span><span class="p">,</span>
<span class="gp"> ....:</span> <span class="mi">1349979305</span><span class="p">,</span> <span class="mi">1350065705</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="gp"> ....:</span>
<span class="gr">Out[32]: </span>
<span class="go">DatetimeIndex(['2012-10-08 18:15:05', '2012-10-09 18:15:05',</span>
<span class="go"> '2012-10-10 18:15:05', '2012-10-11 18:15:05',</span>
<span class="go"> '2012-10-12 18:15:05'],</span>
<span class="go"> dtype='datetime64[ns]', freq=None)</span>
<span class="gp">In [33]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="mi">1349720105100</span><span class="p">,</span> <span class="mi">1349720105200</span><span class="p">,</span> <span class="mi">1349720105300</span><span class="p">,</span>
<span class="gp"> ....:</span> <span class="mi">1349720105400</span><span class="p">,</span> <span class="mi">1349720105500</span> <span class="p">],</span> <span class="n">unit</span><span class="o">=</span><span class="s1">'ms'</span><span class="p">)</span>
<span class="gp"> ....:</span>
<span class="gr">Out[33]: </span>
<span class="go">DatetimeIndex(['2012-10-08 18:15:05.100000', '2012-10-08 18:15:05.200000',</span>
<span class="go"> '2012-10-08 18:15:05.300000', '2012-10-08 18:15:05.400000',</span>
<span class="go"> '2012-10-08 18:15:05.500000'],</span>
<span class="go"> dtype='datetime64[ns]', freq=None)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-162">这些<em>工作</em>,但结果可能是意外的。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [34]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="mi">1</span><span class="p">])</span>
<span class="gr">Out[34]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">'1970-01-01 00:00:00.000000001'</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="gp">In [35]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mf">3.14</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[35]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">'1970-01-01 00:00:01'</span><span class="p">,</span> <span class="s1">'1970-01-01 00:00:03.140000'</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>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-163">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-164">大纪元倍数将舍入到最接近的纳秒。</span></p>
</div>
</div>
</div>
<div class="section" id="generating-ranges-of-timestamps">
<span id="timeseries-daterange"></span><h2><span class="yiyi-st" id="yiyi-165">Generating Ranges of Timestamps</span></h2>
<p><span class="yiyi-st" id="yiyi-166">要生成带有时间戳的索引,可以使用DatetimeIndex或Index构造函数,并传入datetime对象列表:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [36]: </span><span class="n">dates</span> <span class="o">=</span> <span class="p">[</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2012</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">)]</span>
<span class="c"># Note the frequency information</span>
<span class="gp">In [37]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DatetimeIndex</span><span class="p">(</span><span class="n">dates</span><span class="p">)</span>
<span class="gp">In [38]: </span><span class="n">index</span>
<span class="gr">Out[38]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">'2012-05-01'</span><span class="p">,</span> <span class="s1">'2012-05-02'</span><span class="p">,</span> <span class="s1">'2012-05-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"># Automatically converted to DatetimeIndex</span>
<span class="gp">In [39]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">(</span><span class="n">dates</span><span class="p">)</span>
<span class="gp">In [40]: </span><span class="n">index</span>
<span class="gr">Out[40]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">'2012-05-01'</span><span class="p">,</span> <span class="s1">'2012-05-02'</span><span class="p">,</span> <span class="s1">'2012-05-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>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-167">实际上,这变得非常麻烦,因为我们经常需要具有大量时间戳的非常长的索引。</span><span class="yiyi-st" id="yiyi-168">如果我们需要在正常频率上的时间戳,我们可以使用pandas函数<code class="docutils literal"><span class="pre">date_range</span></code>和<code class="docutils literal"><span class="pre">bdate_range</span></code>来创建时间戳索引。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [41]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">'2000-1-1'</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">'M'</span><span class="p">)</span>
<span class="gp">In [42]: </span><span class="n">index</span>
<span class="gr">Out[42]: </span>
<span class="go">DatetimeIndex(['2000-01-31', '2000-02-29', '2000-03-31', '2000-04-30',</span>
<span class="go"> '2000-05-31', '2000-06-30', '2000-07-31', '2000-08-31',</span>
<span class="go"> '2000-09-30', '2000-10-31',</span>
<span class="go"> ...</span>
<span class="go"> '2082-07-31', '2082-08-31', '2082-09-30', '2082-10-31',</span>
<span class="go"> '2082-11-30', '2082-12-31', '2083-01-31', '2083-02-28',</span>
<span class="go"> '2083-03-31', '2083-04-30'],</span>
<span class="go"> dtype='datetime64[ns]', length=1000, freq='M')</span>
<span class="gp">In [43]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">bdate_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">250</span><span class="p">)</span>
<span class="gp">In [44]: </span><span class="n">index</span>
<span class="gr">Out[44]: </span>
<span class="go">DatetimeIndex(['2012-01-02', '2012-01-03', '2012-01-04', '2012-01-05',</span>
<span class="go"> '2012-01-06', '2012-01-09', '2012-01-10', '2012-01-11',</span>
<span class="go"> '2012-01-12', '2012-01-13',</span>
<span class="go"> ...</span>
<span class="go"> '2012-12-03', '2012-12-04', '2012-12-05', '2012-12-06',</span>
<span class="go"> '2012-12-07', '2012-12-10', '2012-12-11', '2012-12-12',</span>
<span class="go"> '2012-12-13', '2012-12-14'],</span>
<span class="go"> dtype='datetime64[ns]', length=250, freq='B')</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-169">类似<code class="docutils literal"><span class="pre">date_range</span></code>和<code class="docutils literal"><span class="pre">bdate_range</span></code>的便利功能使用各种频率别名。</span><span class="yiyi-st" id="yiyi-170"><code class="docutils literal"><span class="pre">date_range</span></code>的默认频率为<strong>日历日</strong>,而<code class="docutils literal"><span class="pre">bdate_range</span></code>的默认频率为<strong>工作日</strong></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [45]: </span><span class="n">start</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="gp">In [46]: </span><span class="n">end</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2012</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="gp">In [47]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">)</span>
<span class="gp">In [48]: </span><span class="n">rng</span>
<span class="gr">Out[48]: </span>
<span class="go">DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03', '2011-01-04',</span>
<span class="go"> '2011-01-05', '2011-01-06', '2011-01-07', '2011-01-08',</span>
<span class="go"> '2011-01-09', '2011-01-10',</span>
<span class="go"> ...</span>
<span class="go"> '2011-12-23', '2011-12-24', '2011-12-25', '2011-12-26',</span>
<span class="go"> '2011-12-27', '2011-12-28', '2011-12-29', '2011-12-30',</span>
<span class="go"> '2011-12-31', '2012-01-01'],</span>
<span class="go"> dtype='datetime64[ns]', length=366, freq='D')</span>
<span class="gp">In [49]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">bdate_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">)</span>
<span class="gp">In [50]: </span><span class="n">rng</span>
<span class="gr">Out[50]: </span>
<span class="go">DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06',</span>
<span class="go"> '2011-01-07', '2011-01-10', '2011-01-11', '2011-01-12',</span>
<span class="go"> '2011-01-13', '2011-01-14',</span>
<span class="go"> ...</span>
<span class="go"> '2011-12-19', '2011-12-20', '2011-12-21', '2011-12-22',</span>
<span class="go"> '2011-12-23', '2011-12-26', '2011-12-27', '2011-12-28',</span>
<span class="go"> '2011-12-29', '2011-12-30'],</span>
<span class="go"> dtype='datetime64[ns]', length=260, freq='B')</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-171"><code class="docutils literal"><span class="pre">date_range</span></code>和<code class="docutils literal"><span class="pre">bdate_range</span></code>可以使用<code class="docutils literal"><span class="pre">start</span></code>,<code class="docutils literal"><span class="pre">end</span></code>等参数的各种组合轻松生成日期范围, <code class="docutils literal"><span class="pre">periods</span></code>和<code class="docutils literal"><span class="pre">freq</span></code>:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [51]: </span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">'BM'</span><span class="p">)</span>
<span class="gr">Out[51]: </span>
<span class="go">DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29',</span>
<span class="go"> '2011-05-31', '2011-06-30', '2011-07-29', '2011-08-31',</span>
<span class="go"> '2011-09-30', '2011-10-31', '2011-11-30', '2011-12-30'],</span>
<span class="go"> dtype='datetime64[ns]', freq='BM')</span>
<span class="gp">In [52]: </span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">'W'</span><span class="p">)</span>
<span class="gr">Out[52]: </span>
<span class="go">DatetimeIndex(['2011-01-02', '2011-01-09', '2011-01-16', '2011-01-23',</span>
<span class="go"> '2011-01-30', '2011-02-06', '2011-02-13', '2011-02-20',</span>
<span class="go"> '2011-02-27', '2011-03-06', '2011-03-13', '2011-03-20',</span>
<span class="go"> '2011-03-27', '2011-04-03', '2011-04-10', '2011-04-17',</span>
<span class="go"> '2011-04-24', '2011-05-01', '2011-05-08', '2011-05-15',</span>
<span class="go"> '2011-05-22', '2011-05-29', '2011-06-05', '2011-06-12',</span>
<span class="go"> '2011-06-19', '2011-06-26', '2011-07-03', '2011-07-10',</span>
<span class="go"> '2011-07-17', '2011-07-24', '2011-07-31', '2011-08-07',</span>
<span class="go"> '2011-08-14', '2011-08-21', '2011-08-28', '2011-09-04',</span>
<span class="go"> '2011-09-11', '2011-09-18', '2011-09-25', '2011-10-02',</span>
<span class="go"> '2011-10-09', '2011-10-16', '2011-10-23', '2011-10-30',</span>
<span class="go"> '2011-11-06', '2011-11-13', '2011-11-20', '2011-11-27',</span>
<span class="go"> '2011-12-04', '2011-12-11', '2011-12-18', '2011-12-25',</span>
<span class="go"> '2012-01-01'],</span>
<span class="go"> dtype='datetime64[ns]', freq='W-SUN')</span>
<span class="gp">In [53]: </span><span class="n">pd</span><span class="o">.</span><span class="n">bdate_range</span><span class="p">(</span><span class="n">end</span><span class="o">=</span><span class="n">end</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
<span class="gr">Out[53]: </span>
<span class="go">DatetimeIndex(['2011-12-05', '2011-12-06', '2011-12-07', '2011-12-08',</span>
<span class="go"> '2011-12-09', '2011-12-12', '2011-12-13', '2011-12-14',</span>
<span class="go"> '2011-12-15', '2011-12-16', '2011-12-19', '2011-12-20',</span>
<span class="go"> '2011-12-21', '2011-12-22', '2011-12-23', '2011-12-26',</span>
<span class="go"> '2011-12-27', '2011-12-28', '2011-12-29', '2011-12-30'],</span>
<span class="go"> dtype='datetime64[ns]', freq='B')</span>
<span class="gp">In [54]: </span><span class="n">pd</span><span class="o">.</span><span class="n">bdate_range</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="n">start</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
<span class="gr">Out[54]: </span>
<span class="go">DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06',</span>
<span class="go"> '2011-01-07', '2011-01-10', '2011-01-11', '2011-01-12',</span>
<span class="go"> '2011-01-13', '2011-01-14', '2011-01-17', '2011-01-18',</span>
<span class="go"> '2011-01-19', '2011-01-20', '2011-01-21', '2011-01-24',</span>
<span class="go"> '2011-01-25', '2011-01-26', '2011-01-27', '2011-01-28'],</span>
<span class="go"> dtype='datetime64[ns]', freq='B')</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-172">开始和结束日期严格包含。</span><span class="yiyi-st" id="yiyi-173">因此,如果指定,则不会生成这些日期之外的任何日期。</span></p>
</div>
<div class="section" id="timestamp-limitations">
<span id="timeseries-timestamp-limits"></span><h2><span class="yiyi-st" id="yiyi-174">Timestamp limitations</span></h2>
<p><span class="yiyi-st" id="yiyi-175">由于pandas表示纳秒分辨率的时间戳,可以使用64位整数表示的时间间隔限制为大约584年:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [55]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="o">.</span><span class="n">min</span>
<span class="gr">Out[55]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'1677-09-21 00:12:43.145225'</span><span class="p">)</span>
<span class="gp">In [56]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="o">.</span><span class="n">max</span>
<span class="gr">Out[56]: </span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'2262-04-11 23:47:16.854775807'</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-176">有关如何在这些边界之外表示数据的方法,请参见<a class="reference internal" href="#timeseries-oob"><span class="std std-ref">here</span></a>。</span></p>
</div>
<div class="section" id="datetimeindex">
<span id="timeseries-datetimeindex"></span><h2><span class="yiyi-st" id="yiyi-177">DatetimeIndex</span></h2>
<p><span class="yiyi-st" id="yiyi-178"><code class="docutils literal"><span class="pre">DatetimeIndex</span></code>的主要用途之一是作为pandas对象的索引。</span><span class="yiyi-st" id="yiyi-179"><code class="docutils literal"><span class="pre">DatetimeIndex</span></code>类包含许多与时间相关的优化:</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-180">对于各种偏移量的大范围的日期被预先计算并缓存在引擎盖下,以便使得生成随后的日期范围非常快(只需要抓住切片)</span></li>
<li><span class="yiyi-st" id="yiyi-181">使用pandas对象上的<code class="docutils literal"><span class="pre">shift</span></code>和<code class="docutils literal"><span class="pre">tshift</span></code>方法快速移位</span></li>
<li><span class="yiyi-st" id="yiyi-182">具有相同频率的重叠DatetimeIndex对象的联合非常快(对于快速数据对齐很重要)</span></li>
<li><span class="yiyi-st" id="yiyi-183">通过<code class="docutils literal"><span class="pre">year</span></code>,<code class="docutils literal"><span class="pre">month</span></code>等属性快速访问日期字段</span></li>
<li><span class="yiyi-st" id="yiyi-184">正则化函数如<code class="docutils literal"><span class="pre">snap</span></code>和非常快的<code class="docutils literal"><span class="pre">asof</span></code>逻辑</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-185">DatetimeIndex对象具有常规索引对象的所有基本功能和一个高级时间序列特定方法的smorgasbord,以便于频率处理。</span></p>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-186">也可以看看</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-187"><a class="reference internal" href="basics.html#basics-reindexing"><span class="std std-ref">Reindexing methods</span></a></span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-188">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-189">虽然pandas不强迫你有一个排序的日期索引,但如果日期未排序,这些方法中的一些可能具有意外或不正确的行为。</span><span class="yiyi-st" id="yiyi-190">所以请小心。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-191"><code class="docutils literal"><span class="pre">DatetimeIndex</span></code>可以像常规索引一样使用,并提供其所有智能功能,如选择,切片等。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [57]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">'BM'</span><span class="p">)</span>
<span class="gp">In [58]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</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">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">rng</span><span class="p">)),</span> <span class="n">index</span><span class="o">=</span><span class="n">rng</span><span class="p">)</span>
<span class="gp">In [59]: </span><span class="n">ts</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[59]: </span>
<span class="go">DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29',</span>
<span class="go"> '2011-05-31', '2011-06-30', '2011-07-29', '2011-08-31',</span>
<span class="go"> '2011-09-30', '2011-10-31', '2011-11-30', '2011-12-30'],</span>
<span class="go"> dtype='datetime64[ns]', freq='BM')</span>
<span class="gp">In [60]: </span><span class="n">ts</span><span class="p">[:</span><span class="mi">5</span><span class="p">]</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[60]: </span>
<span class="go">DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29',</span>
<span class="go"> '2011-05-31'],</span>
<span class="go"> dtype='datetime64[ns]', freq='BM')</span>
<span class="gp">In [61]: </span><span class="n">ts</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[61]: </span>
<span class="go">DatetimeIndex(['2011-01-31', '2011-03-31', '2011-05-31', '2011-07-29',</span>
<span class="go"> '2011-09-30', '2011-11-30'],</span>
<span class="go"> dtype='datetime64[ns]', freq='2BM')</span>
</pre></div>
</div>
<div class="section" id="datetimeindex-partial-string-indexing">
<span id="timeseries-partialindexing"></span><h3><span class="yiyi-st" id="yiyi-192">DatetimeIndex Partial String Indexing</span></h3>
<p><span class="yiyi-st" id="yiyi-193">您可以传递解析为日期的日期和字符串作为索引参数:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [62]: </span><span class="n">ts</span><span class="p">[</span><span class="s1">'1/31/2011'</span><span class="p">]</span>
<span class="gr">Out[62]: </span><span class="o">-</span><span class="mf">1.2812473076599531</span>
<span class="gp">In [63]: </span><span class="n">ts</span><span class="p">[</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2011</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">25</span><span class="p">):]</span>
<span class="gr">Out[63]: </span>
<span class="go">2011-12-30 0.687738</span>
<span class="go">Freq: BM, dtype: float64</span>
<span class="gp">In [64]: </span><span class="n">ts</span><span class="p">[</span><span class="s1">'10/31/2011'</span><span class="p">:</span><span class="s1">'12/31/2011'</span><span class="p">]</span>
<span class="gr">Out[64]: </span>
<span class="go">2011-10-31 0.149748</span>
<span class="go">2011-11-30 -0.732339</span>
<span class="go">2011-12-30 0.687738</span>
<span class="go">Freq: BM, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-194">为了方便访问较长的时间序列,您还可以将年或年和月作为字符串传入:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [65]: </span><span class="n">ts</span><span class="p">[</span><span class="s1">'2011'</span><span class="p">]</span>
<span class="gr">Out[65]: </span>
<span class="go">2011-01-31 -1.281247</span>
<span class="go">2011-02-28 -0.727707</span>
<span class="go">2011-03-31 -0.121306</span>
<span class="go">2011-04-29 -0.097883</span>
<span class="go">2011-05-31 0.695775</span>
<span class="go">2011-06-30 0.341734</span>
<span class="go">2011-07-29 0.959726</span>
<span class="go">2011-08-31 -1.110336</span>
<span class="go">2011-09-30 -0.619976</span>
<span class="go">2011-10-31 0.149748</span>
<span class="go">2011-11-30 -0.732339</span>
<span class="go">2011-12-30 0.687738</span>
<span class="go">Freq: BM, dtype: float64</span>
<span class="gp">In [66]: </span><span class="n">ts</span><span class="p">[</span><span class="s1">'2011-6'</span><span class="p">]</span>
<span class="gr">Out[66]: </span>
<span class="go">2011-06-30 0.341734</span>
<span class="go">Freq: BM, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-195">这种类型的切片将在具有<code class="docutils literal"><span class="pre">DateTimeIndex</span></code>的DataFrame上工作。</span><span class="yiyi-st" id="yiyi-196">由于部分字符串选择是标签分割的一种形式,所以包括端点<strong>。</strong></span><span class="yiyi-st" id="yiyi-197">这将包括所包含日期的匹配时间。</span><span class="yiyi-st" id="yiyi-198">这里有一个例子:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [67]: </span><span class="n">dft</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">randn</span><span class="p">(</span><span class="mi">100000</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">'A'</span><span class="p">],</span>
<span class="gp"> ....:</span> <span class="n">index</span><span class="o">=</span><span class="n">pd</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">100000</span><span class="p">,</span><span class="n">freq</span><span class="o">=</span><span class="s1">'T'</span><span class="p">))</span>
<span class="gp"> ....:</span>
<span class="gp">In [68]: </span><span class="n">dft</span>
<span class="gr">Out[68]: </span>
<span class="go"> A</span>
<span class="go">2013-01-01 00:00:00 0.176444</span>
<span class="go">2013-01-01 00:01:00 0.403310</span>
<span class="go">2013-01-01 00:02:00 -0.154951</span>
<span class="go">2013-01-01 00:03:00 0.301624</span>
<span class="go">2013-01-01 00:04:00 -2.179861</span>
<span class="go">2013-01-01 00:05:00 -1.369849</span>
<span class="go">2013-01-01 00:06:00 -0.954208</span>
<span class="go">... ...</span>
<span class="go">2013-03-11 10:33:00 -0.293083</span>
<span class="go">2013-03-11 10:34:00 -0.059881</span>
<span class="go">2013-03-11 10:35:00 1.252450</span>
<span class="go">2013-03-11 10:36:00 0.046611</span>
<span class="go">2013-03-11 10:37:00 0.059478</span>
<span class="go">2013-03-11 10:38:00 -0.286539</span>
<span class="go">2013-03-11 10:39:00 0.841669</span>
<span class="go">[100000 rows x 1 columns]</span>
<span class="gp">In [69]: </span><span class="n">dft</span><span class="p">[</span><span class="s1">'2013'</span><span class="p">]</span>
<span class="gr">Out[69]: </span>
<span class="go"> A</span>
<span class="go">2013-01-01 00:00:00 0.176444</span>
<span class="go">2013-01-01 00:01:00 0.403310</span>
<span class="go">2013-01-01 00:02:00 -0.154951</span>
<span class="go">2013-01-01 00:03:00 0.301624</span>
<span class="go">2013-01-01 00:04:00 -2.179861</span>
<span class="go">2013-01-01 00:05:00 -1.369849</span>
<span class="go">2013-01-01 00:06:00 -0.954208</span>
<span class="go">... ...</span>
<span class="go">2013-03-11 10:33:00 -0.293083</span>
<span class="go">2013-03-11 10:34:00 -0.059881</span>
<span class="go">2013-03-11 10:35:00 1.252450</span>
<span class="go">2013-03-11 10:36:00 0.046611</span>
<span class="go">2013-03-11 10:37:00 0.059478</span>
<span class="go">2013-03-11 10:38:00 -0.286539</span>
<span class="go">2013-03-11 10:39:00 0.841669</span>
<span class="go">[100000 rows x 1 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-199">这会从当月的第一个时间开始,包括该月的最后一个日期和时间</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [70]: </span><span class="n">dft</span><span class="p">[</span><span class="s1">'2013-1'</span><span class="p">:</span><span class="s1">'2013-2'</span><span class="p">]</span>
<span class="gr">Out[70]: </span>
<span class="go"> A</span>
<span class="go">2013-01-01 00:00:00 0.176444</span>
<span class="go">2013-01-01 00:01:00 0.403310</span>
<span class="go">2013-01-01 00:02:00 -0.154951</span>
<span class="go">2013-01-01 00:03:00 0.301624</span>
<span class="go">2013-01-01 00:04:00 -2.179861</span>
<span class="go">2013-01-01 00:05:00 -1.369849</span>
<span class="go">2013-01-01 00:06:00 -0.954208</span>
<span class="go">... ...</span>
<span class="go">2013-02-28 23:53:00 0.103114</span>
<span class="go">2013-02-28 23:54:00 -1.303422</span>
<span class="go">2013-02-28 23:55:00 0.451943</span>
<span class="go">2013-02-28 23:56:00 0.220534</span>
<span class="go">2013-02-28 23:57:00 -1.624220</span>
<span class="go">2013-02-28 23:58:00 0.093915</span>
<span class="go">2013-02-28 23:59:00 -1.087454</span>
<span class="go">[84960 rows x 1 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-200">这指定了包括最后一天的所有时间的停止时间<strong></strong></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [71]: </span><span class="n">dft</span><span class="p">[</span><span class="s1">'2013-1'</span><span class="p">:</span><span class="s1">'2013-2-28'</span><span class="p">]</span>
<span class="gr">Out[71]: </span>
<span class="go"> A</span>
<span class="go">2013-01-01 00:00:00 0.176444</span>
<span class="go">2013-01-01 00:01:00 0.403310</span>
<span class="go">2013-01-01 00:02:00 -0.154951</span>
<span class="go">2013-01-01 00:03:00 0.301624</span>
<span class="go">2013-01-01 00:04:00 -2.179861</span>
<span class="go">2013-01-01 00:05:00 -1.369849</span>
<span class="go">2013-01-01 00:06:00 -0.954208</span>
<span class="go">... ...</span>
<span class="go">2013-02-28 23:53:00 0.103114</span>
<span class="go">2013-02-28 23:54:00 -1.303422</span>
<span class="go">2013-02-28 23:55:00 0.451943</span>
<span class="go">2013-02-28 23:56:00 0.220534</span>
<span class="go">2013-02-28 23:57:00 -1.624220</span>
<span class="go">2013-02-28 23:58:00 0.093915</span>
<span class="go">2013-02-28 23:59:00 -1.087454</span>
<span class="go">[84960 rows x 1 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-201">这指定了<strong>精确</strong>停止时间(与上述不同)</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [72]: </span><span class="n">dft</span><span class="p">[</span><span class="s1">'2013-1'</span><span class="p">:</span><span class="s1">'2013-2-28 00:00:00'</span><span class="p">]</span>
<span class="gr">Out[72]: </span>
<span class="go"> A</span>
<span class="go">2013-01-01 00:00:00 0.176444</span>
<span class="go">2013-01-01 00:01:00 0.403310</span>
<span class="go">2013-01-01 00:02:00 -0.154951</span>
<span class="go">2013-01-01 00:03:00 0.301624</span>
<span class="go">2013-01-01 00:04:00 -2.179861</span>
<span class="go">2013-01-01 00:05:00 -1.369849</span>
<span class="go">2013-01-01 00:06:00 -0.954208</span>
<span class="go">... ...</span>
<span class="go">2013-02-27 23:54:00 0.897051</span>
<span class="go">2013-02-27 23:55:00 -0.309230</span>
<span class="go">2013-02-27 23:56:00 1.944713</span>
<span class="go">2013-02-27 23:57:00 0.369265</span>
<span class="go">2013-02-27 23:58:00 0.053071</span>
<span class="go">2013-02-27 23:59:00 -0.019734</span>
<span class="go">2013-02-28 00:00:00 1.388189</span>
<span class="go">[83521 rows x 1 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-202">我们停在所包含的端点,因为它是索引的一部分</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [73]: </span><span class="n">dft</span><span class="p">[</span><span class="s1">'2013-1-15'</span><span class="p">:</span><span class="s1">'2013-1-15 12:30:00'</span><span class="p">]</span>
<span class="gr">Out[73]: </span>
<span class="go"> A</span>
<span class="go">2013-01-15 00:00:00 0.501288</span>
<span class="go">2013-01-15 00:01:00 -0.605198</span>
<span class="go">2013-01-15 00:02:00 0.215146</span>
<span class="go">2013-01-15 00:03:00 0.924732</span>
<span class="go">2013-01-15 00:04:00 -2.228519</span>
<span class="go">2013-01-15 00:05:00 1.517331</span>
<span class="go">2013-01-15 00:06:00 -1.188774</span>
<span class="go">... ...</span>
<span class="go">2013-01-15 12:24:00 1.358314</span>
<span class="go">2013-01-15 12:25:00 -0.737727</span>
<span class="go">2013-01-15 12:26:00 1.838323</span>
<span class="go">2013-01-15 12:27:00 -0.774090</span>
<span class="go">2013-01-15 12:28:00 0.622261</span>
<span class="go">2013-01-15 12:29:00 -0.631649</span>
<span class="go">2013-01-15 12:30:00 0.193284</span>
<span class="go">[751 rows x 1 columns]</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-203">警告</span></p>
<p><span class="yiyi-st" id="yiyi-204">以下选择将产生<code class="docutils literal"><span class="pre">KeyError</span></code>;否则这种选择方法将与pandas中的其他选择方法不一致(因为这不是<em>切片</em>,也不解析为一个)</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dft</span><span class="p">[</span><span class="s1">'2013-1-15 12:30:00'</span><span class="p">]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-205">要选择单个行,请使用<code class="docutils literal"><span class="pre">.loc</span></code></span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [74]: </span><span class="n">dft</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">'2013-1-15 12:30:00'</span><span class="p">]</span>
<span class="gr">Out[74]: </span>
<span class="go">A 0.193284</span>
<span class="go">Name: 2013-01-15 12:30:00, dtype: float64</span>
</pre></div>
</div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-206"><span class="versionmodified">版本0.18.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-207">DatetimeIndex部分字符串索引也适用于具有<code class="docutils literal"><span class="pre">MultiIndex</span></code>的DataFrames。</span><span class="yiyi-st" id="yiyi-208">例如:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [75]: </span><span class="n">dft2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">'A'</span><span class="p">],</span>
<span class="gp"> ....:</span> <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_product</span><span class="p">([</span><span class="n">pd</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="gp"> ....:</span> <span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
<span class="gp"> ....:</span> <span class="n">freq</span><span class="o">=</span><span class="s1">'12H'</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="p">[</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">]]))</span>
<span class="gp"> ....:</span>
<span class="gp">In [76]: </span><span class="n">dft2</span>
<span class="gr">Out[76]: </span>
<span class="go"> A</span>
<span class="go">2013-01-01 00:00:00 a -0.659574</span>
<span class="go"> b 1.494522</span>
<span class="go">2013-01-01 12:00:00 a -0.778425</span>
<span class="go"> b -0.253355</span>
<span class="go">2013-01-02 00:00:00 a -2.816159</span>
<span class="go"> b -1.210929</span>
<span class="go">2013-01-02 12:00:00 a 0.144669</span>
<span class="go">... ...</span>
<span class="go">2013-01-04 00:00:00 b -1.624463</span>
<span class="go">2013-01-04 12:00:00 a 0.056912</span>
<span class="go"> b 0.149867</span>
<span class="go">2013-01-05 00:00:00 a -1.256173</span>
<span class="go"> b 2.324544</span>
<span class="go">2013-01-05 12:00:00 a -1.067396</span>
<span class="go"> b -0.660996</span>
<span class="go">[20 rows x 1 columns]</span>
<span class="gp">In [77]: </span><span class="n">dft2</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">'2013-01-05'</span><span class="p">]</span>
<span class="gr">Out[77]: </span>
<span class="go"> A</span>
<span class="go">2013-01-05 00:00:00 a -1.256173</span>
<span class="go"> b 2.324544</span>
<span class="go">2013-01-05 12:00:00 a -1.067396</span>
<span class="go"> b -0.660996</span>
<span class="gp">In [78]: </span><span class="n">idx</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">IndexSlice</span>
<span class="gp">In [79]: </span><span class="n">dft2</span> <span class="o">=</span> <span class="n">dft2</span><span class="o">.</span><span class="n">swaplevel</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">sort_index</span><span class="p">()</span>
<span class="gp">In [80]: </span><span class="n">dft2</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">idx</span><span class="p">[:,</span> <span class="s1">'2013-01-05'</span><span class="p">],</span> <span class="p">:]</span>
<span class="gr">Out[80]: </span>
<span class="go"> A</span>
<span class="go">a 2013-01-05 00:00:00 -1.256173</span>
<span class="go"> 2013-01-05 12:00:00 -1.067396</span>
<span class="go">b 2013-01-05 00:00:00 2.324544</span>
<span class="go"> 2013-01-05 12:00:00 -0.660996</span>
</pre></div>
</div>
</div>
<div class="section" id="datetime-indexing">
<h3><span class="yiyi-st" id="yiyi-209">Datetime Indexing</span></h3>
<p><span class="yiyi-st" id="yiyi-210">使用部分字符串索引<code class="docutils literal"><span class="pre">DateTimeIndex</span></code>取决于句点的“准确性”,换句话说,间隔与索引的频率之间的具体程度。</span><span class="yiyi-st" id="yiyi-211">相反,使用datetime对象的索引是精确的,因为对象具有确切的含义。</span><span class="yiyi-st" id="yiyi-212">这些也遵循<em>的语义,包括两个端点</em>。</span></p>
<p><span class="yiyi-st" id="yiyi-213">这些<code class="docutils literal"><span class="pre">datetime</span></code>对象是特定的<code class="docutils literal"><span class="pre">小时,</span> <span class="pre">分钟,</span></code>和<code class="docutils literal"><span class="pre">seconds</span></code>没有明确指定(它们是<code class="docutils literal"><span class="pre">0</span></code>)。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [81]: </span><span class="n">dft</span><span class="p">[</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2013</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="n">datetime</span><span class="p">(</span><span class="mi">2013</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">28</span><span class="p">)]</span>
<span class="gr">Out[81]: </span>
<span class="go"> A</span>
<span class="go">2013-01-01 00:00:00 0.176444</span>
<span class="go">2013-01-01 00:01:00 0.403310</span>
<span class="go">2013-01-01 00:02:00 -0.154951</span>
<span class="go">2013-01-01 00:03:00 0.301624</span>
<span class="go">2013-01-01 00:04:00 -2.179861</span>
<span class="go">2013-01-01 00:05:00 -1.369849</span>
<span class="go">2013-01-01 00:06:00 -0.954208</span>
<span class="go">... ...</span>
<span class="go">2013-02-27 23:54:00 0.897051</span>
<span class="go">2013-02-27 23:55:00 -0.309230</span>
<span class="go">2013-02-27 23:56:00 1.944713</span>
<span class="go">2013-02-27 23:57:00 0.369265</span>
<span class="go">2013-02-27 23:58:00 0.053071</span>
<span class="go">2013-02-27 23:59:00 -0.019734</span>
<span class="go">2013-02-28 00:00:00 1.388189</span>
<span class="go">[83521 rows x 1 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-214">没有默认值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [82]: </span><span class="n">dft</span><span class="p">[</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2013</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">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2013</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">28</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">0</span><span class="p">)]</span>
<span class="gr">Out[82]: </span>
<span class="go"> A</span>
<span class="go">2013-01-01 10:12:00 -0.246733</span>
<span class="go">2013-01-01 10:13:00 -1.429225</span>
<span class="go">2013-01-01 10:14:00 -1.265339</span>
<span class="go">2013-01-01 10:15:00 0.710986</span>
<span class="go">2013-01-01 10:16:00 -0.818200</span>
<span class="go">2013-01-01 10:17:00 0.543542</span>
<span class="go">2013-01-01 10:18:00 1.577713</span>
<span class="go">... ...</span>
<span class="go">2013-02-28 10:06:00 0.311249</span>
<span class="go">2013-02-28 10:07:00 2.366080</span>
<span class="go">2013-02-28 10:08:00 -0.490372</span>
<span class="go">2013-02-28 10:09:00 0.373340</span>
<span class="go">2013-02-28 10:10:00 0.638442</span>
<span class="go">2013-02-28 10:11:00 1.330135</span>
<span class="go">2013-02-28 10:12:00 -0.945450</span>
<span class="go">[83521 rows x 1 columns]</span>
</pre></div>
</div>
</div>
<div class="section" id="truncating-fancy-indexing">
<h3><span class="yiyi-st" id="yiyi-215">Truncating & Fancy Indexing</span></h3>
<p><span class="yiyi-st" id="yiyi-216">提供了等效于切片的<code class="docutils literal"><span class="pre">truncate</span></code>便利函数:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [83]: </span><span class="n">ts</span><span class="o">.</span><span class="n">truncate</span><span class="p">(</span><span class="n">before</span><span class="o">=</span><span class="s1">'10/31/2011'</span><span class="p">,</span> <span class="n">after</span><span class="o">=</span><span class="s1">'12/31/2011'</span><span class="p">)</span>
<span class="gr">Out[83]: </span>
<span class="go">2011-10-31 0.149748</span>
<span class="go">2011-11-30 -0.732339</span>
<span class="go">2011-12-30 0.687738</span>
<span class="go">Freq: BM, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-217">即使是打破DatetimeIndex的频率规律性的复杂花式索引也会导致<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>(但频率丢失):</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [84]: </span><span class="n">ts</span><span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">6</span><span class="p">]]</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[84]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">'2011-01-31'</span><span class="p">,</span> <span class="s1">'2011-03-31'</span><span class="p">,</span> <span class="s1">'2011-07-29'</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>
</pre></div>
</div>
</div>
<div class="section" id="time-date-components">
<span id="timeseries-offsets"></span><h3><span class="yiyi-st" id="yiyi-218">Time/Date Components</span></h3>
<p><span class="yiyi-st" id="yiyi-219">有几个时间/日期属性可以从<code class="docutils literal"><span class="pre">Timestamp</span></code>或像<code class="docutils literal"><span class="pre">DateTimeIndex</span></code>的时间戳集合访问。</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="19%">
<col width="81%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-220">属性</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-221">描述</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-222">year</span></td>
<td><span class="yiyi-st" id="yiyi-223">datetime的年份</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-224">month</span></td>
<td><span class="yiyi-st" id="yiyi-225">datetime的月份</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-226">day</span></td>
<td><span class="yiyi-st" id="yiyi-227">datetime的日期</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-228">hour</span></td>
<td><span class="yiyi-st" id="yiyi-229">datetime的小时</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-230">minute</span></td>
<td><span class="yiyi-st" id="yiyi-231">datetime的分钟</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-232">second</span></td>
<td><span class="yiyi-st" id="yiyi-233">datetime的秒数</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-234">microsecond</span></td>
<td><span class="yiyi-st" id="yiyi-235">datetime的微秒</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-236">nanosecond</span></td>
<td><span class="yiyi-st" id="yiyi-237">datetime的纳秒</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-238">date</span></td>
<td><span class="yiyi-st" id="yiyi-239">返回datetime.date(不包含时区信息)</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-240">time</span></td>
<td><span class="yiyi-st" id="yiyi-241">返回datetime.time(不包含时区信息)</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-242">dayofyear</span></td>
<td><span class="yiyi-st" id="yiyi-243">一年的序数</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-244">weekofyear</span></td>
<td><span class="yiyi-st" id="yiyi-245">一年的周数</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-246">week</span></td>
<td><span class="yiyi-st" id="yiyi-247">一年的周数</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-248">dayofweek</span></td>
<td><span class="yiyi-st" id="yiyi-249">星期几的数字,星期一= 0,星期日= 6</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-250">weekday</span></td>
<td><span class="yiyi-st" id="yiyi-251">星期几的数字,星期一= 0,星期日= 6</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-252">weekday_name</span></td>
<td><span class="yiyi-st" id="yiyi-253">一周中的日期名称(例如:星期五)</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-254">quarter</span></td>
<td><span class="yiyi-st" id="yiyi-255">季度:Jan = Mar = 1,Apr-Jun = 2等。</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-256">days_in_month</span></td>
<td><span class="yiyi-st" id="yiyi-257">日期时间的月份中的天数</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-258">is_month_start</span></td>
<td><span class="yiyi-st" id="yiyi-259">逻辑指示是否每月的第一天(由频率定义)</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-260">is_month_end</span></td>
<td><span class="yiyi-st" id="yiyi-261">逻辑指示是否每月的最后一天(由频率定义)</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-262">is_quarter_start</span></td>
<td><span class="yiyi-st" id="yiyi-263">逻辑指示季度的第一天(由频率定义)</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-264">is_quarter_end</span></td>
<td><span class="yiyi-st" id="yiyi-265">逻辑指示季度的最后一天(由频率定义)</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-266">is_year_start</span></td>
<td><span class="yiyi-st" id="yiyi-267">逻辑指示一年中的第一天(由频率定义)</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-268">is_year_end</span></td>
<td><span class="yiyi-st" id="yiyi-269">逻辑指示一年中的最后一天(由频率定义)</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-270">is_leap_year</span></td>
<td><span class="yiyi-st" id="yiyi-271">逻辑指示日期是否属于闰年</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-272">此外,如果您有一个具有datetimelike值的<code class="docutils literal"><span class="pre">Series</span></code>,那么您可以通过<code class="docutils literal"><span class="pre">.dt</span></code>访问器访问这些属性,请参阅<a class="reference internal" href="basics.html#basics-dt-accessors"><span class="std std-ref">docs</span></a></span></p>
</div>
</div>
<div class="section" id="dateoffset-objects">
<h2><span class="yiyi-st" id="yiyi-273">DateOffset objects</span></h2>
<p><span class="yiyi-st" id="yiyi-274">在前面的示例中,我们通过将<a class="reference internal" href="#timeseries-offset-aliases"><span class="std std-ref">frequency strings</span></a>(如“M”,“W”和“BM”)传递到<code class="docutils literal"><span class="pre">freq</span></code>关键字,以各种频率创建DatetimeIndex对象。</span><span class="yiyi-st" id="yiyi-275">在这种情况下,这些频率字符串正被转换为pandas <code class="docutils literal"><span class="pre">DateOffset</span></code>的实例,其表示规则的频率增量。</span><span class="yiyi-st" id="yiyi-276">诸如“月”,“工作日”或“一小时”的特定偏移逻辑表示在其各种子类中。</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="19%">
<col width="81%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-277">班级名称</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-278">描述</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-279">DateOffset</span></td>
<td><span class="yiyi-st" id="yiyi-280">通用偏移类,默认为1个日历日</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-281">BDay</span></td>
<td><span class="yiyi-st" id="yiyi-282">工作日(工作日)</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-283">CDay</span></td>
<td><span class="yiyi-st" id="yiyi-284">自定义工作日(实验)</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-285">Week</span></td>
<td><span class="yiyi-st" id="yiyi-286">一周,任选地锚定在一周的一天</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-287">WeekOfMonth</span></td>
<td><span class="yiyi-st" id="yiyi-288">每个月第y周的第x天</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-289">LastWeekOfMonth</span></td>
<td><span class="yiyi-st" id="yiyi-290">每个月最后一周的第x天</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-291">MonthEnd</span></td>
<td><span class="yiyi-st" id="yiyi-292">日历月结束</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-293">MonthBegin</span></td>
<td><span class="yiyi-st" id="yiyi-294">日历月开始</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-295">BMonthEnd</span></td>
<td><span class="yiyi-st" id="yiyi-296">业务月末</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-297">BMonthBegin</span></td>
<td><span class="yiyi-st" id="yiyi-298">商业月开始</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-299">CBMonthEnd</span></td>
<td><span class="yiyi-st" id="yiyi-300">定制业务月末</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-301">CBMonthBegin</span></td>
<td><span class="yiyi-st" id="yiyi-302">定制商业月开始</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-303">SemiMonthEnd</span></td>
<td><span class="yiyi-st" id="yiyi-304">15日(或其他day_of_month)和日历月结束</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-305">SemiMonthBegin</span></td>
<td><span class="yiyi-st" id="yiyi-306">15日(或其他day_of_month)和日历月开始</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-307">QuarterEnd</span></td>
<td><span class="yiyi-st" id="yiyi-308">日历季度末</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-309">QuarterBegin</span></td>
<td><span class="yiyi-st" id="yiyi-310">日历季度开始</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-311">BQuarterEnd</span></td>
<td><span class="yiyi-st" id="yiyi-312">业务季度末</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-313">BQuarterBegin</span></td>
<td><span class="yiyi-st" id="yiyi-314">业务季开始</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-315">FY5253Quarter</span></td>
<td><span class="yiyi-st" id="yiyi-316">零售(又名52-53周)季度</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-317">YearEnd</span></td>
<td><span class="yiyi-st" id="yiyi-318">日历年结束</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-319">YearBegin</span></td>
<td><span class="yiyi-st" id="yiyi-320">日历年开始</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-321">BYearEnd</span></td>
<td><span class="yiyi-st" id="yiyi-322">业务年度结束</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-323">BYearBegin</span></td>
<td><span class="yiyi-st" id="yiyi-324">商业年开始</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-325">FY5253</span></td>
<td><span class="yiyi-st" id="yiyi-326">零售(又名52-53周)一年</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-327">BusinessHour</span></td>
<td><span class="yiyi-st" id="yiyi-328">营业时间</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-329">CustomBusinessHour</span></td>
<td><span class="yiyi-st" id="yiyi-330">定制营业时间</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-331">Hour</span></td>
<td><span class="yiyi-st" id="yiyi-332">一小时</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-333">Minute</span></td>
<td><span class="yiyi-st" id="yiyi-334">等一下</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-335">Second</span></td>
<td><span class="yiyi-st" id="yiyi-336">一秒</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-337">Milli</span></td>
<td><span class="yiyi-st" id="yiyi-338">一毫秒</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-339">Micro</span></td>
<td><span class="yiyi-st" id="yiyi-340">一微秒</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-341">Nano</span></td>
<td><span class="yiyi-st" id="yiyi-342">一纳秒</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-343">基本的<code class="docutils literal"><span class="pre">DateOffset</span></code>采用与<code class="docutils literal"><span class="pre">dateutil.relativedelta</span></code>相同的参数,其工作方式如下:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [85]: </span><span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>