Skip to content

BUG: PyArrow-backed DataFrame not exportable to Stata format #54671

Closed
@maharris77

Description

@maharris77

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 pyarrow as pa
df = pd.DataFrame(data=[[1, 'a'], [2, 'b']],
                  index=[0, 1],
                  columns=['one', 'two'],
                  )
df = df.astype({'one': pd.ArrowDtype(pa.int64()),
                'two': pd.ArrowDtype(pa.string())})
df.to_stata('test_stata.dta', version=118)

Issue Description

The ArrowDtype columns are not recognized by the if/else logic of pandas.io.stata._dtype_to_default_stata_fmt, resulting in error with output:

"NotImplementedError: Data type int64[pyarrow] not supported."

I suspect this is true for all the PyArrow data types.

Note that fixing PyArrow issues in DataFrame.to_stata will also require recognizing PyArrow datetimes and dates in pandas.io.stata._prepare_pandas, for example replacing the line if lib.is_np_dtype(data[col].dtype, "M"): with if lib.is_np_dtype(data[col].dtype, "M") or is_datetime64_any_dtype(data[col].dtype): after importing the function is_datetime64_any_dtype from pandas.core.dtypes.common. However, I'm not sure that workaround handles all cases, including dates that are not datetimes (and maybe there is a faster C implementation?). I do not yet have a workaround for the dtype conversion logic that is the main issue.

Expected Behavior

Should run without error and output a Stata format (.dta) file.

Installed Versions

INSTALLED VERSIONS

commit : 6e194cf
python : 3.11.4.final.0
python-bits : 64
OS : Darwin
OS-release : 23.0.0
Version : Darwin Kernel Version 23.0.0: Tue Aug 1 03:24:46 PDT 2023; root:xnu-10002.0.242.0.6~31/RELEASE_ARM64_T8112
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 2.1.0rc0
numpy : 1.25.1
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2.1
Cython : None
pytest : 7.4.0
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : 2.9.6
jinja2 : 3.1.2
IPython : 8.7.0
pandas_datareader : None
bs4 : 4.12.2
bottleneck : None
brotli :
dataframe-api-compat: None
fastparquet : 2023.7.0
fsspec : 2023.6.0
gcsfs : None
matplotlib : 3.7.1
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 12.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.1
snappy : None
sqlalchemy : 1.4.47
tables : None
tabulate : None
xarray : 2023.7.0
xlrd : 2.0.1
zstandard : 0.19.0
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Arrowpyarrow functionalityBugIO Stataread_stata, to_stataNA - MaskedArraysRelated to pd.NA and nullable extension arrays

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions