Skip to content

Commit 9acbe37

Browse files
committed
Wip
1 parent d7e8d44 commit 9acbe37

File tree

8 files changed

+19
-33
lines changed

8 files changed

+19
-33
lines changed

pandas/core/base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,10 @@ def ndim(self) -> int:
214214

215215
@cache_readonly
216216
def _obj_with_exclusions(self):
217-
# breakpoint()
218217
if self._selection is not None and isinstance(self.obj, ABCDataFrame):
219218
return self.obj.reindex(columns=self._selection_list)
220219

221-
if len(self.exclusions) > 0 and isinstance(self.obj, ABCDataFrame):
220+
if len(self.exclusions) > 0:
222221
return self.obj.drop(self.exclusions, axis=1)
223222
else:
224223
return self.obj

pandas/core/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -7732,6 +7732,7 @@ def count(self, axis=0, level=None, numeric_only=False):
77327732
Lewis 1
77337733
Myla 1
77347734
"""
7735+
breakpoint()
77357736
axis = self._get_axis_number(axis)
77367737
if level is not None:
77377738
return self._count_level(level, axis=axis, numeric_only=numeric_only)

pandas/core/groupby/groupby.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class providing the base-class of operations.
66
(defined in pandas.core.groupby.generic)
77
expose these user-facing objects to provide specific functionality.
88
"""
9+
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
910

1011
from contextlib import contextmanager
1112
import datetime
@@ -408,7 +409,6 @@ def __init__(
408409
observed=observed,
409410
mutated=self.mutated,
410411
)
411-
412412
self.obj = obj
413413
self.axis = obj._get_axis_number(axis)
414414
self.grouper = grouper
@@ -508,13 +508,17 @@ def _get_index(self, name):
508508
@cache_readonly
509509
def _selected_obj(self):
510510
# Note: _selected_obj is always just `self.obj` for SeriesGroupBy
511-
512-
if self._selection is None or isinstance(self.obj, Series):
511+
breakpoint()
512+
if self.as_index:
513+
obj = self._obj_with_exclusions
514+
else:
515+
obj = self.obj
516+
if self._selection is None or isinstance(obj, Series):
513517
if self._group_selection is not None:
514-
return self.obj[self._group_selection]
515-
return self.obj
518+
return obj[self._group_selection]
519+
return obj
516520
else:
517-
return self.obj[self._selection]
521+
return obj[self._selection]
518522

519523
def _reset_group_selection(self):
520524
"""
@@ -704,7 +708,7 @@ def __iter__(self):
704708
)
705709
)
706710
def apply(self, func, *args, **kwargs):
707-
# breakpoint()
711+
708712
func = self._is_builtin_func(func)
709713

710714
# this is needed so we don't try and wrap strings. If we could
@@ -732,7 +736,6 @@ def f(g):
732736
# ignore SettingWithCopy here in case the user mutates
733737
with option_context("mode.chained_assignment", None):
734738
try:
735-
# breakpoint()
736739
result = self._python_apply_general(f)
737740
except TypeError:
738741
# gh-20949
@@ -749,11 +752,7 @@ def f(g):
749752
return result
750753

751754
def _python_apply_general(self, f):
752-
breakpoint()
753-
if self.group_keys:
754-
keys, values, mutated = self.grouper.apply(f, self._obj_with_exclusions, self.axis)
755-
else:
756-
keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)
755+
keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)
757756

758757
return self._wrap_applied_output(
759758
keys, values, not_indexed_same=mutated or self.mutated
@@ -1581,7 +1580,7 @@ def rolling(self, *args, **kwargs):
15811580
Return a rolling grouper, providing rolling functionality per group.
15821581
"""
15831582
from pandas.core.window import RollingGroupby
1584-
kwargs['exclusions'] = self.exclusions
1583+
15851584
return RollingGroupby(self, *args, **kwargs)
15861585

15871586
@Substitution(name="groupby")

pandas/core/groupby/ops.py

-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ def _get_group_keys(self):
149149
return get_flattened_iterator(comp_ids, ngroups, self.levels, self.codes)
150150

151151
def apply(self, f, data: FrameOrSeries, axis: int = 0):
152-
# breakpoint()
153152
mutated = self.mutated
154153
splitter = self._get_splitter(data, axis=axis)
155154
group_keys = self._get_group_keys()
@@ -170,7 +169,6 @@ def apply(self, f, data: FrameOrSeries, axis: int = 0):
170169
and not sdata.index._has_complex_internals
171170
):
172171
try:
173-
# breakpoint()
174172
result_values, mutated = splitter.fast_apply(f, sdata, group_keys)
175173

176174
except libreduction.InvalidApply as err:
@@ -929,7 +927,6 @@ def _chop(self, sdata: Series, slice_obj: slice) -> Series:
929927
class FrameSplitter(DataSplitter):
930928
def fast_apply(self, f, sdata: FrameOrSeries, names):
931929
# must return keys::list, values::list, mutated::bool
932-
# breakpoint()
933930
starts, ends = lib.generate_slices(self.slabels, self.ngroups)
934931
return libreduction.apply_frame_axis0(sdata, f, names, starts, ends)
935932

pandas/core/window/common.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self, obj, *args, **kwargs):
5252
kwargs.pop("parent", None)
5353
groupby = kwargs.pop("groupby", None)
5454
if groupby is None:
55-
groupby, obj = obj, obj._obj_with_exclusions
55+
groupby, obj = obj, obj.obj
5656
self._groupby = groupby
5757
self._groupby.mutated = True
5858
self._groupby.grouper.mutated = True
@@ -82,13 +82,12 @@ def _apply(
8282
# TODO: can we de-duplicate with _dispatch?
8383
def f(x, name=name, *args):
8484
x = self._shallow_copy(x)
85-
# x.obj = x._obj_with_exclusions
8685

8786
if isinstance(name, str):
8887
return getattr(x, name)(*args, **kwargs)
8988

9089
return x.apply(name, *args, **kwargs)
91-
# breakpoint()
90+
9291
return self._groupby.apply(f)
9392

9493

pandas/core/window/rolling.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ def __init__(
9393
self.axis = obj._get_axis_number(axis) if axis is not None else None
9494
self.validate()
9595
self._numba_func_cache: Dict[Optional[str], Callable] = dict()
96-
self.exclusions = kwargs.get('exclusions', set())
9796

9897
@property
9998
def _constructor(self):
@@ -1174,7 +1173,6 @@ class _Rolling_and_Expanding(_Rolling):
11741173
)
11751174

11761175
def count(self):
1177-
11781176
blocks, obj = self._create_blocks()
11791177
results = []
11801178
for b in blocks:
@@ -1188,7 +1186,6 @@ def count(self):
11881186
closed=self.closed,
11891187
).sum()
11901188
results.append(result)
1191-
11921189
return self._wrap_results(results, blocks, obj)
11931190

11941191
_shared_docs["apply"] = dedent(
@@ -1954,7 +1951,6 @@ def aggregate(self, func, *args, **kwargs):
19541951
@Substitution(name="rolling")
19551952
@Appender(_shared_docs["count"])
19561953
def count(self):
1957-
19581954
# different impl for freq counting
19591955
if self.is_freq_type:
19601956
window_func = self._get_roll_func("roll_count")
@@ -1972,8 +1968,7 @@ def apply(
19721968
engine_kwargs=None,
19731969
args=None,
19741970
kwargs=None,
1975-
):
1976-
# breakpoint()
1971+
):
19771972
return super().apply(
19781973
func,
19791974
raw=raw,

pandas/tests/groupby/test_apply.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ def test_apply_chunk_view():
370370

371371
result = df.groupby("key", group_keys=False).apply(lambda x: x[:2])
372372
expected = df.take([0, 1, 3, 4, 6, 7])
373-
breakpoint()
374373
tm.assert_frame_equal(result, expected)
375374

376375

@@ -406,7 +405,6 @@ def f(group):
406405

407406
expected = df.copy()
408407
expected["v2"] = np.tile([0.0, 0.5, 1], 2)
409-
breakpoint()
410408
tm.assert_frame_equal(result, expected)
411409

412410

@@ -828,6 +826,7 @@ def test_apply_index_has_complex_internals(index):
828826
# GH 31248
829827
df = DataFrame({"group": [1, 1, 2], "value": [0, 1, 0]}, index=index)
830828
result = df.groupby("group").apply(lambda x: x)
829+
breakpoint()
831830
tm.assert_frame_equal(result, df)
832831

833832

pandas/tests/window/test_grouper.py

-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def test_getitem(self):
3030
expected = g_mutated.B.apply(lambda x: x.rolling(2).mean())
3131

3232
result = g.rolling(2).mean().B
33-
# breakpoint()
3433
tm.assert_series_equal(result, expected)
3534

3635
result = g.rolling(2).B.mean()
@@ -62,9 +61,7 @@ def test_rolling(self):
6261

6362
for f in ["sum", "mean", "min", "max", "count", "kurt", "skew"]:
6463
result = getattr(r, f)()
65-
# breakpoint()
6664
expected = g.apply(lambda x: getattr(x.rolling(4), f)())
67-
# breakpoint()
6865
tm.assert_frame_equal(result, expected)
6966

7067
for f in ["std", "var"]:

0 commit comments

Comments
 (0)