Skip to content

Commit 5af3867

Browse files
committed
TST: ensure groupby get by index value pandas-dev#33439
1 parent dec736f commit 5af3867

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ Other
750750
- Bug in :meth:`DataFrame.__dir__` caused a segfault when using unicode surrogates in a column name (:issue:`25509`)
751751
- Bug in :meth:`DataFrame.plot.scatter` caused an error when plotting variable marker sizes (:issue:`32904`)
752752
- :class:`IntegerArray` now implements the ``sum`` operation (:issue:`33172`)
753+
- Added test for getting values by index value during groupby aggregation (:issue:`33439`)
753754

754755
.. ---------------------------------------------------------------------------
755756

pandas/tests/groupby/test_groupby.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ def test_pass_args_kwargs(ts, tsframe):
199199
def f(x, q=None, axis=0):
200200
return np.percentile(x, q, axis=axis)
201201

202-
g = lambda x: np.percentile(x, 80, axis=0)
202+
def g(x):
203+
return np.percentile(x, 80, axis=0)
203204

204205
# Series
205206
ts_grouped = ts.groupby(lambda x: x.month)
@@ -422,7 +423,9 @@ def test_frame_groupby_columns(tsframe):
422423
assert len(aggregated.columns) == 2
423424

424425
# transform
425-
tf = lambda x: x - x.mean()
426+
def tf(x):
427+
return x - x.mean()
428+
426429
groupedT = tsframe.T.groupby(mapping, axis=0)
427430
tm.assert_frame_equal(groupedT.transform(tf).T, grouped.transform(tf))
428431

@@ -1020,7 +1023,9 @@ def test_seriesgroupby_name_attr(df):
10201023
assert result.count().name == "C"
10211024
assert result.mean().name == "C"
10221025

1023-
testFunc = lambda x: np.sum(x) * 2
1026+
def testFunc(x):
1027+
return np.sum(x) * 2
1028+
10241029
assert result.agg(testFunc).name == "C"
10251030

10261031

@@ -1102,7 +1107,9 @@ def test_series_grouper_noncontig_index():
11021107
grouped = values.groupby(labels)
11031108

11041109
# accessing the index elements causes segfault
1105-
f = lambda x: len(set(map(id, x.index)))
1110+
def f(x):
1111+
return len(set(map(id, x.index)))
1112+
11061113
grouped.agg(f)
11071114

11081115

@@ -2012,3 +2019,16 @@ def test_groups_repr_truncates(max_seq_items, expected):
20122019

20132020
result = df.groupby(np.array(df.a)).groups.__repr__()
20142021
assert result == expected
2022+
2023+
2024+
def test_groupby_get_by_index():
2025+
# GH 33439
2026+
df = pd.DataFrame({
2027+
"A": ["S", "W", "W"],
2028+
"B": [1.0, 1.0, 2.0],
2029+
})
2030+
res = df.groupby("A").agg(
2031+
{"B": lambda x: x.get(x.index[-1])}
2032+
)
2033+
expected = pd.DataFrame(dict(A=["S", "W"], B=[1.0, 2.0])).set_index("A")
2034+
pd.testing.assert_frame_equal(res, expected)

0 commit comments

Comments
 (0)