Skip to content
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

fix: handle None when converting numerics to parquet #768

Merged
merged 7 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pandas_gbq/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ def cast_dataframe_for_parquet(
errors="ignore",
)
elif column_type in {"NUMERIC", "DECIMAL", "BIGNUMERIC", "BIGDECIMAL"}:
cast_column = dataframe[column_name].map(decimal.Decimal)
# decimal.Decimal does not support `None` input, add support here.
# https://github.com/googleapis/python-bigquery-pandas/issues/719
def convert(x):
if x is None:
Copy link
Collaborator

@chalmerlowe chalmerlowe May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QUESTION:

The original issue mentions that None and pd.NA are not handled appropriately.

This code seems to only address None.
Is the intent to check for both?

IF yes... this may be a possible check that is more general AND as other edge cases arise could enable edits to the code quickly and easily.

Suggested change
if x is None:
if x in {None, pd.NA}:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, thank you for catching this! I'll update the code.

return decimal.Decimal("NaN")
else:
return decimal.Decimal(x)

cast_column = dataframe[column_name].map(convert)
else:
cast_column = None

Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,24 @@ def test_cast_dataframe_for_parquet_w_null_fields():
schema = {"fields": None}
result = load.cast_dataframe_for_parquet(dataframe, schema)
pandas.testing.assert_frame_equal(result, expected)


# Verifies null numerics are properly handled
# https://github.com/googleapis/python-bigquery-pandas/issues/719
def test_cast_dataframe_for_parquet_w_null_numerics():
from decimal import Decimal

dataframe = pandas.DataFrame(
{
"A": pandas.Series([Decimal("5413.36"), Decimal("nan"), None]),
},
)

schema = {"fields": [{"name": "A", "type": "BIGNUMERIC"}]}
result = load.cast_dataframe_for_parquet(dataframe, schema)

# pandas.testing.assert_frame_equal() doesn't distinguish Decimal('nan')
# vs. None, verify Decimal("nan") directly.
# https://github.com/pandas-dev/pandas/issues/18463
assert result["A"][1].is_nan()
chalmerlowe marked this conversation as resolved.
Show resolved Hide resolved
assert result["A"][2].is_nan()