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

add capability to pass options to to_csv method #103

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iainsgillis
Copy link

On Python 3.10 and 3.11, an upstream bug in pandas causes a failure when serializing a dataframe to csv when there's a null byte in the dataframe.

This pull request leaves the default behaviour alone, but gives users the options to modify pd.DataFrame.to_csv behaviour, including fixing that issue with the escapechar parameter.

See pandas-dev/pandas#47871

If anyone encounters this issue, an alternative is to monkeypatch this socrata-py library. Example follows:

import sys
if sys.version_info >= (3, 10):
    from socrata.sources import Source

    def patched_df(self, dataframe, **kwargs):
        print("using monkeypatched function: see https://github.com/pandas-dev/pandas/issues/47871")
        s = io.StringIO()
        pd_to_csv_params = kwargs.pop("pd_to_csv_params", {"escapechar": "\\"})
        dataframe.to_csv(s, index=False, **pd_to_csv_params)
        return self._chunked_bytes(bytes(s.getvalue().encode()), "text/csv", **kwargs)

    Source.df = patched_df

On Python 3.10 and 3.11, an upstream bug in pandas causes a failure when serializing a dataframe to csv when there's a null byte in the dataframe. This pull request leaves the default behaviour alone, but gives users the options to modify to_csv behaviour, including fixing that issue with the `escapechar` parameter.

See pandas-dev/pandas#47871
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant