Skip to content

Commit dbb3a4a

Browse files
author
Joe Jevnik
committed
BUG: add the 'name' attribute to dataframes that go through apply_frame_axis0
1 parent 4de5cdc commit dbb3a4a

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
@@ -5777,6 +5777,21 @@ def test_agg_over_numpy_arrays(self):
57775777

57785778
assert_frame_equal(result, expected)
57795779

5780+
def test_group_name_available_in_inference_pass(self):
5781+
df = pd.DataFrame({'a': [0, 0, 1, 1, 2, 2], 'b': np.arange(6)})
5782+
5783+
names = []
5784+
5785+
def f(group):
5786+
names.append(group.name)
5787+
return group.copy()
5788+
5789+
df.groupby('a', sort=False, group_keys=False).apply(f)
5790+
# we expect 2 zeros because we call ``f`` once to see if a faster route
5791+
# can be used.
5792+
expected_names = [0, 0, 1, 2]
5793+
tm.assert_equal(names, expected_names)
5794+
57805795

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

0 commit comments

Comments
 (0)