Skip to content

BUG: using read_xml with iterparse and names will ignore duplicate values #47630

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 1 commit into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pandas/io/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def _iterparse_nodes(self, iterparse: Callable) -> list[dict[str, str | None]]:
for col, nm in zip(self.iterparse[row_node], self.names):
if curr_elem == col:
elem_val = elem.text.strip() if elem.text else None
if elem_val not in row.values() and nm not in row:
if row.get(nm) != elem_val and nm not in row:
row[nm] = elem_val
if col in elem.attrib:
if elem.attrib[col] not in row.values() and nm not in row:
Expand Down
40 changes: 40 additions & 0 deletions pandas/tests/io/xml/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,46 @@ def test_repeat_names(parser):
tm.assert_frame_equal(df_iter, df_expected)


def test_repeat_values_new_names(parser):
xml = """\
<shapes>
<shape>
<name>rectangle</name>
<family>rectangle</family>
</shape>
<shape>
<name>square</name>
<family>rectangle</family>
</shape>
<shape>
<name>ellipse</name>
<family>ellipse</family>
</shape>
<shape>
<name>circle</name>
<family>ellipse</family>
</shape>
</shapes>"""
df_xpath = read_xml(xml, xpath=".//shape", parser=parser, names=["name", "group"])

df_iter = read_xml_iterparse(
xml,
parser=parser,
iterparse={"shape": ["name", "family"]},
names=["name", "group"],
)

df_expected = DataFrame(
{
"name": ["rectangle", "square", "ellipse", "circle"],
"group": ["rectangle", "rectangle", "ellipse", "ellipse"],
}
)

tm.assert_frame_equal(df_xpath, df_expected)
tm.assert_frame_equal(df_iter, df_expected)


def test_names_option_wrong_length(datapath, parser):
filename = datapath("io", "data", "xml", "books.xml")

Expand Down