-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
TST: Add test for Arrow decimal groupby variance #62754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nour-taqatqa
wants to merge
6
commits into
pandas-dev:main
Choose a base branch
from
nour-taqatqa:my_personal_branch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8463664
Add test for Arrow decimal groupby variance
nour-taqatqa d2d716a
edited comment for test\
nour-taqatqa 2781b3e
reverted groupby.py to upstream/main
nour-taqatqa b2baf44
Fix test for Arrow-backed variance and update expected dtype
nour-taqatqa 2fce768
Delete old test from test_cython.py
nour-taqatqa 537a152
Revert test_cython.py to upstream/main
nour-taqatqa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,10 @@ | |
| Timestamp, | ||
| bdate_range, | ||
| ) | ||
| import pyarrow as pa | ||
| import decimal | ||
| import pandas._testing as tm | ||
| import math | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
|
|
@@ -413,3 +416,61 @@ def test_cython_agg_EA_known_dtypes(data, op_name, action, with_na): | |
|
|
||
| result = grouped["col"].aggregate(op_name) | ||
| assert result.dtype == expected_dtype | ||
|
|
||
| #testing groupby.var() when called with pyarrow datatype | ||
|
|
||
| @pytest.mark.parametrize("with_na", [False, True]) | ||
| def test_groupby_var_arrow_decimal(with_na): | ||
|
||
| # Create Arrow-backed decimal Series | ||
| data = pd.Series( | ||
| [ | ||
| decimal.Decimal("123.000"), | ||
| decimal.Decimal("12.000"), | ||
| decimal.Decimal("5.5"), | ||
| decimal.Decimal("7.25") | ||
| ], | ||
| dtype=pd.ArrowDtype(pa.decimal128(6, 3)) | ||
| ) | ||
|
|
||
| if with_na: | ||
| data.iloc[3] = pd.NA # introduce a missing value | ||
|
|
||
| df = DataFrame({"key": ["a", "a", "b", "b"], "col": data}) | ||
| grouped = df.groupby("key") | ||
|
|
||
| # Perform the aggregation using .var() (calls _cython_agg_general internally) | ||
| result = grouped.var()#it correctly converts it to double[pyarrow] | ||
|
|
||
|
|
||
| # Check dtype is still Arrow double | ||
| expected_dtype = pd.ArrowDtype(pa.float64()) | ||
| assert isinstance(result["col"].dtype, pd.ArrowDtype) | ||
| assert result["col"].dtype == expected_dtype | ||
|
|
||
|
|
||
| # Compute expected variance manually for group "a" | ||
| vals_a = [123.0, 12.0] # convert to float | ||
| if with_na: | ||
| vals_b = [5.5] # single value → var is NA | ||
| else: | ||
| vals_b = [5.5, 7.25] | ||
|
|
||
| # Compute variance using pandas (float) | ||
| expected_var_a = pd.Series(vals_a).var() | ||
| expected_var_b = pd.Series(vals_b).var() if len(vals_b) > 1 else pd.NA | ||
|
|
||
| # Helper function for float comparison with NA support | ||
| def _almost_equal_or_na(a, b, tol=1e-12): | ||
| if pd.isna(a) and pd.isna(b): | ||
| return True | ||
| return math.isclose(float(a), float(b), rel_tol=tol, abs_tol=tol) | ||
|
|
||
| # Compare the DataFrame result | ||
| assert _almost_equal_or_na(result.loc["a", "col"], expected_var_a) | ||
| assert _almost_equal_or_na(result.loc["b", "col"], expected_var_b) | ||
|
|
||
| # Also test the SeriesGroupBy path | ||
| result_series = grouped["col"].var() | ||
| assert _almost_equal_or_na(result_series.loc["a"], expected_var_a) | ||
| assert _almost_equal_or_na(result_series.loc["b"], expected_var_b) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you undo the changes in this file?