forked from wizardforcel/pandas-doc-zh
-
Notifications
You must be signed in to change notification settings - Fork 2
/
computation.html
1194 lines (1130 loc) · 131 KB
/
computation.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="computation"></span><h1><span class="yiyi-st" id="yiyi-66">Computational tools</span></h1>
<blockquote>
<p>原文:<a href="http://pandas.pydata.org/pandas-docs/stable/computation.html">http://pandas.pydata.org/pandas-docs/stable/computation.html</a></p>
<p>译者:<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
<p>校对:(虚位以待)</p>
</blockquote>
<div class="section" id="statistical-functions">
<h2><span class="yiyi-st" id="yiyi-67">Statistical Functions</span></h2>
<div class="section" id="percent-change">
<span id="computation-pct-change"></span><h3><span class="yiyi-st" id="yiyi-68">Percent Change</span></h3>
<p><span class="yiyi-st" id="yiyi-69"><code class="docutils literal"><span class="pre">Series</span></code>,<code class="docutils literal"><span class="pre">DataFrame</span></code>和<code class="docutils literal"><span class="pre">Panel</span></code>都有一个方法<code class="docutils literal"><span class="pre">pct_change</span></code>,用于计算给定时间段内的百分比变化(在之前使用<code class="docutils literal"><span class="pre">fill_method</span></code>填充NA / null值<em>)。</em></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="n">ser</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">8</span><span class="p">))</span>
<span class="gp">In [2]: </span><span class="n">ser</span><span class="o">.</span><span class="n">pct_change</span><span class="p">()</span>
<span class="gr">Out[2]: </span>
<span class="go">0 NaN</span>
<span class="go">1 -1.602976</span>
<span class="go">2 4.334938</span>
<span class="go">3 -0.247456</span>
<span class="go">4 -2.067345</span>
<span class="go">5 -1.142903</span>
<span class="go">6 -1.688214</span>
<span class="go">7 -9.759729</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [3]: </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="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">10</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span>
<span class="gp">In [4]: </span><span class="n">df</span><span class="o">.</span><span class="n">pct_change</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="gr">Out[4]: </span>
<span class="go"> 0 1 2 3</span>
<span class="go">0 NaN NaN NaN NaN</span>
<span class="go">1 NaN NaN NaN NaN</span>
<span class="go">2 NaN NaN NaN NaN</span>
<span class="go">3 -0.218320 -1.054001 1.987147 -0.510183</span>
<span class="go">4 -0.439121 -1.816454 0.649715 -4.822809</span>
<span class="go">5 -0.127833 -3.042065 -5.866604 -1.776977</span>
<span class="go">6 -2.596833 -1.959538 -2.111697 -3.798900</span>
<span class="go">7 -0.117826 -2.169058 0.036094 -0.067696</span>
<span class="go">8 2.492606 -1.357320 -1.205802 -1.558697</span>
<span class="go">9 -1.012977 2.324558 -1.003744 -0.371806</span>
</pre></div>
</div>
</div>
<div class="section" id="covariance">
<span id="computation-covariance"></span><h3><span class="yiyi-st" id="yiyi-70">Covariance</span></h3>
<p><span class="yiyi-st" id="yiyi-71"><code class="docutils literal"><span class="pre">Series</span></code>对象具有方法<code class="docutils literal"><span class="pre">cov</span></code>以计算系列之间的协方差(不包括NA /空值)。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [5]: </span><span class="n">s1</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">1000</span><span class="p">))</span>
<span class="gp">In [6]: </span><span class="n">s2</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">1000</span><span class="p">))</span>
<span class="gp">In [7]: </span><span class="n">s1</span><span class="o">.</span><span class="n">cov</span><span class="p">(</span><span class="n">s2</span><span class="p">)</span>
<span class="gr">Out[7]: </span><span class="mf">0.00068010881743108746</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-72">类似地,<code class="docutils literal"><span class="pre">DataFrame</span></code>具有方法<code class="docutils literal"><span class="pre">cov</span></code>以计算DataFrame中的系列之间的成对协方差,也不包括NA /空值。</span></p>
<div class="admonition note" id="computation-covariance-caveats">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-73">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-74">假设丢失的数据随机丢失,这导致对无偏差的协方差矩阵的估计。</span><span class="yiyi-st" id="yiyi-75">然而,对于许多应用,该估计可能是不可接受的,因为估计的协方差矩阵不能保证为正半定。</span><span class="yiyi-st" id="yiyi-76">这可以导致具有大于1的绝对值的估计相关,和/或不可逆协方差矩阵。</span><span class="yiyi-st" id="yiyi-77">有关详细信息,请参阅<a class="reference external" href="http://en.wikipedia.org/w/index.php?title=Estimation_of_covariance_matrices">协方差矩阵估计</a>。</span></p>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [8]: </span><span class="n">frame</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">1000</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</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="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">,</span> <span class="s1">'e'</span><span class="p">])</span>
<span class="gp">In [9]: </span><span class="n">frame</span><span class="o">.</span><span class="n">cov</span><span class="p">()</span>
<span class="gr">Out[9]: </span>
<span class="go"> a b c d e</span>
<span class="go">a 1.000882 -0.003177 -0.002698 -0.006889 0.031912</span>
<span class="go">b -0.003177 1.024721 0.000191 0.009212 0.000857</span>
<span class="go">c -0.002698 0.000191 0.950735 -0.031743 -0.005087</span>
<span class="go">d -0.006889 0.009212 -0.031743 1.002983 -0.047952</span>
<span class="go">e 0.031912 0.000857 -0.005087 -0.047952 1.042487</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-78"><code class="docutils literal"><span class="pre">DataFrame.cov</span></code>还支持可选的<code class="docutils literal"><span class="pre">min_periods</span></code>关键字,该关键字指定每个列对所需的最小观测值数,以获得有效的结果。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [10]: </span><span class="n">frame</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">3</span><span class="p">),</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="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">])</span>
<span class="gp">In [11]: </span><span class="n">frame</span><span class="o">.</span><span class="n">ix</span><span class="p">[:</span><span class="mi">5</span><span class="p">,</span> <span class="s1">'a'</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 [12]: </span><span class="n">frame</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">5</span><span class="p">:</span><span class="mi">10</span><span class="p">,</span> <span class="s1">'b'</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 [13]: </span><span class="n">frame</span><span class="o">.</span><span class="n">cov</span><span class="p">()</span>
<span class="gr">Out[13]: </span>
<span class="go"> a b c</span>
<span class="go">a 1.210090 -0.430629 0.018002</span>
<span class="go">b -0.430629 1.240960 0.347188</span>
<span class="go">c 0.018002 0.347188 1.301149</span>
<span class="gp">In [14]: </span><span class="n">frame</span><span class="o">.</span><span class="n">cov</span><span class="p">(</span><span class="n">min_periods</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>
<span class="gr">Out[14]: </span>
<span class="go"> a b c</span>
<span class="go">a 1.210090 NaN 0.018002</span>
<span class="go">b NaN 1.240960 0.347188</span>
<span class="go">c 0.018002 0.347188 1.301149</span>
</pre></div>
</div>
</div>
<div class="section" id="correlation">
<span id="computation-correlation"></span><h3><span class="yiyi-st" id="yiyi-79">Correlation</span></h3>
<p><span class="yiyi-st" id="yiyi-80">提供了用于计算相关性的几种方法:</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="20%">
<col width="80%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-81">方法名称</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-82">描述</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-83"><code class="docutils literal"><span class="pre">pearson</span> <span class="pre">(默认)</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-84">标准相关系数</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-85"><code class="docutils literal"><span class="pre">kendall</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-86">Kendall Tau相关系数</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-87"><code class="docutils literal"><span class="pre">spearman</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-88">斯皮尔曼等级相关系数</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-89">所有这些目前使用成对完全观察来计算。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-90">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-91">请参阅与<a class="reference internal" href="#computation-covariance"><span class="std std-ref">covariance section</span></a>中计算相关矩阵的此方法相关联的<a class="reference internal" href="#computation-covariance-caveats"><span class="std std-ref">caveats</span></a>。</span></p>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [15]: </span><span class="n">frame</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">1000</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</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="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">,</span> <span class="s1">'e'</span><span class="p">])</span>
<span class="gp">In [16]: </span><span class="n">frame</span><span class="o">.</span><span class="n">ix</span><span class="p">[::</span><span class="mi">2</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="c"># Series with Series</span>
<span class="gp">In [17]: </span><span class="n">frame</span><span class="p">[</span><span class="s1">'a'</span><span class="p">]</span><span class="o">.</span><span class="n">corr</span><span class="p">(</span><span class="n">frame</span><span class="p">[</span><span class="s1">'b'</span><span class="p">])</span>
<span class="gr">Out[17]: </span><span class="mf">0.013479040400098775</span>
<span class="gp">In [18]: </span><span class="n">frame</span><span class="p">[</span><span class="s1">'a'</span><span class="p">]</span><span class="o">.</span><span class="n">corr</span><span class="p">(</span><span class="n">frame</span><span class="p">[</span><span class="s1">'b'</span><span class="p">],</span> <span class="n">method</span><span class="o">=</span><span class="s1">'spearman'</span><span class="p">)</span>
<span class="gr">Out[18]: </span><span class="o">-</span><span class="mf">0.0072898851595406371</span>
<span class="c"># Pairwise correlation of DataFrame columns</span>
<span class="gp">In [19]: </span><span class="n">frame</span><span class="o">.</span><span class="n">corr</span><span class="p">()</span>
<span class="gr">Out[19]: </span>
<span class="go"> a b c d e</span>
<span class="go">a 1.000000 0.013479 -0.049269 -0.042239 -0.028525</span>
<span class="go">b 0.013479 1.000000 -0.020433 -0.011139 0.005654</span>
<span class="go">c -0.049269 -0.020433 1.000000 0.018587 -0.054269</span>
<span class="go">d -0.042239 -0.011139 0.018587 1.000000 -0.017060</span>
<span class="go">e -0.028525 0.005654 -0.054269 -0.017060 1.000000</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-92">请注意,非数字列将从相关性计算中自动排除。</span></p>
<p><span class="yiyi-st" id="yiyi-93">像<code class="docutils literal"><span class="pre">cov</span></code>,<code class="docutils literal"><span class="pre">corr</span></code>也支持可选的<code class="docutils literal"><span class="pre">min_periods</span></code>关键字:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [20]: </span><span class="n">frame</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">3</span><span class="p">),</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="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">])</span>
<span class="gp">In [21]: </span><span class="n">frame</span><span class="o">.</span><span class="n">ix</span><span class="p">[:</span><span class="mi">5</span><span class="p">,</span> <span class="s1">'a'</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 [22]: </span><span class="n">frame</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">5</span><span class="p">:</span><span class="mi">10</span><span class="p">,</span> <span class="s1">'b'</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 [23]: </span><span class="n">frame</span><span class="o">.</span><span class="n">corr</span><span class="p">()</span>
<span class="gr">Out[23]: </span>
<span class="go"> a b c</span>
<span class="go">a 1.000000 -0.076520 0.160092</span>
<span class="go">b -0.076520 1.000000 0.135967</span>
<span class="go">c 0.160092 0.135967 1.000000</span>
<span class="gp">In [24]: </span><span class="n">frame</span><span class="o">.</span><span class="n">corr</span><span class="p">(</span><span class="n">min_periods</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>
<span class="gr">Out[24]: </span>
<span class="go"> a b c</span>
<span class="go">a 1.000000 NaN 0.160092</span>
<span class="go">b NaN 1.000000 0.135967</span>
<span class="go">c 0.160092 0.135967 1.000000</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-94">在DataFrame上实现相关方法<code class="docutils literal"><span class="pre">corrwith</span></code>,以计算不同DataFrame对象中包含的类似标记的系列之间的相关性。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [25]: </span><span class="n">index</span> <span class="o">=</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="s1">'c'</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">,</span> <span class="s1">'e'</span><span class="p">]</span>
<span class="gp">In [26]: </span><span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'one'</span><span class="p">,</span> <span class="s1">'two'</span><span class="p">,</span> <span class="s1">'three'</span><span class="p">,</span> <span class="s1">'four'</span><span class="p">]</span>
<span class="gp">In [27]: </span><span class="n">df1</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">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">)</span>
<span class="gp">In [28]: </span><span class="n">df2</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">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">[:</span><span class="mi">4</span><span class="p">],</span> <span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">)</span>
<span class="gp">In [29]: </span><span class="n">df1</span><span class="o">.</span><span class="n">corrwith</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span>
<span class="gr">Out[29]: </span>
<span class="go">one -0.125501</span>
<span class="go">two -0.493244</span>
<span class="go">three 0.344056</span>
<span class="go">four 0.004183</span>
<span class="go">dtype: float64</span>
<span class="gp">In [30]: </span><span class="n">df2</span><span class="o">.</span><span class="n">corrwith</span><span class="p">(</span><span class="n">df1</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[30]: </span>
<span class="go">a -0.675817</span>
<span class="go">b 0.458296</span>
<span class="go">c 0.190809</span>
<span class="go">d -0.186275</span>
<span class="go">e NaN</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
</div>
<div class="section" id="data-ranking">
<span id="computation-ranking"></span><h3><span class="yiyi-st" id="yiyi-95">Data ranking</span></h3>
<p><span class="yiyi-st" id="yiyi-96"><code class="docutils literal"><span class="pre">rank</span></code>方法生成数据排名,其中系统分配了组的排名的平均值(默认情况下):</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [31]: </span><span class="n">s</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">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">5</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">'abcde'</span><span class="p">))</span>
<span class="gp">In [32]: </span><span class="n">s</span><span class="p">[</span><span class="s1">'d'</span><span class="p">]</span> <span class="o">=</span> <span class="n">s</span><span class="p">[</span><span class="s1">'b'</span><span class="p">]</span> <span class="c1"># so there's a tie</span>
<span class="gp">In [33]: </span><span class="n">s</span><span class="o">.</span><span class="n">rank</span><span class="p">()</span>
<span class="gr">Out[33]: </span>
<span class="go">a 5.0</span>
<span class="go">b 2.5</span>
<span class="go">c 1.0</span>
<span class="go">d 2.5</span>
<span class="go">e 4.0</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-97"><code class="docutils literal"><span class="pre">rank</span></code>也是DataFrame方法,可以对行(<code class="docutils literal"><span class="pre">axis=0</span></code>)或列(<code class="docutils literal"><span class="pre">axis=1</span></code>)排序。</span><span class="yiyi-st" id="yiyi-98"><code class="docutils literal"><span class="pre">NaN</span></code>值从排名中排除。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [34]: </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="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</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">10</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span>
<span class="gp">In [35]: </span><span class="n">df</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="mi">2</span><span class="p">][:</span><span class="mi">5</span><span class="p">]</span> <span class="c1"># some ties</span>
<span class="gp">In [36]: </span><span class="n">df</span>
<span class="gr">Out[36]: </span>
<span class="go"> 0 1 2 3 4 5</span>
<span class="go">0 -0.904948 -1.163537 -1.457187 0.135463 -1.457187 0.294650</span>
<span class="go">1 -0.976288 -0.244652 -0.748406 -0.999601 -0.748406 -0.800809</span>
<span class="go">2 0.401965 1.460840 1.256057 1.308127 1.256057 0.876004</span>
<span class="go">3 0.205954 0.369552 -0.669304 0.038378 -0.669304 1.140296</span>
<span class="go">4 -0.477586 -0.730705 -1.129149 -0.601463 -1.129149 -0.211196</span>
<span class="go">5 -1.092970 -0.689246 0.908114 0.204848 NaN 0.463347</span>
<span class="go">6 0.376892 0.959292 0.095572 -0.593740 NaN -0.069180</span>
<span class="go">7 -1.002601 1.957794 -0.120708 0.094214 NaN -1.467422</span>
<span class="go">8 -0.547231 0.664402 -0.519424 -0.073254 NaN -1.263544</span>
<span class="go">9 -0.250277 -0.237428 -1.056443 0.419477 NaN 1.375064</span>
<span class="gp">In [37]: </span><span class="n">df</span><span class="o">.</span><span class="n">rank</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[37]: </span>
<span class="go"> 0 1 2 3 4 5</span>
<span class="go">0 4.0 3.0 1.5 5.0 1.5 6.0</span>
<span class="go">1 2.0 6.0 4.5 1.0 4.5 3.0</span>
<span class="go">2 1.0 6.0 3.5 5.0 3.5 2.0</span>
<span class="go">3 4.0 5.0 1.5 3.0 1.5 6.0</span>
<span class="go">4 5.0 3.0 1.5 4.0 1.5 6.0</span>
<span class="go">5 1.0 2.0 5.0 3.0 NaN 4.0</span>
<span class="go">6 4.0 5.0 3.0 1.0 NaN 2.0</span>
<span class="go">7 2.0 5.0 3.0 4.0 NaN 1.0</span>
<span class="go">8 2.0 5.0 3.0 4.0 NaN 1.0</span>
<span class="go">9 2.0 3.0 1.0 4.0 NaN 5.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-99"><code class="docutils literal"><span class="pre">rank</span></code>可选地采用参数<code class="docutils literal"><span class="pre">ascending</span></code>,其默认为真;当为假时,数据被反向排序,其中较大的值被分配较小的等级。</span></p>
<p><span class="yiyi-st" id="yiyi-100"><code class="docutils literal"><span class="pre">rank</span></code>支持使用<code class="docutils literal"><span class="pre">method</span></code>参数指定的不同的打破方法:</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-101"><code class="docutils literal"><span class="pre">average</span></code>:绑定组的平均排名</span></li>
<li><span class="yiyi-st" id="yiyi-102"><code class="docutils literal"><span class="pre">min</span></code>:组中的最低排名</span></li>
<li><span class="yiyi-st" id="yiyi-103"><code class="docutils literal"><span class="pre">max</span></code>:组中的最高排名</span></li>
<li><span class="yiyi-st" id="yiyi-104"><code class="docutils literal"><span class="pre">first</span></code>:按照它们在数组中显示的顺序分配的排名</span></li>
</ul>
</div></blockquote>
</div>
</div>
<div class="section" id="window-functions">
<span id="stats-moments"></span><h2><span class="yiyi-st" id="yiyi-105">Window Functions</span></h2>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-106">警告</span></p>
<p><span class="yiyi-st" id="yiyi-107">在版本0.18.0之前,<code class="docutils literal"><span class="pre">pd.rolling_*</span></code>,<code class="docutils literal"><span class="pre">pd.expanding_*</span></code>和<code class="docutils literal"><span class="pre">pd.ewm*</span></code>是模块级函数,现在已弃用。</span><span class="yiyi-st" id="yiyi-108">使用<code class="xref py py-class docutils literal"><span class="pre">Rolling</span></code>,<code class="xref py py-class docutils literal"><span class="pre">Expanding</span></code>和<code class="xref py py-class docutils literal"><span class="pre">EWM</span></code>替换这些。</span><span class="yiyi-st" id="yiyi-109">对象和相应的方法调用。</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-110">弃用警告将显示新语法,请参阅示例<a class="reference internal" href="whatsnew.html#whatsnew-0180-window-deprecations"><span class="std std-ref">here</span></a>您可以在此查看以前的文档<a class="reference external" href="http://pandas.pydata.org/pandas-docs/version/0.17.1/computation.html#moving-rolling-statistics-moments"></a></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-111">对于处理数据,提供了许多用于计算公共<em>窗口</em>或<em>滚动 t>统计的窗口函数。</em></span><span class="yiyi-st" id="yiyi-112">其中包括计数,总和,平均值,中值,相关性,方差,协方差,标准偏差,偏度和峰度。</span></p>
<p><span class="yiyi-st" id="yiyi-113">从版本0.18.1开始,可以直接从DataFrameGroupBy对象中使用<code class="docutils literal"><span class="pre">rolling()</span></code>和<code class="docutils literal"><span class="pre">expanding()</span></code>函数,请参阅<a class="reference internal" href="groupby.html#groupby-transform-window-resample"><span class="std std-ref">groupby docs</span></a>。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-114">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-115">窗口统计的API非常类似于使用<code class="docutils literal"><span class="pre">GroupBy</span></code>对象的方式,请参阅文档<a class="reference internal" href="groupby.html#groupby"><span class="std std-ref">here</span></a></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-116">我们通过相应的对象处理<code class="docutils literal"><span class="pre">rolling</span></code>,<code class="docutils literal"><span class="pre">expanding</span></code>和<code class="docutils literal"><span class="pre">指数式</span> <span class="pre">加权</span> <code class="xref py py-class docutils literal"><span class="pre">Rolling</span></code>,<code class="xref py py-class docutils literal"><span class="pre">Expanding</span></code>和<code class="xref py py-class docutils literal"><span class="pre">EWM</span></code>。</code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [38]: </span><span class="n">s</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">1000</span><span class="p">),</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">'1/1/2000'</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="gp">In [39]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">cumsum</span><span class="p">()</span>
<span class="gp">In [40]: </span><span class="n">s</span>
<span class="gr">Out[40]: </span>
<span class="go">2000-01-01 -0.268824</span>
<span class="go">2000-01-02 -1.771855</span>
<span class="go">2000-01-03 -0.818003</span>
<span class="go">2000-01-04 -0.659244</span>
<span class="go">2000-01-05 -1.942133</span>
<span class="go">2000-01-06 -1.869391</span>
<span class="go">2000-01-07 0.563674</span>
<span class="go"> ... </span>
<span class="go">2002-09-20 -68.233054</span>
<span class="go">2002-09-21 -66.765687</span>
<span class="go">2002-09-22 -67.457323</span>
<span class="go">2002-09-23 -69.253182</span>
<span class="go">2002-09-24 -70.296818</span>
<span class="go">2002-09-25 -70.844674</span>
<span class="go">2002-09-26 -72.475016</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-117">这些是从<code class="docutils literal"><span class="pre">Series</span></code>和<code class="docutils literal"><span class="pre">DataFrame</span></code>的方法创建的。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [41]: </span><span class="n">r</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">60</span><span class="p">)</span>
<span class="gp">In [42]: </span><span class="n">r</span>
<span class="gr">Out[42]: </span><span class="n">Rolling</span> <span class="p">[</span><span class="n">window</span><span class="o">=</span><span class="mi">60</span><span class="p">,</span><span class="n">center</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-118">这些对象提供了可选方法和属性的制表完成。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [14]: </span><span class="n">r</span><span class="o">.</span>
<span class="go">r.agg r.apply r.count r.exclusions r.max r.median r.name r.skew r.sum</span>
<span class="go">r.aggregate r.corr r.cov r.kurt r.mean r.min r.quantile r.std r.var</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-119">通常这些方法都有相同的接口。</span><span class="yiyi-st" id="yiyi-120">他们都接受以下参数:</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-121"><code class="docutils literal"><span class="pre">window</span></code>:移动窗口的大小</span></li>
<li><span class="yiyi-st" id="yiyi-122"><code class="docutils literal"><span class="pre">min_periods</span></code>:要求非空数据点的阈值(否则结果为NA)</span></li>
<li><span class="yiyi-st" id="yiyi-123"><code class="docutils literal"><span class="pre">center</span></code>:boolean,是否将标签设置在中心(默认为False)</span></li>
</ul>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-124">警告</span></p>
<p><span class="yiyi-st" id="yiyi-125">在0.18.0更改之前,<code class="docutils literal"><span class="pre">freq</span></code>和<code class="docutils literal"><span class="pre">how</span></code>参数在API中。</span><span class="yiyi-st" id="yiyi-126">这些在新的API中已弃用。</span><span class="yiyi-st" id="yiyi-127">您可以在创建窗口函数之前对输入进行简单重新采样。</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-128">例如,代替<code class="docutils literal"><span class="pre">s.rolling(window=5,freq='D').max()</span></code>在滚动5天窗口中获取最大值,可以使用<code class="docutils literal"><span class="pre">s.resample('D').max().rolling(window=5).max()</span></code>,首先将数据重新采样为每日数据,然后提供滚动5天窗口。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-129">然后,我们可以在这些<code class="docutils literal"><span class="pre">rolling</span></code>对象上调用方法。</span><span class="yiyi-st" id="yiyi-130">这些返回类似索引的对象:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [43]: </span><span class="n">r</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[43]: </span>
<span class="go">2000-01-01 NaN</span>
<span class="go">2000-01-02 NaN</span>
<span class="go">2000-01-03 NaN</span>
<span class="go">2000-01-04 NaN</span>
<span class="go">2000-01-05 NaN</span>
<span class="go">2000-01-06 NaN</span>
<span class="go">2000-01-07 NaN</span>
<span class="go"> ... </span>
<span class="go">2002-09-20 -62.694135</span>
<span class="go">2002-09-21 -62.812190</span>
<span class="go">2002-09-22 -62.914971</span>
<span class="go">2002-09-23 -63.061867</span>
<span class="go">2002-09-24 -63.213876</span>
<span class="go">2002-09-25 -63.375074</span>
<span class="go">2002-09-26 -63.539734</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [44]: </span><span class="n">s</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s1">'k--'</span><span class="p">)</span>
<span class="gr">Out[44]: </span><span class="o"><</span><span class="n">matplotlib</span><span class="o">.</span><span class="n">axes</span><span class="o">.</span><span class="n">_subplots</span><span class="o">.</span><span class="n">AxesSubplot</span> <span class="n">at</span> <span class="mh">0x7ff282080dd0</span><span class="o">></span>
<span class="gp">In [45]: </span><span class="n">r</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s1">'k'</span><span class="p">)</span>
<span class="gr">Out[45]: </span><span class="o"><</span><span class="n">matplotlib</span><span class="o">.</span><span class="n">axes</span><span class="o">.</span><span class="n">_subplots</span><span class="o">.</span><span class="n">AxesSubplot</span> <span class="n">at</span> <span class="mh">0x7ff282080dd0</span><span class="o">></span>
</pre></div>
</div>
<img alt="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/rolling_mean_ex.png" src="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/rolling_mean_ex.png">
<p><span class="yiyi-st" id="yiyi-131">它们也可以应用于DataFrame对象。</span><span class="yiyi-st" id="yiyi-132">这实际上只是语法糖用于移动窗口运算符到所有的DataFrame的列:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [46]: </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="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">1000</span><span class="p">,</span> <span class="mi">4</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">'1/1/2000'</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="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="s1">'B'</span><span class="p">,</span> <span class="s1">'C'</span><span class="p">,</span> <span class="s1">'D'</span><span class="p">])</span>
<span class="gp"> ....:</span>
<span class="gp">In [47]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">cumsum</span><span class="p">()</span>
<span class="gp">In [48]: </span><span class="n">df</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">60</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">subplots</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[48]: </span>
<span class="go">array([<matplotlib.axes._subplots.AxesSubplot object at 0x7ff28c067210>,</span>
<span class="go"> <matplotlib.axes._subplots.AxesSubplot object at 0x7ff27e03a0d0>,</span>
<span class="go"> <matplotlib.axes._subplots.AxesSubplot object at 0x7ff280bca510>,</span>
<span class="go"> <matplotlib.axes._subplots.AxesSubplot object at 0x7ff28155b910>], dtype=object)</span>
</pre></div>
</div>
<img alt="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/rolling_mean_frame.png" src="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/rolling_mean_frame.png">
<div class="section" id="method-summary">
<span id="stats-summary"></span><h3><span class="yiyi-st" id="yiyi-133">Method Summary</span></h3>
<p><span class="yiyi-st" id="yiyi-134">我们提供了一些常见的统计功能:</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="20%">
<col width="80%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-135">方法</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-136">描述</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-137"><a class="reference internal" href="generated/pandas.core.window.Rolling.count.html#pandas.core.window.Rolling.count" title="pandas.core.window.Rolling.count"><code class="xref py py-meth docutils literal"><span class="pre">count()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-138">非零观察数</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-139"><a class="reference internal" href="generated/pandas.core.window.Rolling.sum.html#pandas.core.window.Rolling.sum" title="pandas.core.window.Rolling.sum"><code class="xref py py-meth docutils literal"><span class="pre">sum()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-140">值的总和</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-141"><a class="reference internal" href="generated/pandas.core.window.Rolling.mean.html#pandas.core.window.Rolling.mean" title="pandas.core.window.Rolling.mean"><code class="xref py py-meth docutils literal"><span class="pre">mean()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-142">值的平均值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-143"><a class="reference internal" href="generated/pandas.core.window.Rolling.median.html#pandas.core.window.Rolling.median" title="pandas.core.window.Rolling.median"><code class="xref py py-meth docutils literal"><span class="pre">median()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-144">值的算术中值</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-145"><a class="reference internal" href="generated/pandas.core.window.Rolling.min.html#pandas.core.window.Rolling.min" title="pandas.core.window.Rolling.min"><code class="xref py py-meth docutils literal"><span class="pre">min()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-146">最低</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-147"><a class="reference internal" href="generated/pandas.core.window.Rolling.max.html#pandas.core.window.Rolling.max" title="pandas.core.window.Rolling.max"><code class="xref py py-meth docutils literal"><span class="pre">max()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-148">最大值</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-149"><a class="reference internal" href="generated/pandas.core.window.Rolling.std.html#pandas.core.window.Rolling.std" title="pandas.core.window.Rolling.std"><code class="xref py py-meth docutils literal"><span class="pre">std()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-150">贝塞尔校正样本标准偏差</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-151"><a class="reference internal" href="generated/pandas.core.window.Rolling.var.html#pandas.core.window.Rolling.var" title="pandas.core.window.Rolling.var"><code class="xref py py-meth docutils literal"><span class="pre">var()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-152">无偏差</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-153"><a class="reference internal" href="generated/pandas.core.window.Rolling.skew.html#pandas.core.window.Rolling.skew" title="pandas.core.window.Rolling.skew"><code class="xref py py-meth docutils literal"><span class="pre">skew()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-154">样品偏斜度(第三动量)</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-155"><a class="reference internal" href="generated/pandas.core.window.Rolling.kurt.html#pandas.core.window.Rolling.kurt" title="pandas.core.window.Rolling.kurt"><code class="xref py py-meth docutils literal"><span class="pre">kurt()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-156">样品峰度(第4圈)</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-157"><a class="reference internal" href="generated/pandas.core.window.Rolling.quantile.html#pandas.core.window.Rolling.quantile" title="pandas.core.window.Rolling.quantile"><code class="xref py py-meth docutils literal"><span class="pre">quantile()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-158">样本分位数(值%)</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-159"><a class="reference internal" href="generated/pandas.core.window.Rolling.apply.html#pandas.core.window.Rolling.apply" title="pandas.core.window.Rolling.apply"><code class="xref py py-meth docutils literal"><span class="pre">apply()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-160">通用适用</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-161"><a class="reference internal" href="generated/pandas.core.window.Rolling.cov.html#pandas.core.window.Rolling.cov" title="pandas.core.window.Rolling.cov"><code class="xref py py-meth docutils literal"><span class="pre">cov()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-162">无偏协方差(二元)</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-163"><a class="reference internal" href="generated/pandas.core.window.Rolling.corr.html#pandas.core.window.Rolling.corr" title="pandas.core.window.Rolling.corr"><code class="xref py py-meth docutils literal"><span class="pre">corr()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-164">相关(二进制)</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-165"><a class="reference internal" href="generated/pandas.core.window.Rolling.apply.html#pandas.core.window.Rolling.apply" title="pandas.core.window.Rolling.apply"><code class="xref py py-meth docutils literal"><span class="pre">apply()</span></code></a>函数需要一个额外的<code class="docutils literal"><span class="pre">func</span></code>参数,并执行通用滚动计算。</span><span class="yiyi-st" id="yiyi-166"><code class="docutils literal"><span class="pre">func</span></code>参数应该是一个从ndarray输入生成单个值的单个函数。</span><span class="yiyi-st" id="yiyi-167">假设我们想要计算滚动平均绝对偏差:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [49]: </span><span class="n">mad</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">fabs</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="n">x</span><span class="o">.</span><span class="n">mean</span><span class="p">())</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gp">In [50]: </span><span class="n">s</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">60</span><span class="p">)</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">mad</span><span class="p">)</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s1">'k'</span><span class="p">)</span>
<span class="gr">Out[50]: </span><span class="o"><</span><span class="n">matplotlib</span><span class="o">.</span><span class="n">axes</span><span class="o">.</span><span class="n">_subplots</span><span class="o">.</span><span class="n">AxesSubplot</span> <span class="n">at</span> <span class="mh">0x7ff2846d3ad0</span><span class="o">></span>
</pre></div>
</div>
<img alt="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/rolling_apply_ex.png" src="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/rolling_apply_ex.png">
</div>
<div class="section" id="rolling-windows">
<span id="stats-rolling-window"></span><h3><span class="yiyi-st" id="yiyi-168">Rolling Windows</span></h3>
<p><span class="yiyi-st" id="yiyi-169">将<code class="docutils literal"><span class="pre">win_type</span></code>传递到<code class="docutils literal"><span class="pre">.rolling</span></code>生成通用滚动窗口计算,根据<code class="docutils literal"><span class="pre">win_type</span></code>进行加权。</span><span class="yiyi-st" id="yiyi-170">提供以下方法:</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="20%">
<col width="80%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-171">方法</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-172">描述</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-173"><a class="reference internal" href="generated/pandas.core.window.Window.sum.html#pandas.core.window.Window.sum" title="pandas.core.window.Window.sum"><code class="xref py py-meth docutils literal"><span class="pre">sum()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-174">值的总和</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-175"><a class="reference internal" href="generated/pandas.core.window.Window.mean.html#pandas.core.window.Window.mean" title="pandas.core.window.Window.mean"><code class="xref py py-meth docutils literal"><span class="pre">mean()</span></code></a></span></td>
<td><span class="yiyi-st" id="yiyi-176">值的平均值</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-177">窗口中使用的权重由<code class="docutils literal"><span class="pre">win_type</span></code>关键字指定。</span><span class="yiyi-st" id="yiyi-178">公认类型的列表是:</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-179"><code class="docutils literal"><span class="pre">boxcar</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-180"><code class="docutils literal"><span class="pre">triang</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-181"><code class="docutils literal"><span class="pre">blackman</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-182"><code class="docutils literal"><span class="pre">hamming</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-183"><code class="docutils literal"><span class="pre">bartlett</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-184"><code class="docutils literal"><span class="pre">parzen</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-185"><code class="docutils literal"><span class="pre">bohman</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-186"><code class="docutils literal"><span class="pre">blackmanharris</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-187"><code class="docutils literal"><span class="pre">nuttall</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-188"><code class="docutils literal"><span class="pre">barthann</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-189"><code class="docutils literal"><span class="pre">kaiser</span></code>(需要测试版)</span></li>
<li><span class="yiyi-st" id="yiyi-190"><code class="docutils literal"><span class="pre">gaussian</span></code>(needs std)</span></li>
<li><span class="yiyi-st" id="yiyi-191"><code class="docutils literal"><span class="pre">general_gaussian</span></code>(需要功率,宽度)</span></li>
<li><span class="yiyi-st" id="yiyi-192"><code class="docutils literal"><span class="pre">slepian</span></code>(需要宽度)。</span></li>
</ul>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [51]: </span><span class="n">ser</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">10</span><span class="p">),</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">'1/1/2000'</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">))</span>
<span class="gp">In [52]: </span><span class="n">ser</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">win_type</span><span class="o">=</span><span class="s1">'triang'</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[52]: </span>
<span class="go">2000-01-01 NaN</span>
<span class="go">2000-01-02 NaN</span>
<span class="go">2000-01-03 NaN</span>
<span class="go">2000-01-04 NaN</span>
<span class="go">2000-01-05 -1.037870</span>
<span class="go">2000-01-06 -0.767705</span>
<span class="go">2000-01-07 -0.383197</span>
<span class="go">2000-01-08 -0.395513</span>
<span class="go">2000-01-09 -0.558440</span>
<span class="go">2000-01-10 -0.672416</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-193">注意,<code class="docutils literal"><span class="pre">boxcar</span></code>窗口等效于<a class="reference internal" href="generated/pandas.core.window.Rolling.mean.html#pandas.core.window.Rolling.mean" title="pandas.core.window.Rolling.mean"><code class="xref py py-meth docutils literal"><span class="pre">mean()</span></code></a>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [53]: </span><span class="n">ser</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">win_type</span><span class="o">=</span><span class="s1">'boxcar'</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[53]: </span>
<span class="go">2000-01-01 NaN</span>
<span class="go">2000-01-02 NaN</span>
<span class="go">2000-01-03 NaN</span>
<span class="go">2000-01-04 NaN</span>
<span class="go">2000-01-05 -0.841164</span>
<span class="go">2000-01-06 -0.779948</span>
<span class="go">2000-01-07 -0.565487</span>
<span class="go">2000-01-08 -0.502815</span>
<span class="go">2000-01-09 -0.553755</span>
<span class="go">2000-01-10 -0.472211</span>
<span class="go">Freq: D, dtype: float64</span>
<span class="gp">In [54]: </span><span class="n">ser</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[54]: </span>
<span class="go">2000-01-01 NaN</span>
<span class="go">2000-01-02 NaN</span>
<span class="go">2000-01-03 NaN</span>
<span class="go">2000-01-04 NaN</span>
<span class="go">2000-01-05 -0.841164</span>
<span class="go">2000-01-06 -0.779948</span>
<span class="go">2000-01-07 -0.565487</span>
<span class="go">2000-01-08 -0.502815</span>
<span class="go">2000-01-09 -0.553755</span>
<span class="go">2000-01-10 -0.472211</span>
<span class="go">Freq: D, 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 [55]: </span><span class="n">ser</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">win_type</span><span class="o">=</span><span class="s1">'gaussian'</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">std</span><span class="o">=</span><span class="mf">0.1</span><span class="p">)</span>
<span class="gr">Out[55]: </span>
<span class="go">2000-01-01 NaN</span>
<span class="go">2000-01-02 NaN</span>
<span class="go">2000-01-03 NaN</span>
<span class="go">2000-01-04 NaN</span>
<span class="go">2000-01-05 -1.309989</span>
<span class="go">2000-01-06 -1.153000</span>
<span class="go">2000-01-07 0.606382</span>
<span class="go">2000-01-08 -0.681101</span>
<span class="go">2000-01-09 -0.289724</span>
<span class="go">2000-01-10 -0.996632</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<div class="admonition note" id="stats-moments-normalization">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-195">注意</span></p>
<p><span class="yiyi-st" id="yiyi-196">对于具有<code class="docutils literal"><span class="pre">win_type</span></code>的<code class="docutils literal"><span class="pre">.sum()</span></code>,对于窗口的权重没有进行归一化。</span><span class="yiyi-st" id="yiyi-197">传递<code class="docutils literal"><span class="pre">[1,</span> <span class="pre">1,</span> <span class="pre">1]</span></code>的自定义权重将产生与传递<code class="docutils literal"> <span class="pre">[2,</span> <span class="pre">2,</span> <span class="pre">2]</span></code>。</span><span class="yiyi-st" id="yiyi-198">当传递<code class="docutils literal"><span class="pre">win_type</span></code>而不是显式地指定权重时,权重已经被归一化,使得最大权重为1。</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-199">相反,<code class="docutils literal"><span class="pre">.mean()</span></code>计算的性质使得权重相对于彼此被标准化。</span><span class="yiyi-st" id="yiyi-200"><code class="docutils literal"><span class="pre">[1,</span> <span class="pre">1,</span> <span class="pre">1]</span></code>和<code class="docutils literal"><span class="pre">[2, t5 > <span class="pre">2,</span> <span class="pre">2]</span></span></code>产生相同的结果。</span></p>
</div>
</div>
<div class="section" id="time-aware-rolling">
<span id="stats-moments-ts"></span><h3><span class="yiyi-st" id="yiyi-201">Time-aware Rolling</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-202"><span class="versionmodified">版本0.19.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-203">版本0.19.0中的新功能是将偏移量(或可转换)传递给<code class="docutils literal"><span class="pre">.rolling()</span></code>方法,并根据传递的时间窗口生成可变大小的窗口。</span><span class="yiyi-st" id="yiyi-204">对于每个时间点,这包括在指示的时间Δ内出现的所有先前值。</span></p>
<p><span class="yiyi-st" id="yiyi-205">这对于非规则的时间频率指数特别有用。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [56]: </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="s1">'B'</span><span class="p">:</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="mi">2</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mi">4</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 09:00:00'</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">'s'</span><span class="p">))</span>
<span class="gp"> ....:</span>
<span class="gp">In [57]: </span><span class="n">dft</span>
<span class="gr">Out[57]: </span>
<span class="go"> B</span>
<span class="go">2013-01-01 09:00:00 0.0</span>
<span class="go">2013-01-01 09:00:01 1.0</span>
<span class="go">2013-01-01 09:00:02 2.0</span>
<span class="go">2013-01-01 09:00:03 NaN</span>
<span class="go">2013-01-01 09:00:04 4.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-206">这是一个常规的频率指数。</span><span class="yiyi-st" id="yiyi-207">使用整数窗口参数可以沿窗口频率滚动。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [58]: </span><span class="n">dft</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[58]: </span>
<span class="go"> B</span>
<span class="go">2013-01-01 09:00:00 NaN</span>
<span class="go">2013-01-01 09:00:01 1.0</span>
<span class="go">2013-01-01 09:00:02 3.0</span>
<span class="go">2013-01-01 09:00:03 NaN</span>
<span class="go">2013-01-01 09:00:04 NaN</span>
<span class="gp">In [59]: </span><span class="n">dft</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">min_periods</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[59]: </span>
<span class="go"> B</span>
<span class="go">2013-01-01 09:00:00 0.0</span>
<span class="go">2013-01-01 09:00:01 1.0</span>
<span class="go">2013-01-01 09:00:02 3.0</span>
<span class="go">2013-01-01 09:00:03 2.0</span>
<span class="go">2013-01-01 09:00:04 4.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-208">指定偏移允许更直观地规定滚动频率。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [60]: </span><span class="n">dft</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="s1">'2s'</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[60]: </span>
<span class="go"> B</span>
<span class="go">2013-01-01 09:00:00 0.0</span>
<span class="go">2013-01-01 09:00:01 1.0</span>
<span class="go">2013-01-01 09:00:02 3.0</span>
<span class="go">2013-01-01 09:00:03 2.0</span>
<span class="go">2013-01-01 09:00:04 4.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-209">使用非规则但仍然是单调的索引,使用整数窗口滚动不会赋予任何特殊的计算。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [61]: </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="s1">'B'</span><span class="p">:</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="mi">2</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mi">4</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">Index</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">'20130101 09:00:00'</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'20130101 09:00:02'</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'20130101 09:00:03'</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'20130101 09:00:05'</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">'20130101 09:00:06'</span><span class="p">)],</span>
<span class="gp"> ....:</span> <span class="n">name</span><span class="o">=</span><span class="s1">'foo'</span><span class="p">))</span>
<span class="gp"> ....:</span>
<span class="gp">In [62]: </span><span class="n">dft</span>
<span class="gr">Out[62]: </span>
<span class="go"> B</span>
<span class="go">foo </span>
<span class="go">2013-01-01 09:00:00 0.0</span>
<span class="go">2013-01-01 09:00:02 1.0</span>
<span class="go">2013-01-01 09:00:03 2.0</span>
<span class="go">2013-01-01 09:00:05 NaN</span>
<span class="go">2013-01-01 09:00:06 4.0</span>
<span class="gp">In [63]: </span><span class="n">dft</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[63]: </span>
<span class="go"> B</span>
<span class="go">foo </span>
<span class="go">2013-01-01 09:00:00 NaN</span>
<span class="go">2013-01-01 09:00:02 1.0</span>
<span class="go">2013-01-01 09:00:03 3.0</span>
<span class="go">2013-01-01 09:00:05 NaN</span>
<span class="go">2013-01-01 09:00:06 NaN</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-210">使用时间规范为这个稀疏数据生成变量窗口。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [64]: </span><span class="n">dft</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="s1">'2s'</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[64]: </span>
<span class="go"> B</span>
<span class="go">foo </span>
<span class="go">2013-01-01 09:00:00 0.0</span>
<span class="go">2013-01-01 09:00:02 1.0</span>
<span class="go">2013-01-01 09:00:03 3.0</span>
<span class="go">2013-01-01 09:00:05 NaN</span>
<span class="go">2013-01-01 09:00:06 4.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-211">此外,我们现在允许一个可选的<code class="docutils literal"><span class="pre">on</span></code>参数在DataFrame中指定一个列(而不是索引的默认值)。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [65]: </span><span class="n">dft</span> <span class="o">=</span> <span class="n">dft</span><span class="o">.</span><span class="n">reset_index</span><span class="p">()</span>
<span class="gp">In [66]: </span><span class="n">dft</span>
<span class="gr">Out[66]: </span>
<span class="go"> foo B</span>
<span class="go">0 2013-01-01 09:00:00 0.0</span>
<span class="go">1 2013-01-01 09:00:02 1.0</span>
<span class="go">2 2013-01-01 09:00:03 2.0</span>
<span class="go">3 2013-01-01 09:00:05 NaN</span>
<span class="go">4 2013-01-01 09:00:06 4.0</span>
<span class="gp">In [67]: </span><span class="n">dft</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="s1">'2s'</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s1">'foo'</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[67]: </span>
<span class="go"> foo B</span>
<span class="go">0 2013-01-01 09:00:00 0.0</span>
<span class="go">1 2013-01-01 09:00:02 1.0</span>
<span class="go">2 2013-01-01 09:00:03 3.0</span>
<span class="go">3 2013-01-01 09:00:05 NaN</span>
<span class="go">4 2013-01-01 09:00:06 4.0</span>
</pre></div>
</div>
</div>
<div class="section" id="time-aware-rolling-vs-resampling">
<span id="stats-moments-ts-versus-resampling"></span><h3><span class="yiyi-st" id="yiyi-212">Time-aware Rolling vs. Resampling</span></h3>
<p><span class="yiyi-st" id="yiyi-213">使用<code class="docutils literal"><span class="pre">.rolling()</span></code>与基于时间的索引非常类似于<a class="reference internal" href="timeseries.html#timeseries-resampling"><span class="std std-ref">resampling</span></a>。</span><span class="yiyi-st" id="yiyi-214">它们都对时间索引的pandas对象进行操作和执行还原操作。</span></p>
<p><span class="yiyi-st" id="yiyi-215">当使用带偏移的<code class="docutils literal"><span class="pre">.rolling()</span></code>时。</span><span class="yiyi-st" id="yiyi-216">偏移量是时间增量。</span><span class="yiyi-st" id="yiyi-217">采取后向时间窗口,并聚合该窗口中的所有值(包括端点,但不是开始点)。</span><span class="yiyi-st" id="yiyi-218">这是在结果中的那个点的新值。</span><span class="yiyi-st" id="yiyi-219">这些是在输入的每个点的时间空间中的可变大小的窗口。</span><span class="yiyi-st" id="yiyi-220">您将获得与输入相同大小的结果。</span></p>
<p><span class="yiyi-st" id="yiyi-221">当使用带偏移量的<code class="docutils literal"><span class="pre">.resample()</span></code>时。</span><span class="yiyi-st" id="yiyi-222">构造一个新的索引,即偏移的频率。</span><span class="yiyi-st" id="yiyi-223">对于每个频率仓,在落入该仓中的向后时间查看窗口内的来自输入的聚合点。</span><span class="yiyi-st" id="yiyi-224">此聚合的结果是该频点的输出。</span><span class="yiyi-st" id="yiyi-225">窗口在频率空间中是固定大小尺寸。</span><span class="yiyi-st" id="yiyi-226">您的结果将具有在原始输入对象的最小值和最大值之间的常规频率的形状。</span></p>
<p><span class="yiyi-st" id="yiyi-227">总而言之,<code class="docutils literal"><span class="pre">.rolling()</span></code>是基于时间的窗口操作,而<code class="docutils literal"><span class="pre">.resample()</span></code>是基于频率的窗口操作。</span></p>
</div>
<div class="section" id="centering-windows">
<h3><span class="yiyi-st" id="yiyi-228">Centering Windows</span></h3>
<p><span class="yiyi-st" id="yiyi-229">默认情况下,标签设置为窗口的右边缘,但是可以使用<code class="docutils literal"><span class="pre">center</span></code>关键字,因此可以将标签设置在中心。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [68]: </span><span class="n">ser</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[68]: </span>
<span class="go">2000-01-01 NaN</span>
<span class="go">2000-01-02 NaN</span>
<span class="go">2000-01-03 NaN</span>
<span class="go">2000-01-04 NaN</span>
<span class="go">2000-01-05 -0.841164</span>
<span class="go">2000-01-06 -0.779948</span>
<span class="go">2000-01-07 -0.565487</span>
<span class="go">2000-01-08 -0.502815</span>
<span class="go">2000-01-09 -0.553755</span>
<span class="go">2000-01-10 -0.472211</span>
<span class="go">Freq: D, dtype: float64</span>
<span class="gp">In [69]: </span><span class="n">ser</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">center</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[69]: </span>
<span class="go">2000-01-01 NaN</span>
<span class="go">2000-01-02 NaN</span>
<span class="go">2000-01-03 -0.841164</span>
<span class="go">2000-01-04 -0.779948</span>
<span class="go">2000-01-05 -0.565487</span>
<span class="go">2000-01-06 -0.502815</span>
<span class="go">2000-01-07 -0.553755</span>
<span class="go">2000-01-08 -0.472211</span>
<span class="go">2000-01-09 NaN</span>
<span class="go">2000-01-10 NaN</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
</div>
<div class="section" id="binary-window-functions">
<span id="stats-moments-binary"></span><h3><span class="yiyi-st" id="yiyi-230">Binary Window Functions</span></h3>
<p><span class="yiyi-st" id="yiyi-231"><a class="reference internal" href="generated/pandas.core.window.Rolling.cov.html#pandas.core.window.Rolling.cov" title="pandas.core.window.Rolling.cov"><code class="xref py py-meth docutils literal"><span class="pre">cov()</span></code></a>和<a class="reference internal" href="generated/pandas.core.window.Rolling.corr.html#pandas.core.window.Rolling.corr" title="pandas.core.window.Rolling.corr"><code class="xref py py-meth docutils literal"><span class="pre">corr()</span></code></a>可以计算关于两个<code class="docutils literal"><span class="pre">Series</span></code>或<code class="docutils literal"><span class="pre">DataFrame/Series</span></code>或<code class="docutils literal"><span class="pre">DataFrame/DataFrame</span></code>。</span><span class="yiyi-st" id="yiyi-232">这里是每种情况下的行为:</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-233">两个<code class="docutils literal"><span class="pre">Series</span></code>:计算配对的统计信息。</span></li>
<li><span class="yiyi-st" id="yiyi-234"><code class="docutils literal"><span class="pre">DataFrame/Series</span></code>:使用传递的Series计算DataFrame的每个列的统计信息,从而返回一个DataFrame。</span></li>
<li><span class="yiyi-st" id="yiyi-235"><code class="docutils literal"><span class="pre">DataFrame/DataFrame</span></code>:默认情况下计算匹配列名称的统计信息,返回DataFrame。</span><span class="yiyi-st" id="yiyi-236">如果传递关键字参数<code class="docutils literal"><span class="pre">pairwise=True</span></code>,则计算每对列的统计量,返回<code class="docutils literal"><span class="pre">Panel</span></code>,其<code class="docutils literal"><span class="pre">items</span></code> (见<a class="reference internal" href="#stats-moments-corr-pairwise"><span class="std std-ref">the next section</span></a>)。</span></li>
</ul>
<p><span class="yiyi-st" id="yiyi-237">例如:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [70]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">df</span><span class="p">[:</span><span class="mi">20</span><span class="p">]</span>
<span class="gp">In [71]: </span><span class="n">df2</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span><span class="o">.</span><span class="n">corr</span><span class="p">(</span><span class="n">df2</span><span class="p">[</span><span class="s1">'B'</span><span class="p">])</span>
<span class="gr">Out[71]: </span>
<span class="go"> A B C D</span>
<span class="go">2000-01-01 NaN NaN NaN NaN</span>
<span class="go">2000-01-02 NaN NaN NaN NaN</span>
<span class="go">2000-01-03 NaN NaN NaN NaN</span>
<span class="go">2000-01-04 NaN NaN NaN NaN</span>
<span class="go">2000-01-05 -0.262853 1.0 0.334449 0.193380</span>
<span class="go">2000-01-06 -0.083745 1.0 -0.521587 -0.556126</span>
<span class="go">2000-01-07 -0.292940 1.0 -0.658532 -0.458128</span>
<span class="go">... ... ... ... ...</span>
<span class="go">2000-01-14 0.519499 1.0 -0.687277 0.192822</span>
<span class="go">2000-01-15 0.048982 1.0 0.167669 -0.061463</span>
<span class="go">2000-01-16 0.217190 1.0 0.167564 -0.326034</span>
<span class="go">2000-01-17 0.641180 1.0 -0.164780 -0.111487</span>
<span class="go">2000-01-18 0.130422 1.0 0.322833 0.632383</span>
<span class="go">2000-01-19 0.317278 1.0 0.384528 0.813656</span>
<span class="go">2000-01-20 0.293598 1.0 0.159538 0.742381</span>
<span class="go">[20 rows x 4 columns]</span>
</pre></div>
</div>
</div>
<div class="section" id="computing-rolling-pairwise-covariances-and-correlations">
<span id="stats-moments-corr-pairwise"></span><h3><span class="yiyi-st" id="yiyi-238">Computing rolling pairwise covariances and correlations</span></h3>
<p><span class="yiyi-st" id="yiyi-239">在金融数据分析和其他领域中,通常对于时间序列的集合计算协方差和相关矩阵。</span><span class="yiyi-st" id="yiyi-240">通常,人们也对移动窗协方差和相关矩阵感兴趣。</span><span class="yiyi-st" id="yiyi-241">This can be done by passing the <code class="docutils literal"><span class="pre">pairwise</span></code> keyword argument, which in the case of <code class="docutils literal"><span class="pre">DataFrame</span></code> inputs will yield a <code class="docutils literal"><span class="pre">Panel</span></code> whose <code class="docutils literal"><span class="pre">items</span></code> are the dates in question. </span><span class="yiyi-st" id="yiyi-242">在单个DataFrame参数的情况下,甚至可以省略<code class="docutils literal"><span class="pre">pairwise</span></code>参数:</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-243">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-244">将忽略缺少的值,并使用成对完全观察值计算每个条目。</span><span class="yiyi-st" id="yiyi-245">请参阅与此计算协方差和相关矩阵的方法相关联的<a class="reference internal" href="#computation-covariance-caveats"><span class="std std-ref">caveats</span></a>的<a class="reference internal" href="#computation-covariance"><span class="std std-ref">covariance section</span></a>。</span></p>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [72]: </span><span class="n">covs</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="s1">'C'</span><span class="p">,</span><span class="s1">'D'</span><span class="p">]]</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span><span class="o">.</span><span class="n">cov</span><span class="p">(</span><span class="n">df</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="s1">'C'</span><span class="p">]],</span> <span class="n">pairwise</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">In [73]: </span><span class="n">covs</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="o">-</span><span class="mi">50</span><span class="p">]]</span>
<span class="gr">Out[73]: </span>
<span class="go"> A B C</span>
<span class="go">B 2.667506 1.671711 1.938634</span>
<span class="go">C 8.513843 1.938634 10.556436</span>
<span class="go">D -7.714737 -1.434529 -7.082653</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [74]: </span><span class="n">correls</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span><span class="o">.</span><span class="n">corr</span><span class="p">()</span>
<span class="gp">In [75]: </span><span class="n">correls</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="o">-</span><span class="mi">50</span><span class="p">]]</span>
<span class="gr">Out[75]: </span>
<span class="go"> A B C D</span>
<span class="go">A 1.000000 0.604221 0.767429 -0.776170</span>
<span class="go">B 0.604221 1.000000 0.461484 -0.381148</span>
<span class="go">C 0.767429 0.461484 1.000000 -0.748863</span>
<span class="go">D -0.776170 -0.381148 -0.748863 1.000000</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-246">您可以使用<code class="docutils literal"><span class="pre">.loc</span></code>索引有效地检索两列之间的相关性的时间序列:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [76]: </span><span class="n">correls</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="s1">'A'</span><span class="p">,</span> <span class="s1">'C'</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="p">()</span>
<span class="gr">Out[76]: </span><span class="o"><</span><span class="n">matplotlib</span><span class="o">.</span><span class="n">axes</span><span class="o">.</span><span class="n">_subplots</span><span class="o">.</span><span class="n">AxesSubplot</span> <span class="n">at</span> <span class="mh">0x7ff27e0f0c50</span><span class="o">></span>
</pre></div>
</div>
<img alt="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/rolling_corr_pairwise_ex.png" src="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/rolling_corr_pairwise_ex.png">
</div>
</div>
<div class="section" id="aggregation">
<span id="stats-aggregate"></span><h2><span class="yiyi-st" id="yiyi-247">Aggregation</span></h2>
<p><span class="yiyi-st" id="yiyi-248">一旦创建了<code class="docutils literal"><span class="pre">Rolling</span></code>,<code class="docutils literal"><span class="pre">Expanding</span></code>或<code class="docutils literal"><span class="pre">EWM</span></code>对象,就有几种方法可以对数据执行多个计算。</span><span class="yiyi-st" id="yiyi-249">这非常类似于<code class="docutils literal"><span class="pre">.groupby(...).agg</span></code>看到<a class="reference internal" href="groupby.html#groupby-aggregate"><span class="std std-ref">here</span></a>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [77]: </span><span class="n">dfa</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">1000</span><span class="p">,</span> <span class="mi">3</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">'1/1/2000'</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="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="s1">'B'</span><span class="p">,</span> <span class="s1">'C'</span><span class="p">])</span>
<span class="gp"> ....:</span>
<span class="gp">In [78]: </span><span class="n">r</span> <span class="o">=</span> <span class="n">dfa</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">60</span><span class="p">,</span><span class="n">min_periods</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">In [79]: </span><span class="n">r</span>
<span class="gr">Out[79]: </span><span class="n">Rolling</span> <span class="p">[</span><span class="n">window</span><span class="o">=</span><span class="mi">60</span><span class="p">,</span><span class="n">min_periods</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">center</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-250">我们可以通过传递一个函数到整个DataFrame,或通过标准的getitem选择一个系列(或多个系列)。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [80]: </span><span class="n">r</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">)</span>
<span class="gr">Out[80]: </span>
<span class="go"> A B C</span>
<span class="go">2000-01-01 0.314226 -0.001675 0.071823</span>
<span class="go">2000-01-02 1.206791 0.678918 -0.267817</span>
<span class="go">2000-01-03 1.421701 0.600508 -0.445482</span>
<span class="go">2000-01-04 1.912539 -0.759594 1.146974</span>
<span class="go">2000-01-05 2.919639 -0.061759 -0.743617</span>
<span class="go">2000-01-06 2.665637 1.298392 -0.803529</span>
<span class="go">2000-01-07 2.513985 1.923089 -1.928308</span>
<span class="go">... ... ... ...</span>
<span class="go">2002-09-20 1.447669 -12.360302 2.734381</span>
<span class="go">2002-09-21 1.871783 -13.896542 3.086102</span>
<span class="go">2002-09-22 2.540658 -12.594402 3.162542</span>
<span class="go">2002-09-23 2.974674 -12.727703 3.861005</span>
<span class="go">2002-09-24 1.391366 -13.584590 3.790683</span>
<span class="go">2002-09-25 2.027313 -15.083214 3.377896</span>
<span class="go">2002-09-26 1.290363 -13.569459 3.809884</span>
<span class="go">[1000 rows x 3 columns]</span>
<span class="gp">In [81]: </span><span class="n">r</span><span class="p">[</span><span class="s1">'A'</span><span class="p">]</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">)</span>
<span class="gr">Out[81]: </span>
<span class="go">2000-01-01 0.314226</span>
<span class="go">2000-01-02 1.206791</span>
<span class="go">2000-01-03 1.421701</span>
<span class="go">2000-01-04 1.912539</span>
<span class="go">2000-01-05 2.919639</span>
<span class="go">2000-01-06 2.665637</span>
<span class="go">2000-01-07 2.513985</span>
<span class="go"> ... </span>
<span class="go">2002-09-20 1.447669</span>
<span class="go">2002-09-21 1.871783</span>
<span class="go">2002-09-22 2.540658</span>
<span class="go">2002-09-23 2.974674</span>
<span class="go">2002-09-24 1.391366</span>
<span class="go">2002-09-25 2.027313</span>
<span class="go">2002-09-26 1.290363</span>
<span class="go">Freq: D, Name: A, dtype: float64</span>
<span class="gp">In [82]: </span><span class="n">r</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="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">)</span>
<span class="gr">Out[82]: </span>
<span class="go"> A B</span>
<span class="go">2000-01-01 0.314226 -0.001675</span>
<span class="go">2000-01-02 1.206791 0.678918</span>
<span class="go">2000-01-03 1.421701 0.600508</span>
<span class="go">2000-01-04 1.912539 -0.759594</span>
<span class="go">2000-01-05 2.919639 -0.061759</span>
<span class="go">2000-01-06 2.665637 1.298392</span>
<span class="go">2000-01-07 2.513985 1.923089</span>
<span class="go">... ... ...</span>
<span class="go">2002-09-20 1.447669 -12.360302</span>
<span class="go">2002-09-21 1.871783 -13.896542</span>
<span class="go">2002-09-22 2.540658 -12.594402</span>
<span class="go">2002-09-23 2.974674 -12.727703</span>
<span class="go">2002-09-24 1.391366 -13.584590</span>
<span class="go">2002-09-25 2.027313 -15.083214</span>
<span class="go">2002-09-26 1.290363 -13.569459</span>
<span class="go">[1000 rows x 2 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-251">正如您所看到的,聚合的结果将包含所选列,或者如果没有选择列,则包含所有列。</span></p>
<div class="section" id="applying-multiple-functions-at-once">
<span id="stats-aggregate-multifunc"></span><h3><span class="yiyi-st" id="yiyi-252">Applying multiple functions at once</span></h3>
<p><span class="yiyi-st" id="yiyi-253">使用windowed系列,您还可以传递函数的列表或字典以进行聚合,输出DataFrame:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [83]: </span><span class="n">r</span><span class="p">[</span><span class="s1">'A'</span><span class="p">]</span><span class="o">.</span><span class="n">agg</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">])</span>
<span class="gr">Out[83]: </span>
<span class="go"> sum mean std</span>
<span class="go">2000-01-01 0.314226 0.314226 NaN</span>
<span class="go">2000-01-02 1.206791 0.603396 0.408948</span>
<span class="go">2000-01-03 1.421701 0.473900 0.365959</span>
<span class="go">2000-01-04 1.912539 0.478135 0.298925</span>
<span class="go">2000-01-05 2.919639 0.583928 0.350682</span>
<span class="go">2000-01-06 2.665637 0.444273 0.464115</span>
<span class="go">2000-01-07 2.513985 0.359141 0.479828</span>
<span class="go">... ... ... ...</span>
<span class="go">2002-09-20 1.447669 0.024128 1.034827</span>
<span class="go">2002-09-21 1.871783 0.031196 1.031417</span>
<span class="go">2002-09-22 2.540658 0.042344 1.026341</span>
<span class="go">2002-09-23 2.974674 0.049578 1.030021</span>
<span class="go">2002-09-24 1.391366 0.023189 1.024793</span>
<span class="go">2002-09-25 2.027313 0.033789 1.022099</span>
<span class="go">2002-09-26 1.290363 0.021506 1.024751</span>
<span class="go">[1000 rows x 3 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-254">如果传递了dict,则键将用于命名列。</span><span class="yiyi-st" id="yiyi-255">否则将使用函数的名称(存储在函数对象中)。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [84]: </span><span class="n">r</span><span class="p">[</span><span class="s1">'A'</span><span class="p">]</span><span class="o">.</span><span class="n">agg</span><span class="p">({</span><span class="s1">'result1'</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">,</span>
<span class="gp"> ....:</span> <span class="s1">'result2'</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">})</span>
<span class="gp"> ....:</span>
<span class="gr">Out[84]: </span>
<span class="go"> result2 result1</span>
<span class="go">2000-01-01 0.314226 0.314226</span>
<span class="go">2000-01-02 0.603396 1.206791</span>
<span class="go">2000-01-03 0.473900 1.421701</span>
<span class="go">2000-01-04 0.478135 1.912539</span>
<span class="go">2000-01-05 0.583928 2.919639</span>
<span class="go">2000-01-06 0.444273 2.665637</span>
<span class="go">2000-01-07 0.359141 2.513985</span>
<span class="go">... ... ...</span>
<span class="go">2002-09-20 0.024128 1.447669</span>
<span class="go">2002-09-21 0.031196 1.871783</span>
<span class="go">2002-09-22 0.042344 2.540658</span>
<span class="go">2002-09-23 0.049578 2.974674</span>
<span class="go">2002-09-24 0.023189 1.391366</span>
<span class="go">2002-09-25 0.033789 2.027313</span>
<span class="go">2002-09-26 0.021506 1.290363</span>
<span class="go">[1000 rows x 2 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-256">在寡居式DataFrame上,您可以传递要应用于每个列的函数列表,这将生成带有层次索引的聚合结果:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [85]: </span><span class="n">r</span><span class="o">.</span><span class="n">agg</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">])</span>
<span class="gr">Out[85]: </span>
<span class="go"> A B C </span>
<span class="go"> sum mean sum mean sum mean</span>
<span class="go">2000-01-01 0.314226 0.314226 -0.001675 -0.001675 0.071823 0.071823</span>
<span class="go">2000-01-02 1.206791 0.603396 0.678918 0.339459 -0.267817 -0.133908</span>
<span class="go">2000-01-03 1.421701 0.473900 0.600508 0.200169 -0.445482 -0.148494</span>
<span class="go">2000-01-04 1.912539 0.478135 -0.759594 -0.189899 1.146974 0.286744</span>
<span class="go">2000-01-05 2.919639 0.583928 -0.061759 -0.012352 -0.743617 -0.148723</span>
<span class="go">2000-01-06 2.665637 0.444273 1.298392 0.216399 -0.803529 -0.133921</span>
<span class="go">2000-01-07 2.513985 0.359141 1.923089 0.274727 -1.928308 -0.275473</span>
<span class="go">... ... ... ... ... ... ...</span>
<span class="go">2002-09-20 1.447669 0.024128 -12.360302 -0.206005 2.734381 0.045573</span>
<span class="go">2002-09-21 1.871783 0.031196 -13.896542 -0.231609 3.086102 0.051435</span>
<span class="go">2002-09-22 2.540658 0.042344 -12.594402 -0.209907 3.162542 0.052709</span>
<span class="go">2002-09-23 2.974674 0.049578 -12.727703 -0.212128 3.861005 0.064350</span>
<span class="go">2002-09-24 1.391366 0.023189 -13.584590 -0.226410 3.790683 0.063178</span>
<span class="go">2002-09-25 2.027313 0.033789 -15.083214 -0.251387 3.377896 0.056298</span>
<span class="go">2002-09-26 1.290363 0.021506 -13.569459 -0.226158 3.809884 0.063498</span>
<span class="go">[1000 rows x 6 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-257">默认情况下,传递函数的dict具有不同的行为,请参见下一节。</span></p>
</div>
<div class="section" id="applying-different-functions-to-dataframe-columns">
<h3><span class="yiyi-st" id="yiyi-258">Applying different functions to DataFrame columns</span></h3>
<p><span class="yiyi-st" id="yiyi-259">通过将dict传递到<code class="docutils literal"><span class="pre">aggregate</span></code>,您可以对DataFrame的列应用不同的聚合:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [86]: </span><span class="n">r</span><span class="o">.</span><span class="n">agg</span><span class="p">({</span><span class="s1">'A'</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">,</span>
<span class="gp"> ....:</span> <span class="s1">'B'</span> <span class="p">:</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">ddof</span><span class="o">=</span><span class="mi">1</span><span class="p">)})</span>
<span class="gp"> ....:</span>
<span class="gr">Out[86]: </span>
<span class="go"> A B</span>
<span class="go">2000-01-01 0.314226 NaN</span>
<span class="go">2000-01-02 1.206791 0.482437</span>
<span class="go">2000-01-03 1.421701 0.417825</span>
<span class="go">2000-01-04 1.912539 0.851468</span>
<span class="go">2000-01-05 2.919639 0.837474</span>
<span class="go">2000-01-06 2.665637 0.935441</span>
<span class="go">2000-01-07 2.513985 0.867770</span>
<span class="go">... ... ...</span>
<span class="go">2002-09-20 1.447669 1.084259</span>
<span class="go">2002-09-21 1.871783 1.088368</span>
<span class="go">2002-09-22 2.540658 1.084707</span>
<span class="go">2002-09-23 2.974674 1.084936</span>
<span class="go">2002-09-24 1.391366 1.079268</span>
<span class="go">2002-09-25 2.027313 1.091334</span>
<span class="go">2002-09-26 1.290363 1.060255</span>
<span class="go">[1000 rows x 2 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-260">函数名也可以是字符串。</span><span class="yiyi-st" id="yiyi-261">为了使字符串有效,它必须在窗口对象上实现</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [87]: </span><span class="n">r</span><span class="o">.</span><span class="n">agg</span><span class="p">({</span><span class="s1">'A'</span> <span class="p">:</span> <span class="s1">'sum'</span><span class="p">,</span> <span class="s1">'B'</span> <span class="p">:</span> <span class="s1">'std'</span><span class="p">})</span>
<span class="gr">Out[87]: </span>
<span class="go"> A B</span>
<span class="go">2000-01-01 0.314226 NaN</span>
<span class="go">2000-01-02 1.206791 0.482437</span>
<span class="go">2000-01-03 1.421701 0.417825</span>
<span class="go">2000-01-04 1.912539 0.851468</span>
<span class="go">2000-01-05 2.919639 0.837474</span>
<span class="go">2000-01-06 2.665637 0.935441</span>
<span class="go">2000-01-07 2.513985 0.867770</span>
<span class="go">... ... ...</span>
<span class="go">2002-09-20 1.447669 1.084259</span>
<span class="go">2002-09-21 1.871783 1.088368</span>
<span class="go">2002-09-22 2.540658 1.084707</span>
<span class="go">2002-09-23 2.974674 1.084936</span>
<span class="go">2002-09-24 1.391366 1.079268</span>
<span class="go">2002-09-25 2.027313 1.091334</span>
<span class="go">2002-09-26 1.290363 1.060255</span>
<span class="go">[1000 rows x 2 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-262">此外,您可以传递嵌套dict以指示不同列上的不同聚合。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [88]: </span><span class="n">r</span><span class="o">.</span><span class="n">agg</span><span class="p">({</span><span class="s1">'A'</span> <span class="p">:</span> <span class="p">[</span><span class="s1">'sum'</span><span class="p">,</span><span class="s1">'std'</span><span class="p">],</span> <span class="s1">'B'</span> <span class="p">:</span> <span class="p">[</span><span class="s1">'mean'</span><span class="p">,</span><span class="s1">'std'</span><span class="p">]</span> <span class="p">})</span>
<span class="gr">Out[88]: </span>
<span class="go"> A B </span>
<span class="go"> sum std mean std</span>
<span class="go">2000-01-01 0.314226 NaN -0.001675 NaN</span>
<span class="go">2000-01-02 1.206791 0.408948 0.339459 0.482437</span>
<span class="go">2000-01-03 1.421701 0.365959 0.200169 0.417825</span>
<span class="go">2000-01-04 1.912539 0.298925 -0.189899 0.851468</span>
<span class="go">2000-01-05 2.919639 0.350682 -0.012352 0.837474</span>
<span class="go">2000-01-06 2.665637 0.464115 0.216399 0.935441</span>
<span class="go">2000-01-07 2.513985 0.479828 0.274727 0.867770</span>
<span class="go">... ... ... ... ...</span>
<span class="go">2002-09-20 1.447669 1.034827 -0.206005 1.084259</span>
<span class="go">2002-09-21 1.871783 1.031417 -0.231609 1.088368</span>
<span class="go">2002-09-22 2.540658 1.026341 -0.209907 1.084707</span>
<span class="go">2002-09-23 2.974674 1.030021 -0.212128 1.084936</span>
<span class="go">2002-09-24 1.391366 1.024793 -0.226410 1.079268</span>
<span class="go">2002-09-25 2.027313 1.022099 -0.251387 1.091334</span>
<span class="go">2002-09-26 1.290363 1.024751 -0.226158 1.060255</span>
<span class="go">[1000 rows x 4 columns]</span>