Skip to content

REF: Deduplicate to_xml code #45132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 1, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions pandas/io/formats/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def other_namespaces(self) -> dict:

return nmsp_dict

def build_attribs(self, d: dict[str, Any], elem_row: Any) -> None:
def build_attribs(self, d: dict[str, Any], elem_row: Any) -> Any:
"""
Create attributes of row.

Expand All @@ -270,6 +270,7 @@ def build_attribs(self, d: dict[str, Any], elem_row: Any) -> None:
elem_row.attrib[attr_name] = val
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be copied?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe avoid val? (This change came on last recent to_xml PR).

if not isna(d[col]):
    elem_row.attrib[attr_name] = str(d[col])

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep thx, this sounds good

except KeyError:
raise KeyError(f"no valid column, {col}")
return elem_row

def _get_flat_col_name(self, col: str | tuple) -> str:
flat_col = col
Expand Down Expand Up @@ -347,7 +348,10 @@ def build_tree(self) -> bytes:
self.build_elems(d, elem_row)

else:
self.build_attribs(d, elem_row)
build_elem_row = self.build_attribs(d, elem_row)
if build_elem_row is not None:
elem_row = build_elem_row

self.build_elems(d, elem_row)

self.out_xml = tostring(self.root, method="xml", encoding=self.encoding)
Expand Down Expand Up @@ -465,7 +469,10 @@ def build_tree(self) -> bytes:
self.build_elems(d, elem_row)

else:
self.build_attribs(d, elem_row)
build_elem_row = self.build_attribs(d, elem_row)
if build_elem_row is not None:
elem_row = build_elem_row

self.build_elems(d, elem_row)

self.out_xml = tostring(
Expand Down