Skip to content

Commit 1648809

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

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
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

+17-4
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ 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): return np.percentile(x, 80, axis=0)
203203

204204
# Series
205205
ts_grouped = ts.groupby(lambda x: x.month)
@@ -422,7 +422,7 @@ def test_frame_groupby_columns(tsframe):
422422
assert len(aggregated.columns) == 2
423423

424424
# transform
425-
tf = lambda x: x - x.mean()
425+
def tf(x): return x - x.mean()
426426
groupedT = tsframe.T.groupby(mapping, axis=0)
427427
tm.assert_frame_equal(groupedT.transform(tf).T, grouped.transform(tf))
428428

@@ -1020,7 +1020,7 @@ def test_seriesgroupby_name_attr(df):
10201020
assert result.count().name == "C"
10211021
assert result.mean().name == "C"
10221022

1023-
testFunc = lambda x: np.sum(x) * 2
1023+
def testFunc(x): return np.sum(x) * 2
10241024
assert result.agg(testFunc).name == "C"
10251025

10261026

@@ -1102,7 +1102,7 @@ def test_series_grouper_noncontig_index():
11021102
grouped = values.groupby(labels)
11031103

11041104
# accessing the index elements causes segfault
1105-
f = lambda x: len(set(map(id, x.index)))
1105+
def f(x): return len(set(map(id, x.index)))
11061106
grouped.agg(f)
11071107

11081108

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

20132013
result = df.groupby(np.array(df.a)).groups.__repr__()
20142014
assert result == expected
2015+
2016+
2017+
def test_groupby_get_by_index():
2018+
# GH 33439
2019+
df = pd.DataFrame({
2020+
"A": ["S", "W", "W"],
2021+
"B": [1.0, 1.0, 2.0],
2022+
})
2023+
res = df.groupby("A").agg(
2024+
{"B": lambda x: x.get(x.index[-1])}
2025+
)
2026+
expected = pd.DataFrame(dict(A=["S", "W"], B=[1.0, 2.0])).set_index("A")
2027+
pd.testing.assert_frame_equal(res, expected)

0 commit comments

Comments
 (0)