Skip to content

BUG: V1.2 DataFrame.to_csv() fails to write a file with codecs #39247

@Kazzz-S

Description

@Kazzz-S
  • I have checked that this issue has not already been reported.

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import pandas as pd
import codecs

x    = [ 1, 2, 3, 4, 5  ]
y    = [ 6, 7, 8, 9, 10 ]
z    = [ 'a', 'b', 'c', 'd', 'e' ]
data = { "X": x, "Y":y, "Z":z }
df   = pd.DataFrame( data, columns=[ "X", "Y", "Z" ] )
print( "Pandas version = %s" % pd.__version__ )
print(df)

fp = codecs.open( "out-testPD12.csv", "w", "utf-8" )
fp.write( "Pandas version = %s\n" % pd.__version__  )
df.to_csv( fp, index=False, header=True )
fp.close()

Problem description

When saving the file, TypeError: utf_8_encode() argument 1 must be str, not bytes flags on.

Expected Output

The output below has been obtained by downgrading and pinned Pandas to V1.1.5.
V1.1.2 is also tested OK.

Pandas version = 1.1.5
X,Y,Z
1,6,a
2,7,b
3,8,c
4,9,d
5,10,e

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c
python : 3.8.2.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Tue Nov 10 00:10:30 PST 2020; root:xnu-6153.141.10~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.2.0
numpy : 1.19.2
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 51.1.2.post20210112
Cython : 0.29.21
pytest : 6.2.1
hypothesis : None
sphinx : 3.4.3
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.8.3
fastparquet : None
gcsfs : None
matplotlib : 3.3.2
numexpr : 2.7.2
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : 1.3.21
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.51.2

(base) Catalina1{kazzz-s} temp (1)% python testPD12.py 
Pandas version = 1.2.0
   X   Y  Z
0  1   6  a
1  2   7  b
2  3   8  c
3  4   9  d
4  5  10  e
Traceback (most recent call last):
  File "testPD12.py", line 52, in <module>
    df.to_csv( fp, index=False, header=True )
  File "/Users/kazzz-s/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py", line 3384, in to_csv
    return DataFrameRenderer(formatter).to_csv(
  File "/Users/kazzz-s/opt/anaconda3/lib/python3.8/site-packages/pandas/io/formats/format.py", line 1083, in to_csv
    csv_formatter.save()
  File "/Users/kazzz-s/opt/anaconda3/lib/python3.8/site-packages/pandas/io/formats/csvs.py", line 248, in save
    self._save()
  File "/Users/kazzz-s/opt/anaconda3/lib/python3.8/site-packages/pandas/io/common.py", line 104, in __exit__
    self.close()
  File "/Users/kazzz-s/opt/anaconda3/lib/python3.8/site-packages/pandas/io/common.py", line 89, in close
    self.handle.flush()
  File "/Users/kazzz-s/opt/anaconda3/lib/python3.8/codecs.py", line 721, in write
    return self.writer.write(data)
  File "/Users/kazzz-s/opt/anaconda3/lib/python3.8/codecs.py", line 377, in write
    data, consumed = self.encode(object, self.errors)
TypeError: utf_8_encode() argument 1 must be str, not bytes

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO DataIO issues that don't fit into a more specific labelRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions