Skip to content

BUG: cannot write xml with nullable dtypes #43903

Closed
@dmyersturnbull

Description

@dmyersturnbull

  • 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 master branch of pandas.

Reproducible Example

import logging
import numpy as np
import pandas as pd
import pytest

logger = logging.getLogger("test")

class XmlTest:

    def test_to_xml_dtypes(self):
        dtypes = [
            pd.StringDtype(),
            pd.BooleanDtype(),
            pd.Float64Dtype(),
            pd.Float32Dtype(),
            pd.Int64Dtype(),
            pd.UInt64Dtype(),
            pd.Int32Dtype(),
            pd.UInt32Dtype(),
            pd.Int16Dtype(),
            pd.UInt16Dtype(),
            pd.Int8Dtype(),
            pd.UInt8Dtype(),
            pd.StringDtype(),
        ]
        data = [
            pd.Series({"abc": 1, "xyz": pd.NA}),
            pd.Series({"abc": pd.NA, "xyz": 0}),
        ]
        failed = {}
        for dtype in dtypes:
            df = pd.DataFrame(data).astype(dtype)
            try:
                df.to_xml()
            except TypeError as e:
                logger.error(dtype, exc_info=True)
                failed[str(dtype)] = str(e)
        assert failed == [], f"Failed on dtypes: {failed}"

if __name__ == "__main__":
    pytest.main()

Issue Description

to_xml fails with nullable dtypes provided by Pandas like Float64Dtype. Example from a test run in CI; it fails on Ubuntu too.

Expected Behavior

Output valid xml that can be read with read_xml to get approximately the same DataFrame.

Installed Versions

INSTALLED VERSIONS ------------------ commit : f00ed8f python : 3.9.5.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19042 machine : AMD64 processor : Intel64 Family 6 Model 165 Stepping 2, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United States.1252

pandas : 1.3.0
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.1
setuptools : 52.0.0.post20210125
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : 1.0.2
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 2021.07.0
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : 1.0.8
s3fs : None
scipy : 1.7.0
sqlalchemy : None
tables : None
tabulate : 0.8.9
xarray : None
xlrd : 2.0.1
xlwt : None
numba : None
None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions