@@ -302,18 +302,18 @@ def __getitem__(self, key):
302
302
303
303
def _make_wrapper (self , name ):
304
304
if name not in self ._apply_whitelist :
305
- is_callable = callable (getattr (self .obj , name , None ))
305
+ is_callable = callable (getattr (self ._selected_obj , name , None ))
306
306
kind = ' callable ' if is_callable else ' '
307
307
msg = ("Cannot access{0}attribute {1!r} of {2!r} objects, try "
308
308
"using the 'apply' method" .format (kind , name ,
309
309
type (self ).__name__ ))
310
310
raise AttributeError (msg )
311
311
312
- f = getattr (self .obj , name )
312
+ f = getattr (self ._selected_obj , name )
313
313
if not isinstance (f , types .MethodType ):
314
314
return self .apply (lambda self : getattr (self , name ))
315
315
316
- f = getattr (type (self .obj ), name )
316
+ f = getattr (type (self ._selected_obj ), name )
317
317
318
318
def wrapper (* args , ** kwargs ):
319
319
# a little trickery for aggregation functions that need an axis
@@ -362,7 +362,7 @@ def get_group(self, name, obj=None):
362
362
group : type of obj
363
363
"""
364
364
if obj is None :
365
- obj = self .obj
365
+ obj = self ._selected_obj
366
366
367
367
inds = self ._get_index (name )
368
368
return obj .take (inds , axis = self .axis , convert = False )
@@ -424,7 +424,7 @@ def f(g):
424
424
return self ._python_apply_general (f )
425
425
426
426
def _python_apply_general (self , f ):
427
- keys , values , mutated = self .grouper .apply (f , self .obj , self .axis )
427
+ keys , values , mutated = self .grouper .apply (f , self ._selected_obj , self .axis )
428
428
429
429
return self ._wrap_applied_output (keys , values ,
430
430
not_indexed_same = mutated )
@@ -437,7 +437,7 @@ def agg(self, func, *args, **kwargs):
437
437
return self .aggregate (func , * args , ** kwargs )
438
438
439
439
def _iterate_slices (self ):
440
- yield self .name , self .obj
440
+ yield self .name , self ._selected_obj
441
441
442
442
def transform (self , func , * args , ** kwargs ):
443
443
raise NotImplementedError
@@ -573,7 +573,7 @@ def nth(self, n, dropna=None):
573
573
return self ._selected_obj [is_nth ]
574
574
575
575
if (isinstance (self ._selected_obj , DataFrame )
576
- and dropna not in ['any' , 'all' ]):
576
+ and dropna not in ['any' , 'all' ]):
577
577
# Note: when agg-ing picker doesn't raise this, just returns NaN
578
578
raise ValueError ("For a DataFrame groupby, dropna must be "
579
579
"either None, 'any' or 'all', "
@@ -582,6 +582,7 @@ def nth(self, n, dropna=None):
582
582
# old behaviour, but with all and any support for DataFrames.
583
583
584
584
max_len = n if n >= 0 else - 1 - n
585
+
585
586
def picker (x ):
586
587
x = x .dropna (how = dropna ) # Note: how is ignored if Series
587
588
if len (x ) <= max_len :
@@ -591,7 +592,6 @@ def picker(x):
591
592
592
593
return self .agg (picker )
593
594
594
-
595
595
def cumcount (self , ** kwargs ):
596
596
"""
597
597
Number each item in each group from 0 to the length of that group - 1.
@@ -638,7 +638,7 @@ def cumcount(self, **kwargs):
638
638
"""
639
639
ascending = kwargs .pop ('ascending' , True )
640
640
641
- index = self .obj .index
641
+ index = self ._selected_obj .index
642
642
cumcounts = self ._cumcount_array (ascending = ascending )
643
643
return Series (cumcounts , index )
644
644
@@ -706,8 +706,9 @@ def _cumcount_array(self, arr=None, **kwargs):
706
706
if arr is None :
707
707
arr = np .arange (self .grouper ._max_groupsize , dtype = 'int64' )
708
708
709
- len_index = len (self .obj .index )
709
+ len_index = len (self ._selected_obj .index )
710
710
cumcounts = np .empty (len_index , dtype = arr .dtype )
711
+
711
712
if ascending :
712
713
for v in self .indices .values ():
713
714
cumcounts [v ] = arr [:len (v )]
@@ -722,15 +723,15 @@ def _selected_obj(self):
722
723
return self .obj
723
724
else :
724
725
return self .obj [self ._selection ]
725
-
726
+
726
727
def _index_with_as_index (self , b ):
727
728
"""
728
729
Take boolean mask of index to be returned from apply, if as_index=True
729
730
730
731
"""
731
732
# TODO perf, it feels like this should already be somewhere...
732
733
from itertools import chain
733
- original = self .obj .index
734
+ original = self ._selected_obj .index
734
735
gp = self .grouper
735
736
levels = chain ((gp .levels [i ][gp .labels [i ][b ]]
736
737
for i in range (len (gp .groupings ))),
@@ -812,7 +813,7 @@ def _concat_objects(self, keys, values, not_indexed_same=False):
812
813
813
814
if not not_indexed_same :
814
815
result = concat (values , axis = self .axis )
815
- ax = self .obj ._get_axis (self .axis )
816
+ ax = self ._selected_obj ._get_axis (self .axis )
816
817
817
818
if isinstance (result , Series ):
818
819
result = result .reindex (ax )
@@ -835,14 +836,14 @@ def _apply_filter(self, indices, dropna):
835
836
else :
836
837
indices = np .sort (np .concatenate (indices ))
837
838
if dropna :
838
- filtered = self .obj .take (indices )
839
+ filtered = self ._selected_obj .take (indices )
839
840
else :
840
- mask = np .empty (len (self .obj .index ), dtype = bool )
841
+ mask = np .empty (len (self ._selected_obj .index ), dtype = bool )
841
842
mask .fill (False )
842
843
mask [indices .astype (int )] = True
843
844
# mask fails to broadcast when passed to where; broadcast manually.
844
- mask = np .tile (mask , list (self .obj .shape [1 :]) + [1 ]).T
845
- filtered = self .obj .where (mask ) # Fill with NaNs.
845
+ mask = np .tile (mask , list (self ._selected_obj .shape [1 :]) + [1 ]).T
846
+ filtered = self ._selected_obj .where (mask ) # Fill with NaNs.
846
847
return filtered
847
848
848
849
@@ -1908,7 +1909,7 @@ def transform(self, func, *args, **kwargs):
1908
1909
-------
1909
1910
transformed : Series
1910
1911
"""
1911
- result = self .obj .copy ()
1912
+ result = self ._selected_obj .copy ()
1912
1913
if hasattr (result , 'values' ):
1913
1914
result = result .values
1914
1915
dtype = result .dtype
@@ -1933,8 +1934,8 @@ def transform(self, func, *args, **kwargs):
1933
1934
1934
1935
# downcast if we can (and need)
1935
1936
result = _possibly_downcast_to_dtype (result , dtype )
1936
- return self .obj .__class__ (result , index = self .obj .index ,
1937
- name = self .obj .name )
1937
+ return self ._selected_obj .__class__ (result , index = self ._selected_obj .index ,
1938
+ name = self ._selected_obj .name )
1938
1939
1939
1940
def filter (self , func , dropna = True , * args , ** kwargs ):
1940
1941
"""
@@ -2082,7 +2083,7 @@ def aggregate(self, arg, *args, **kwargs):
2082
2083
if self .axis != 0 : # pragma: no cover
2083
2084
raise ValueError ('Can only pass dict with axis=0' )
2084
2085
2085
- obj = self .obj
2086
+ obj = self ._selected_obj
2086
2087
2087
2088
if any (isinstance (x , (list , tuple , dict )) for x in arg .values ()):
2088
2089
new_arg = OrderedDict ()
@@ -2095,7 +2096,7 @@ def aggregate(self, arg, *args, **kwargs):
2095
2096
2096
2097
keys = []
2097
2098
if self ._selection is not None :
2098
- subset = obj [ self . _selection ]
2099
+ subset = obj
2099
2100
if isinstance (subset , DataFrame ):
2100
2101
raise NotImplementedError
2101
2102
@@ -2294,7 +2295,7 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
2294
2295
2295
2296
if isinstance (v , (np .ndarray , Series )):
2296
2297
if isinstance (v , Series ):
2297
- applied_index = self .obj ._get_axis (self .axis )
2298
+ applied_index = self ._selected_obj ._get_axis (self .axis )
2298
2299
all_indexed_same = _all_indexes_same ([
2299
2300
x .index for x in values
2300
2301
])
@@ -2367,7 +2368,11 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
2367
2368
2368
2369
# if we have date/time like in the original, then coerce dates
2369
2370
# as we are stacking can easily have object dtypes here
2370
- cd = 'coerce' if self .obj .ndim == 2 and self .obj .dtypes .isin (_DATELIKE_DTYPES ).any () else True
2371
+ if (self ._selected_obj .ndim == 2
2372
+ and self ._selected_obj .dtypes .isin (_DATELIKE_DTYPES ).any ()):
2373
+ cd = 'coerce'
2374
+ else :
2375
+ cd = True
2371
2376
return result .convert_objects (convert_dates = cd )
2372
2377
2373
2378
else :
@@ -2668,8 +2673,8 @@ def _wrap_agged_blocks(self, blocks):
2668
2673
return result .convert_objects ()
2669
2674
2670
2675
def _iterate_column_groupbys (self ):
2671
- for i , colname in enumerate (self .obj .columns ):
2672
- yield colname , SeriesGroupBy (self .obj .iloc [:, i ],
2676
+ for i , colname in enumerate (self ._selected_obj .columns ):
2677
+ yield colname , SeriesGroupBy (self ._selected_obj .iloc [:, i ],
2673
2678
selection = colname ,
2674
2679
grouper = self .grouper ,
2675
2680
exclusions = self .exclusions )
@@ -2679,7 +2684,7 @@ def _apply_to_column_groupbys(self, func):
2679
2684
return concat (
2680
2685
(func (col_groupby ) for _ , col_groupby
2681
2686
in self ._iterate_column_groupbys ()),
2682
- keys = self .obj .columns , axis = 1 )
2687
+ keys = self ._selected_obj .columns , axis = 1 )
2683
2688
2684
2689
def ohlc (self ):
2685
2690
"""
@@ -2701,10 +2706,10 @@ def _iterate_slices(self):
2701
2706
if self .axis == 0 :
2702
2707
# kludge
2703
2708
if self ._selection is None :
2704
- slice_axis = self .obj .items
2709
+ slice_axis = self ._selected_obj .items
2705
2710
else :
2706
2711
slice_axis = self ._selection_list
2707
- slicer = lambda x : self .obj [x ]
2712
+ slicer = lambda x : self ._selected_obj [x ]
2708
2713
else :
2709
2714
raise NotImplementedError
2710
2715
0 commit comments