Skip to content

Commit 143c26d

Browse files
committed
TST: Add test for groupby.var() arrow dtype retention
1 parent 939cca6 commit 143c26d

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

pandas/tests/groupby/aggregate/test_cython.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,41 @@ def test_cython_agg_EA_known_dtypes(data, op_name, action, with_na):
413413

414414
result = grouped["col"].aggregate(op_name)
415415
assert result.dtype == expected_dtype
416+
417+
@pytest.mark.parametrize(
418+
"op_name",
419+
[
420+
"var",
421+
"std",
422+
"sem",
423+
"mean",
424+
],
425+
)
426+
@pytest.mark.parametrize(
427+
"dtype",
428+
[
429+
"int64[pyarrow]",
430+
"float64[pyarrow]",
431+
],
432+
)
433+
def test_cython_agg_pyarrow_dtype_retention(op_name, dtype):
434+
# GH#54627 - groupby.var() should return arrow types with arrow backed input
435+
pa = pytest.importorskip("pyarrow")
436+
437+
arr = pd.array([1, 2, 3, 4], dtype=dtype)
438+
df = DataFrame({"key": ["a", "a", "b", "b"], "col": arr})
439+
grouped = df.groupby("key")
440+
441+
result = getattr(grouped, op_name)()
442+
# var/std/sem/mean of numeric pyarrow types should return double[pyarrow]
443+
assert result["col"].dtype == pd.ArrowDtype(pa.float64())
444+
445+
result = grouped.aggregate(op_name)
446+
assert result["col"].dtype == pd.ArrowDtype(pa.float64())
447+
448+
result = getattr(grouped["col"], op_name)()
449+
assert result.dtype == pd.ArrowDtype(pa.float64())
450+
451+
result = grouped["col"].aggregate(op_name)
452+
assert result.dtype == pd.ArrowDtype(pa.float64())
453+

0 commit comments

Comments
 (0)