Skip to content

Commit 710528a

Browse files
author
Joe Jevnik
committed
BUG: add the 'name' attribute to dataframes that go through apply_frame_axis0
1 parent be3f2ae commit 710528a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pandas/src/reduce.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def apply_frame_axis0(object frame, object f, object names,
497497
# Need to infer if our low-level mucking is going to cause a segfault
498498
if n > 0:
499499
chunk = frame.iloc[starts[0]:ends[0]]
500-
shape_before = chunk.shape
500+
object.__setattr__(chunk, 'name', names[0])
501501
try:
502502
result = f(chunk)
503503
if result is chunk:

pandas/tests/groupby/test_groupby.py

+15
Original file line numberDiff line numberDiff line change
@@ -6022,6 +6022,21 @@ def test_cummin_cummax(self):
60226022
result = base_df.groupby('A').B.apply(lambda x: x.cummax()).to_frame()
60236023
tm.assert_frame_equal(expected, result)
60246024

6025+
def test_group_name_available_in_inference_pass(self):
6026+
df = pd.DataFrame({'a': [0, 0, 1, 1, 2, 2], 'b': np.arange(6)})
6027+
6028+
names = []
6029+
6030+
def f(group):
6031+
names.append(group.name)
6032+
return group.copy()
6033+
6034+
df.groupby('a', sort=False, group_keys=False).apply(f)
6035+
# we expect 2 zeros because we call ``f`` once to see if a faster route
6036+
# can be used.
6037+
expected_names = [0, 0, 1, 2]
6038+
tm.assert_equal(names, expected_names)
6039+
60256040

60266041
def assert_fp_equal(a, b):
60276042
assert (np.abs(a - b) < 1e-12).all()

0 commit comments

Comments
 (0)