Skip to content

BUG: Saving to Stata exception - TypeError: encoding without a string argument #46229

@raffaem

Description

@raffaem

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

I'm calling this on a .csv file:

#!/usr/bin/env python3

import pandas as pd
import sys

infp = sys.argv[1]
outfp = infp.replace(".csv",".dta")
df = pd.read_csv(infp, header=0, low_memory=False) 
df.to_stata(outfp, version=118, write_index=False)

Issue Description

Traceback (most recent call last):
  File "/run/media/raffaele/55ab61c4-83cf-4d9f-a5cd-7fcfdc14b4fb/data/progetti_miei/various_tools/csv2dta", line 9, in <module>
    df.to_stata(outfp, version=118, write_index=False)
  File "/home/raffaele/.local/lib/python3.10/site-packages/pandas/util/_decorators.py", line 207, in wrapper
    return func(*args, **kwargs)
  File "/home/raffaele/.local/lib/python3.10/site-packages/pandas/core/frame.py", line 2652, in to_stata
    writer.write_file()
  File "/home/raffaele/.local/lib/python3.10/site-packages/pandas/io/stata.py", line 2659, in write_file
    raise exc
  File "/home/raffaele/.local/lib/python3.10/site-packages/pandas/io/stata.py", line 2639, in write_file
    records = self._prepare_data()
  File "/home/raffaele/.local/lib/python3.10/site-packages/pandas/io/stata.py", line 2827, in _prepare_data
    data = self._convert_strls(data)
  File "/home/raffaele/.local/lib/python3.10/site-packages/pandas/io/stata.py", line 3454, in _convert_strls
    self._strl_blob = ssw.generate_blob(tab)
  File "/home/raffaele/.local/lib/python3.10/site-packages/pandas/io/stata.py", line 3081, in generate_blob
    utf8_string = bytes(strl, "utf-8")
TypeError: encoding without a string argument

Expected Behavior

Should not raise an exception

Installed Versions

Details

pd.show_versions()

INSTALLED VERSIONS

commit : 06d2301
python : 3.10.2.final.0
python-bits : 64
OS : Linux
OS-release : 5.16.10-200.fc35.x86_64
Version : #1 SMP PREEMPT Wed Feb 16 13:28:00 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.1
numpy : 1.22.2
pytz : 2021.3
dateutil : 2.8.1
pip : 21.2.3
setuptools : 57.4.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.7.1
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.29.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO Stataread_stata, to_stataNeeds InfoClarification about behavior needed to assess issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions