Skip to content

Commit 58b6e06

Browse files
authored
BUG: to_xml raising for pd.NA (#45116)
1 parent de82ced commit 58b6e06

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ I/O
856856
- Bug in :func:`to_csv` always coercing datetime columns with different formats to the same format (:issue:`21734`)
857857
- :meth:`DataFrame.to_csv` and :meth:`Series.to_csv` with ``compression`` set to ``'zip'`` no longer create a zip file containing a file ending with ".zip". Instead, they try to infer the inner file name more smartly. (:issue:`39465`)
858858
- Bug in :func:`read_csv` where reading a mixed column of booleans and missing values to a float type results in the missing values becoming 1.0 rather than NaN (:issue:`42808`, :issue:`34120`)
859+
- Bug in :func:`to_xml` raising error for ``pd.NA`` with extension array dtype (:issue:`43903`)
859860
- Bug in :func:`read_csv` when passing simultaneously a parser in ``date_parser`` and ``parse_dates=False``, the parsing was still called (:issue:`44366`)
860861
- Bug in :func:`read_csv` not setting name of :class:`MultiIndex` columns correctly when ``index_col`` is not the first column (:issue:`38549`)
861862
- Bug in :func:`read_csv` silently ignoring errors when failing to create a memory-mapped file (:issue:`44766`)

pandas/io/formats/xml.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from pandas.util._decorators import doc
1919

2020
from pandas.core.dtypes.common import is_list_like
21+
from pandas.core.dtypes.missing import isna
2122

2223
from pandas.core.frame import DataFrame
2324
from pandas.core.shared_docs import _shared_docs
@@ -571,9 +572,7 @@ def build_elems(self) -> None:
571572
elem_name = f"{self.prefix_uri}{flat_col}"
572573
try:
573574
val = (
574-
None
575-
if self.d[col] in [None, ""] or self.d[col] != self.d[col]
576-
else str(self.d[col])
575+
None if isna(self.d[col]) or self.d[col] == "" else str(self.d[col])
577576
)
578577
SubElement(self.elem_row, elem_name).text = val
579578
except KeyError:

pandas/tests/io/xml/__init__.py

Whitespace-only changes.

pandas/tests/io/xml/test_to_xml.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import pandas.util._test_decorators as td
1313

1414
from pandas import (
15+
NA,
1516
DataFrame,
1617
Index,
1718
)
@@ -1307,15 +1308,25 @@ def test_filename_and_suffix_comp(parser, compression_only):
13071308
assert geom_xml == output.strip()
13081309

13091310

1311+
@td.skip_if_no("lxml")
1312+
def test_ea_dtypes(any_numeric_ea_dtype):
1313+
# GH#43903
1314+
expected = """<?xml version='1.0' encoding='utf-8'?>
1315+
<data>
1316+
<row>
1317+
<index>0</index>
1318+
<a/>
1319+
</row>
1320+
</data>"""
1321+
df = DataFrame({"a": [NA]}).astype(any_numeric_ea_dtype)
1322+
result = df.to_xml()
1323+
assert result.strip() == expected
1324+
1325+
13101326
def test_unsuported_compression(datapath, parser):
13111327
with pytest.raises(ValueError, match="Unrecognized compression type"):
13121328
with tm.ensure_clean() as path:
1313-
# Argument "compression" to "to_xml" of "DataFrame" has incompatible type
1314-
# "Literal['7z']"; expected "Union[Literal['infer'], Literal['gzip'],
1315-
# Literal['bz2'], Literal['zip'], Literal['xz'], Dict[str, Any], None]"
1316-
geom_df.to_xml(
1317-
path, parser=parser, compression="7z" # type: ignore[arg-type]
1318-
)
1329+
geom_df.to_xml(path, parser=parser, compression="7z")
13191330

13201331

13211332
# STORAGE OPTIONS

pandas/tests/io/xml/test_xml.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,7 @@ def test_names_option_wrong_type(datapath, parser):
684684
filename = datapath("io", "data", "xml", "books.xml")
685685

686686
with pytest.raises(TypeError, match=("is not a valid type for names")):
687-
read_xml(
688-
filename, names="Col1, Col2, Col3", parser=parser # type: ignore[arg-type]
689-
)
687+
read_xml(filename, names="Col1, Col2, Col3", parser=parser)
690688

691689

692690
# ENCODING
@@ -1056,10 +1054,7 @@ def test_wrong_compression(parser, compression, compression_only):
10561054
def test_unsuported_compression(datapath, parser):
10571055
with pytest.raises(ValueError, match="Unrecognized compression type"):
10581056
with tm.ensure_clean() as path:
1059-
# error: Argument "compression" to "read_xml" has incompatible type
1060-
# "Literal['7z']"; expected "Union[Literal['infer'], Literal['gzip'],
1061-
# Literal['bz2'], Literal['zip'], Literal['xz'], Dict[str, Any], None]"
1062-
read_xml(path, parser=parser, compression="7z") # type: ignore[arg-type]
1057+
read_xml(path, parser=parser, compression="7z")
10631058

10641059

10651060
# STORAGE OPTIONS

0 commit comments

Comments
 (0)