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 of to_pandas() when having integer column with null values #1232

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/snowflake/snowpark/_internal/server_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ def _fix_pandas_df_fixed_type(
# we try to strictly use astype("int64") in this scenario. If the values are too large to
# fit in int64, an OverflowError is thrown and we rely on to_numeric to choose and appropriate
# floating datatype to represent the number.
if column_metadata.precision > 10:
if column_metadata.precision > 10 and not pd_df[pandas_col_name].hasnans:
try:
pd_df[pandas_col_name] = pd_df[pandas_col_name].astype("int64")
except OverflowError:
Expand Down
16 changes: 16 additions & 0 deletions tests/integ/test_df_to_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ def check_fetch_data_exception(query: str) -> None:
assert df._plan.queries[2].sql.strip().startswith("SELECT")
isinstance(df.toPandas(), PandasDF)

def test_to_pandas_for_int_column_with_none_values(session):
# Assert that we try to fit into int64 when possible and keep precision
data = [
[0],
[1],
[None]
]
schema = ["A"]
df = session.create_dataframe(data, schema)

pdf = df.to_pandas()
assert pdf["A"][0] == 0
assert pdf["A"][1] == 1
assert pd.isna(pdf["A"][2])
assert pdf["A"].dtype == "float64"


@pytest.mark.skipif(
IS_IN_STORED_PROC, reason="SNOW-507565: Need localaws for large result"
Expand Down
Loading