@@ -673,9 +673,17 @@ def var(self, ddof=1):
673
673
def size (self ):
674
674
"""
675
675
Compute group sizes
676
+
676
677
"""
677
678
return self .grouper .size ()
678
679
680
+ def count (self ):
681
+ """
682
+ Number of non-null items in each group.
683
+
684
+ """
685
+ return self ._python_agg_general (lambda x : notnull (x ).sum ())
686
+
679
687
sum = _groupby_function ('sum' , 'add' , np .sum )
680
688
prod = _groupby_function ('prod' , 'prod' , np .prod )
681
689
min = _groupby_function ('min' , 'min' , np .min , numeric_only = False )
@@ -687,12 +695,10 @@ def size(self):
687
695
688
696
def ohlc (self ):
689
697
"""
690
- Compute sum of values, excluding missing values
691
-
692
- For multiple groupings, the result index will be a MultiIndex
698
+ Deprecated, use .resample(how="ohlc") instead.
693
699
694
700
"""
695
- return self . _cython_agg_general ('ohlc' )
701
+ raise AttributeError ('ohlc is deprecated, use resample(how="ohlc"). ' )
696
702
697
703
def nth (self , n , dropna = None ):
698
704
"""
@@ -939,6 +945,7 @@ def _cython_agg_general(self, how, numeric_only=True):
939
945
result , names = self .grouper .aggregate (obj .values , how )
940
946
except AssertionError as e :
941
947
raise GroupByError (str (e ))
948
+ # infer old dytpe
942
949
output [name ] = self ._try_cast (result , obj )
943
950
944
951
if len (output ) == 0 :
@@ -947,6 +954,8 @@ def _cython_agg_general(self, how, numeric_only=True):
947
954
return self ._wrap_aggregated_output (output , names )
948
955
949
956
def _python_agg_general (self , func , * args , ** kwargs ):
957
+ _dtype = kwargs .pop ("_dtype" , None )
958
+
950
959
func = _intercept_function (func )
951
960
f = lambda x : func (x , * args , ** kwargs )
952
961
@@ -955,7 +964,14 @@ def _python_agg_general(self, func, *args, **kwargs):
955
964
for name , obj in self ._iterate_slices ():
956
965
try :
957
966
result , counts = self .grouper .agg_series (obj , f )
958
- output [name ] = self ._try_cast (result , obj )
967
+
968
+ if _dtype is None : # infer old dytpe
969
+ output [name ] = self ._try_cast (result , obj )
970
+ elif _dtype is False :
971
+ output [name ] = result
972
+ else :
973
+ output [name ] = _possibly_downcast_to_dtype (result , _dtype )
974
+
959
975
except TypeError :
960
976
continue
961
977
@@ -2889,16 +2905,6 @@ def _apply_to_column_groupbys(self, func):
2889
2905
in self ._iterate_column_groupbys ()),
2890
2906
keys = self ._selected_obj .columns , axis = 1 )
2891
2907
2892
- def ohlc (self ):
2893
- """
2894
- Compute sum of values, excluding missing values
2895
-
2896
- For multiple groupings, the result index will be a MultiIndex
2897
- """
2898
- return self ._apply_to_column_groupbys (
2899
- lambda x : x ._cython_agg_general ('ohlc' ))
2900
-
2901
-
2902
2908
from pandas .tools .plotting import boxplot_frame_groupby
2903
2909
DataFrameGroupBy .boxplot = boxplot_frame_groupby
2904
2910
0 commit comments