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: Workaround for Pandas.DataFrame.to_csv bug #28755

Merged
merged 1 commit into from
Jun 13, 2024
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
2 changes: 1 addition & 1 deletion superset/utils/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def escape_values(v: Any) -> Union[str, Any]:
if isinstance(value, str):
df.at[idx, name] = escape_value(value)

return df.to_csv(**kwargs)
return df.to_csv(escapechar="\\", **kwargs)


def get_chart_csv_data(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import io

import pandas as pd
import pyarrow as pa
Expand Down Expand Up @@ -59,24 +58,39 @@ def test_escape_value():


def test_df_to_escaped_csv():
csv_rows = [
["col_a", "=func()"],
["-10", "=cmd|' /C calc'!A0"],
["a", '""=b'],
[" =a", "b"],
]
csv_str = "\n".join([",".join(row) for row in csv_rows])

df = pd.read_csv(io.StringIO(csv_str))
df = pd.DataFrame(
Copy link
Member Author

Choose a reason for hiding this comment

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

Note these tests are unit as opposed to integration tests—no database dependency—and thus I opted to move the file.

data={
"value": [
"a",
"col_a",
"=func()",
"-10",
"=cmd|' /C calc'!A0",
'""=b',
" =a",
"\x00",
]
}
)

escaped_csv_str = csv.df_to_escaped_csv(
df,
encoding="utf8",
index=False,
header=False,
)

escaped_csv_str = csv.df_to_escaped_csv(df, encoding="utf8", index=False)
escaped_csv_rows = [row.split(",") for row in escaped_csv_str.strip().split("\n")]

assert escaped_csv_rows == [
["col_a", "'=func()"],
["-10", r"'=cmd\|' /C calc'!A0"],
["a", "'=b"], # pandas seems to be removing the leading ""
["' =a", "b"],
["a"],
["col_a"],
["'=func()"],
["-10"],
[r"'=cmd\\|' /C calc'!A0"],
['"\'""""=b"'],
["' =a"],
["\x00"],
]

df = pa.array([1, None]).to_pandas(integer_object_nulls=True).to_frame()
Expand Down
Loading