Skip to content

BUG: Problem using to_csv with BytesIO #37292

Closed
@amotl

Description

@amotl

Dear people of Pandas,

first things first: Thanks for all of your excellent work conceiving and maintaining Pandas. You know who you are.

With kind regards,
Andreas.


  • 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.


Code Sample, a copy-pastable example

import pandas as pd
from io import BytesIO

df = pd.DataFrame()
buffer = BytesIO()

df.to_csv(buffer)

Problem description

The snippet above croaks with

TypeError: a bytes-like object is required, not 'str'

We expected this to work. However, we will be happy to learn otherwise. We also had a look at #22555 and #35129 which seem to be related but not exactly on the spot.

The background on this is that we are currently in the process of upgrading Kotori to Python 3 (yeah, we are late to the game). However, coming from this, we can confirm it worked when using Pandas 0.18.1 on Python 2 the other day.

The relevant code is

Thanks already for looking into this!

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.6.9.final.0
python-bits : 64
OS : Darwin
OS-release : 17.7.0
Version : Darwin Kernel Version 17.7.0: Thu Jun 18 21:21:34 PDT 2020; root:xnu-4570.71.82.5~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 1.1.3
numpy : 1.19.2
pytz : 2018.9
dateutil : 2.8.1
pip : 20.2.3
setuptools : 50.3.0
Cython : None
pytest : 4.6.9
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.3.6
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.8
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions