Skip to content

BUG: pd.DataFrame.to_csv with escaping produces different output in python 3.10 and 3.11 than 3.8 and 3.9 #51123

Open
@LeeMendelowitz

Description

@LeeMendelowitz

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import sys

v = sys.version_info
py_version = f'{v.major}-{v.minor}-{v.micro}'
file_name = f'test.{py_version}.{pd.__version__}.csv'

weird_strings = ['"hi"', 'you,\nthere', 'hi\\there', 'hi\there', 'weird\nstuff\rgoing\\']
df = pd.DataFrame({'d' : weird_strings})
df.to_csv(file_name, index = False, header = False, na_rep = 'NULL', escapechar = "\\", doublequote = False)

Issue Description

The above code produces different CSV files using pandas 1.5.3 in python 3.10 and python 3.11 than it does in python 3.8 and python 3.9.

Could this be related to #47871, which also describes different pandas.DataFrame.to_csv behavior around escaping between python versions?

Expected Behavior

I would expect the CSV files to be identical regardless of the version of Python.

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.10.9.final.0
python-bits : 64
OS : Darwin
OS-release : 21.6.0
Version : Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.3
numpy : 1.24.1
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 65.6.3
pip : 22.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions