@@ -841,7 +841,7 @@ def to_frame(self, filter_observations=True):
841
841
to_long = deprecate ('to_long' , to_frame )
842
842
toLong = deprecate ('toLong' , to_frame )
843
843
844
- def apply (self , func , axis = 'major' , args = (), ** kwargs ):
844
+ def apply (self , func , axis = 'major' , ** kwargs ):
845
845
"""
846
846
Applies function along input axis of the Panel
847
847
@@ -852,9 +852,6 @@ def apply(self, func, axis='major', args=(), **kwargs):
852
852
e.g. if axis = 'items', then the combination of major_axis/minor_axis
853
853
will be passed a Series
854
854
axis : {'major', 'minor', 'items'}
855
- args : tuple
856
- Positional arguments to pass to function in addition to the
857
- array/series
858
855
Additional keyword arguments will be passed as keywords to the function
859
856
860
857
Examples
@@ -868,25 +865,36 @@ def apply(self, func, axis='major', args=(), **kwargs):
868
865
-------
869
866
result : Pandas Object
870
867
"""
871
- axis = self ._get_axis_number (axis )
872
- axis_name = self ._get_axis_name (axis )
873
- ax = self ._get_axis (axis )
874
- values = self .values
875
- ndim = self .ndim
876
868
877
- if args or kwargs and not isinstance (func , np .ufunc ):
878
- f = lambda x : func (x , * args , * *kwargs )
869
+ if kwargs and not isinstance (func , np .ufunc ):
870
+ f = lambda x : func (x , ** kwargs )
879
871
else :
880
872
f = func
881
873
874
+ # 2d-slabs
875
+ if isinstance (axis , (tuple ,list )) and len (axis ) == 2 :
876
+ return self ._apply_2d (f , axis = axis )
877
+
878
+ axis = self ._get_axis_number (axis )
879
+
882
880
# try ufunc like
883
881
if isinstance (f , np .ufunc ):
884
882
try :
885
- result = np .apply_along_axis (func , axis , values )
883
+ result = np .apply_along_axis (func , axis , self . values )
886
884
return self ._wrap_result (result , axis = axis )
887
885
except (AttributeError ):
888
886
pass
889
887
888
+ # 1d
889
+ return self ._apply_1d (f , axis = axis )
890
+
891
+ def _apply_1d (self , func , axis ):
892
+
893
+ axis_name = self ._get_axis_name (axis )
894
+ ax = self ._get_axis (axis )
895
+ ndim = self .ndim
896
+ values = self .values
897
+
890
898
# iter thru the axes
891
899
slice_axis = self ._get_axis (axis )
892
900
slice_indexer = [0 ]* (ndim - 1 )
@@ -902,14 +910,14 @@ def apply(self, func, axis='major', args=(), **kwargs):
902
910
points = cartesian_product (planes )
903
911
904
912
results = []
905
- for i in xrange (np .prod (shape )):
913
+ for i in range (np .prod (shape )):
906
914
907
915
# construct the object
908
916
pts = tuple ([ p [i ] for p in points ])
909
917
indexer .put (indlist , slice_indexer )
910
918
911
919
obj = Series (values [tuple (indexer )],index = slice_axis ,name = pts )
912
- result = func (obj , * args , ** kwargs )
920
+ result = func (obj )
913
921
914
922
results .append (result )
915
923
@@ -940,6 +948,32 @@ def apply(self, func, axis='major', args=(), **kwargs):
940
948
planes = planes [::- 1 ]
941
949
return self ._construct_return_type (results ,planes )
942
950
951
+ def _apply_2d (self , func , axis ):
952
+ """ handle 2-d slices, equiv to iterating over the other axis """
953
+
954
+ ndim = self .ndim
955
+ axis = [ self ._get_axis_number (a ) for a in axis ]
956
+
957
+ # construct slabs, in 2-d this is a DataFrame result
958
+ indexer_axis = list (range (ndim ))
959
+ for a in axis :
960
+ indexer_axis .remove (a )
961
+ indexer_axis = indexer_axis [0 ]
962
+
963
+ slicer = [ slice (None ,None ) ] * ndim
964
+ ax = self ._get_axis (indexer_axis )
965
+
966
+ results = []
967
+ for i , e in enumerate (ax ):
968
+
969
+ slicer [indexer_axis ] = i
970
+ sliced = self .iloc [tuple (slicer )]
971
+
972
+ obj = func (sliced )
973
+ results .append ((e ,obj ))
974
+
975
+ return self ._construct_return_type (dict (results ))
976
+
943
977
def _reduce (self , op , axis = 0 , skipna = True , numeric_only = None ,
944
978
filter_type = None , ** kwds ):
945
979
axis_name = self ._get_axis_name (axis )
@@ -961,7 +995,7 @@ def _construct_return_type(self, result, axes=None, **kwargs):
961
995
# need to assume they are the same
962
996
if ndim is None :
963
997
if isinstance (result ,dict ):
964
- ndim = getattr (result . values ( )[0 ],'ndim' ,None )
998
+ ndim = getattr (list ( compat . itervalues ( result ) )[0 ],'ndim' ,None )
965
999
966
1000
# a saclar result
967
1001
if ndim is None :
0 commit comments