Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
XML ='''
<values>
<guidedSaleKey>
<code>9023000918982</code>
<externalReference>0102350511</externalReference>
</guidedSaleKey>
<store>
<code>02300</code>
<externalReference>1543</externalReference>
<currency>EUR</currency>
</store>
</values>
'''
df = pd.read_xml(XML,iterparse={"values":["code","code"]}, names=["guided_code","store_code"])
print(df)
Issue Description
dataframe will not be able to return value of both code
elements from guidedSaleKey
and store
this will return this:
guided_code store_code
0 9023000918982 9023000918982
Expected Behavior
this should return a dataframe like this:
guided_code store_code
0 9023000918982 2300
Installed Versions
Comment From: bama-chi
there was quite same bug with attributes on this issue
Comment From: ParfaitG
I am not sure this is a bug. Per read_xml docs, the dictionary key in iterparse
is intended to be the repeating element which serializes as rows in DataFrame.
Here you are using the root which never repeats in XML. Conceptually, guidedSaleKey
and store
are two different sets that can repeat in varying numbers. You can run read_xml
separately then join DataFrames if their repetition matches.
Comment From: ParfaitG
However, your issue may relate to an iterparse
issue regarding same-named elements which I recently observed on this SO post:
from io import BytesIO
import pandas as pd
xml = b'''\
<?xml version="1.0" encoding="UTF-8"?>
<histdata>
<item>
<datetime>20-1-2023 00:00:00</datetime>
<value channel="Traffic Total" chid="-1">92 kbit/s</value>
<value channel="Traffic In" chid="0">77 kbit/s</value>
<value channel="Traffic Out" chid="1">16 kbit/s</value>
<value channel="Downtime" chid="-4">0 %</value>
<coverage>100 %</coverage>
</item>
<item>
<datetime>20-1-2023 00:05:00</datetime>
<value channel="Traffic Total" chid="-1">82 kbit/s</value>
<value channel="Traffic In" chid="0">727 kbit/s</value>
<value channel="Traffic Out" chid="1">18 kbit/s</value>
<value channel="Downtime" chid="-4">0 %</value>
<coverage>100 %</coverage>
</item>
</histdata>
'''
with BytesIO(xml) as f: # only latest pandas 2.0 dev supports file-like objects for iterparse
items_df = pd.read_xml(
f,
iterparse={"item": [
"datetime", "value", "value", "value", "value", "coverage"
]},
names = [
"datetime", "value1", "value2", "value3", "value4", "coverage"
]
)
items_df
# datetime value1 value2 value3 value4 coverage
# 0 20-1-2023 00:00:00 92 kbit/s 92 kbit/s 92 kbit/s 92 kbit/s 100 %
# 1 20-1-2023 00:05:00 82 kbit/s 82 kbit/s 82 kbit/s 82 kbit/s 100 %
As you can see the first <value>
element text repeats for all other <value>
instances. Interestingly, the repetitious result is not observed when only parsing the repeating attributes suggesting a bug:
with BytesIO(xml) as f:
attribs_df = pd.read_xml(
f,
iterparse={"item": [
"datetime", "channel", "chid", "channel", "chid", "channel", "chid", "channel", "chid", "coverage"
]},
names = [
"datetime", "channe11", "chid1", "channel2", "chid2", "channel3", "chid3", "channel4", "chid4", "coverage"
]
)
attribs_df
# datetime channe11 chid1 channel2 chid2 channel3 chid3 channel4 chid4 coverage
# 0 20-1-2023 00:00:00 Traffic Total -1 Traffic In 0 Traffic Out 1 Downtime -4 100 %
# 1 20-1-2023 00:05:00 Traffic Total -1 Traffic In 0 Traffic Out 1 Downtime -4 100 %