BUG: df.to_csv() fails when encountering a NULL character

This issue has been created since 2022-09-23.

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
pd.DataFrame(['\x00-1']).to_csv(index=False)

...
File "pandas/_libs/writers.pyx", line 75, in pandas._libs.writers.write_csv_rows
_csv.Error: need to escape, but no escapechar set

Issue Description

Attempting df.to_csv() with any data that contains the NULL character (i.e. '\x00') throws the "need to escape, but no escapechar set" even tho quoting is enabled. Providing any escapechar works around the issue, even tho the escapechar is not actually used.

Expected Behavior

CSV conversion should happen without error -- quoting the special character if needed.

Installed Versions

INSTALLED VERSIONS

commit : ca60aab
python : 3.10.6.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 : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.4
numpy : 1.23.2
pytz : 2018.5
dateutil : 2.8.1
setuptools : 65.3.0
pip : 22.2.2
Cython : 0.29.32
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : 1.1
pymysql : None
psycopg2 : 2.9.3
jinja2 : 3.1.2
IPython : 7.31.1
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.1.1
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 2.5.8
pandas_gbq : None
pyarrow : 6.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.0
snappy : None
sqlalchemy : None
tables : None
tabulate : 0.8.10
xarray : None
xlrd : 1.1.0
xlwt : 1.3.0
zstandard : None

eshmu wrote this answer on 2022-09-23

Appears to be related to a change in Python 3.10: python/cpython#56387 (comment)

More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-09-29
Star Count 35374
Watcher Count 1122
Fork Count 15034
Issue Count 3579

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
[TASK] Add Ensemble ROM Capability 0 2021-11-24 2022-07-14
How to modify singleagent.py with a pid controller to move in xyz space? 6 2020-12-05 2022-08-20
Compute Optimizer Recommendations return highest savings low risk recommendation 0 2022-01-31 2022-09-23
AttributeError: module 'textual.events' has no attribute 'Startup' 2 2021-08-07 2022-08-31
[Feature Request] Fullscreen contained in a frame 1 2022-04-26 2022-09-18
Custom Fonts are overwritten. 1 2021-03-03 2022-09-22
Slider - Have a nicer style for slider when tick marks are showing 0 2021-04-16 2022-09-29
ComboBox - incoherent selection color in Dark Style 0 2021-04-16 2022-08-16
Icon doesn't change if enabled 9 2022-06-26 2022-09-21
[README] Update the use of options under settings 0 2022-06-26 2022-09-21
$viewer.update()方法第二次才生效,请问是什么原因呢? 2 2022-07-14 2022-09-21
Information about optional security codes (CVV/CVC etc.) 2 2021-03-15 2022-09-02
Implement HTMLDetailsElement setter 2 2021-04-07 2022-08-17
Intraday schedules not rendered on Safari 2 2021-05-21 2022-09-11
About multi-speaker datasets and tacotron2... 9 2021-08-11 2022-07-21
Clip without triangulate/crinkle 5 2022-03-09 2022-09-12
Strikethrough completed appointments 2 2022-03-07 2022-07-30
Use Instagram login with AppAuth 1 2021-07-18 2022-09-29
[Build] Disable push to GitHub during `release-it` 0 2019-03-22 2022-09-11
Bug: TextInput clear button is not working 0 2019-03-15 2022-08-20
`create` does not work for UncheckedArray, as sizeof(UncheckedArray[T])==0 4 2021-10-15 2022-07-28
[core] A NullPointerException occurs in decompiling multi-entry loops 3 2022-08-10 2022-09-13
TTF_FontHeight() doesn't report the correct height in bold-font and style underline 0 2021-02-11 2022-09-25
Memory issues in kubeappsapis/helm-related logic: `getChart` fetches the whole repo each time it's invoked 3 2021-10-08 2022-08-20
[pod_install] removes flutter module paths 1 2022-05-23 2022-09-12
[Bug] ray dashboard ip:8265 out of memory 6 2022-02-24 2022-08-03
st.session_state bug? 1 2021-12-02 2022-09-27
Implement indexQuery function ? 2 2020-04-24 2022-08-08
Add select previous 1 2016-10-24 2022-01-20
Help Center Release Chores: V1 0 2022-09-20 2022-09-21
Webbased frontend behind apache reverse proxy and subsite a la mysite.de/zigbee2mqtt 1 2022-07-05 2022-09-23
Export: import embedded JPEG -> exports as PNG 0 2021-12-19 2022-09-16
Add gzipped size for lightweight CSS frameworks 7 2019-05-20 2022-09-20
Some sort of builtin storage analyzer 1 2022-03-26 2022-07-15
Fix usage of deprecated method - ConfigurationContext.getFromContext(DataContext) 2 2021-10-07 2022-08-27
Recovering from keyword identifier mentions raw identifier that never appeared in the source code later 6 2022-06-08 2022-09-22
如何使用action 1 2020-07-31 2022-01-13
Migrate HLP architecture to match Function Helpers requirements 0 2022-03-03 2022-08-30
Change the default value of quitDriverOnBrowserReset config option to be the negation of whether to cache the driver or not 0 2021-02-26 2022-09-17
[Feature request] Different serializers for input and output approach 9 2021-09-23 2022-09-28
Including a Dockerfile in your change log directory causes StackOverflowException 3 2022-05-24 2022-09-15
[UI] Ability to impersonate Cluster Users 0 2022-07-07 2022-07-05
How in development is in development for this package? 1 2020-10-01 2022-09-16
Add a dotnet tool package 1 2018-02-08 2022-07-27
caddy ingress controller clusterrole is missing configmap resource access 1 2020-12-07 2022-09-16
RuntimeError: Horovod detected that one or more processes exited with non-zero status 1 2022-08-12 2022-09-25
`horovodrun` exits without error message 1 2022-08-11 2022-09-11
ForwardingCookieHandler.java line 136 3 2021-11-24 2022-09-29
Not Null Boolean Type didn't save from ChangeTracker Property 2 2022-07-23 2022-09-19
ReplaceService IMigrationsAssembly dosen't work 5 2022-07-23 2022-09-23