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(csv): Ensure df_to_escaped_csv does not coerce integer columns to float #20151

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions superset/utils/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from typing import Any, Dict, Optional
from urllib.error import URLError

import numpy as np
import pandas as pd
import simplejson

Expand Down Expand Up @@ -64,8 +65,12 @@ def df_to_escaped_csv(df: pd.DataFrame, **kwargs: Any) -> Any:
# Escape csv headers
df = df.rename(columns=escape_values)

# Escape csv rows
df = df.applymap(escape_values)
# Escape csv values
for name, column in df.items():
if column.dtype == np.dtype(object):
for idx, value in enumerate(column.values):
if isinstance(value, str):
df.at[idx, name] = escape_value(value)

return df.to_csv(**kwargs)

Expand Down
4 changes: 4 additions & 0 deletions tests/integration_tests/utils/csv_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io

import pandas as pd
import pyarrow as pa
import pytest

from superset.utils import csv
Expand Down Expand Up @@ -77,3 +78,6 @@ def test_df_to_escaped_csv():
["a", "'=b"], # pandas seems to be removing the leading ""
["' =a", "b"],
]

df = pa.array([1, None]).to_pandas(integer_object_nulls=True).to_frame()
assert csv.df_to_escaped_csv(df, encoding="utf8", index=False) == '0\n1\n""\n'