26
26
from pandas .util ._decorators import doc
27
27
from pandas .util ._exceptions import find_stack_level
28
28
29
- from pandas .core .dtypes .common import is_datetime64_ns_dtype
29
+ from pandas .core .dtypes .common import (
30
+ is_datetime64_ns_dtype ,
31
+ is_numeric_dtype ,
32
+ )
30
33
from pandas .core .dtypes .missing import isna
31
34
32
35
import pandas .core .common as common # noqa: PDF018
45
48
args_compat ,
46
49
create_section_header ,
47
50
kwargs_compat ,
51
+ kwargs_numeric_only ,
48
52
numba_notes ,
49
53
template_header ,
50
54
template_returns ,
@@ -518,6 +522,7 @@ def aggregate(self, func, *args, **kwargs):
518
522
@doc (
519
523
template_header ,
520
524
create_section_header ("Parameters" ),
525
+ kwargs_numeric_only ,
521
526
args_compat ,
522
527
window_agg_numba_parameters (),
523
528
kwargs_compat ,
@@ -531,7 +536,14 @@ def aggregate(self, func, *args, **kwargs):
531
536
aggregation_description = "(exponential weighted moment) mean" ,
532
537
agg_method = "mean" ,
533
538
)
534
- def mean (self , * args , engine = None , engine_kwargs = None , ** kwargs ):
539
+ def mean (
540
+ self ,
541
+ numeric_only : bool = False ,
542
+ * args ,
543
+ engine = None ,
544
+ engine_kwargs = None ,
545
+ ** kwargs ,
546
+ ):
535
547
if maybe_use_numba (engine ):
536
548
if self .method == "single" :
537
549
func = generate_numba_ewm_func
@@ -545,7 +557,7 @@ def mean(self, *args, engine=None, engine_kwargs=None, **kwargs):
545
557
deltas = tuple (self ._deltas ),
546
558
normalize = True ,
547
559
)
548
- return self ._apply (ewm_func )
560
+ return self ._apply (ewm_func , name = "mean" )
549
561
elif engine in ("cython" , None ):
550
562
if engine_kwargs is not None :
551
563
raise ValueError ("cython engine does not accept engine_kwargs" )
@@ -560,13 +572,14 @@ def mean(self, *args, engine=None, engine_kwargs=None, **kwargs):
560
572
deltas = deltas ,
561
573
normalize = True ,
562
574
)
563
- return self ._apply (window_func )
575
+ return self ._apply (window_func , name = "mean" , numeric_only = numeric_only )
564
576
else :
565
577
raise ValueError ("engine must be either 'numba' or 'cython'" )
566
578
567
579
@doc (
568
580
template_header ,
569
581
create_section_header ("Parameters" ),
582
+ kwargs_numeric_only ,
570
583
args_compat ,
571
584
window_agg_numba_parameters (),
572
585
kwargs_compat ,
@@ -580,7 +593,14 @@ def mean(self, *args, engine=None, engine_kwargs=None, **kwargs):
580
593
aggregation_description = "(exponential weighted moment) sum" ,
581
594
agg_method = "sum" ,
582
595
)
583
- def sum (self , * args , engine = None , engine_kwargs = None , ** kwargs ):
596
+ def sum (
597
+ self ,
598
+ numeric_only : bool = False ,
599
+ * args ,
600
+ engine = None ,
601
+ engine_kwargs = None ,
602
+ ** kwargs ,
603
+ ):
584
604
if not self .adjust :
585
605
raise NotImplementedError ("sum is not implemented with adjust=False" )
586
606
if maybe_use_numba (engine ):
@@ -596,7 +616,7 @@ def sum(self, *args, engine=None, engine_kwargs=None, **kwargs):
596
616
deltas = tuple (self ._deltas ),
597
617
normalize = False ,
598
618
)
599
- return self ._apply (ewm_func )
619
+ return self ._apply (ewm_func , name = "sum" )
600
620
elif engine in ("cython" , None ):
601
621
if engine_kwargs is not None :
602
622
raise ValueError ("cython engine does not accept engine_kwargs" )
@@ -611,7 +631,7 @@ def sum(self, *args, engine=None, engine_kwargs=None, **kwargs):
611
631
deltas = deltas ,
612
632
normalize = False ,
613
633
)
614
- return self ._apply (window_func )
634
+ return self ._apply (window_func , name = "sum" , numeric_only = numeric_only )
615
635
else :
616
636
raise ValueError ("engine must be either 'numba' or 'cython'" )
617
637
@@ -624,6 +644,7 @@ def sum(self, *args, engine=None, engine_kwargs=None, **kwargs):
624
644
Use a standard estimation bias correction.
625
645
"""
626
646
).replace ("\n " , "" , 1 ),
647
+ kwargs_numeric_only ,
627
648
args_compat ,
628
649
kwargs_compat ,
629
650
create_section_header ("Returns" ),
@@ -634,9 +655,18 @@ def sum(self, *args, engine=None, engine_kwargs=None, **kwargs):
634
655
aggregation_description = "(exponential weighted moment) standard deviation" ,
635
656
agg_method = "std" ,
636
657
)
637
- def std (self , bias : bool = False , * args , ** kwargs ):
658
+ def std (self , bias : bool = False , numeric_only : bool = False , * args , ** kwargs ):
638
659
nv .validate_window_func ("std" , args , kwargs )
639
- return zsqrt (self .var (bias = bias , ** kwargs ))
660
+ if (
661
+ numeric_only
662
+ and self ._selected_obj .ndim == 1
663
+ and not is_numeric_dtype (self ._selected_obj .dtype )
664
+ ):
665
+ # Raise directly so error message says std instead of var
666
+ raise NotImplementedError (
667
+ f"{ type (self ).__name__ } .std does not implement numeric_only"
668
+ )
669
+ return zsqrt (self .var (bias = bias , numeric_only = numeric_only , ** kwargs ))
640
670
641
671
def vol (self , bias : bool = False , * args , ** kwargs ):
642
672
warnings .warn (
@@ -658,6 +688,7 @@ def vol(self, bias: bool = False, *args, **kwargs):
658
688
Use a standard estimation bias correction.
659
689
"""
660
690
).replace ("\n " , "" , 1 ),
691
+ kwargs_numeric_only ,
661
692
args_compat ,
662
693
kwargs_compat ,
663
694
create_section_header ("Returns" ),
@@ -668,7 +699,7 @@ def vol(self, bias: bool = False, *args, **kwargs):
668
699
aggregation_description = "(exponential weighted moment) variance" ,
669
700
agg_method = "var" ,
670
701
)
671
- def var (self , bias : bool = False , * args , ** kwargs ):
702
+ def var (self , bias : bool = False , numeric_only : bool = False , * args , ** kwargs ):
672
703
nv .validate_window_func ("var" , args , kwargs )
673
704
window_func = window_aggregations .ewmcov
674
705
wfunc = partial (
@@ -682,7 +713,7 @@ def var(self, bias: bool = False, *args, **kwargs):
682
713
def var_func (values , begin , end , min_periods ):
683
714
return wfunc (values , begin , end , min_periods , values )
684
715
685
- return self ._apply (var_func )
716
+ return self ._apply (var_func , name = "var" , numeric_only = numeric_only )
686
717
687
718
@doc (
688
719
template_header ,
@@ -703,6 +734,7 @@ def var_func(values, begin, end, min_periods):
703
734
Use a standard estimation bias correction.
704
735
"""
705
736
).replace ("\n " , "" , 1 ),
737
+ kwargs_numeric_only ,
706
738
kwargs_compat ,
707
739
create_section_header ("Returns" ),
708
740
template_returns ,
@@ -717,10 +749,13 @@ def cov(
717
749
other : DataFrame | Series | None = None ,
718
750
pairwise : bool | None = None ,
719
751
bias : bool = False ,
752
+ numeric_only : bool = False ,
720
753
** kwargs ,
721
754
):
722
755
from pandas import Series
723
756
757
+ self ._validate_numeric_only ("cov" , numeric_only )
758
+
724
759
def cov_func (x , y ):
725
760
x_array = self ._prep_values (x )
726
761
y_array = self ._prep_values (y )
@@ -752,7 +787,9 @@ def cov_func(x, y):
752
787
)
753
788
return Series (result , index = x .index , name = x .name )
754
789
755
- return self ._apply_pairwise (self ._selected_obj , other , pairwise , cov_func )
790
+ return self ._apply_pairwise (
791
+ self ._selected_obj , other , pairwise , cov_func , numeric_only
792
+ )
756
793
757
794
@doc (
758
795
template_header ,
@@ -771,6 +808,7 @@ def cov_func(x, y):
771
808
observations will be used.
772
809
"""
773
810
).replace ("\n " , "" , 1 ),
811
+ kwargs_numeric_only ,
774
812
kwargs_compat ,
775
813
create_section_header ("Returns" ),
776
814
template_returns ,
@@ -784,10 +822,13 @@ def corr(
784
822
self ,
785
823
other : DataFrame | Series | None = None ,
786
824
pairwise : bool | None = None ,
825
+ numeric_only : bool = False ,
787
826
** kwargs ,
788
827
):
789
828
from pandas import Series
790
829
830
+ self ._validate_numeric_only ("corr" , numeric_only )
831
+
791
832
def cov_func (x , y ):
792
833
x_array = self ._prep_values (x )
793
834
y_array = self ._prep_values (y )
@@ -825,7 +866,9 @@ def _cov(X, Y):
825
866
result = cov / zsqrt (x_var * y_var )
826
867
return Series (result , index = x .index , name = x .name )
827
868
828
- return self ._apply_pairwise (self ._selected_obj , other , pairwise , cov_func )
869
+ return self ._apply_pairwise (
870
+ self ._selected_obj , other , pairwise , cov_func , numeric_only
871
+ )
829
872
830
873
831
874
class ExponentialMovingWindowGroupby (BaseWindowGroupby , ExponentialMovingWindow ):
@@ -921,6 +964,7 @@ def corr(
921
964
self ,
922
965
other : DataFrame | Series | None = None ,
923
966
pairwise : bool | None = None ,
967
+ numeric_only : bool = False ,
924
968
** kwargs ,
925
969
):
926
970
return NotImplementedError
@@ -930,6 +974,7 @@ def cov(
930
974
other : DataFrame | Series | None = None ,
931
975
pairwise : bool | None = None ,
932
976
bias : bool = False ,
977
+ numeric_only : bool = False ,
933
978
** kwargs ,
934
979
):
935
980
return NotImplementedError
0 commit comments