Skip to content

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