Skip to content
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

BUG: to_xml raises KeyError for index=False when the index does not start at zero #42458

Closed
3 tasks done
stephan-hesselmann-by opened this issue Jul 9, 2021 · 1 comment · Fixed by #42464
Closed
3 tasks done
Labels
Bug IO XML read_xml, to_xml
Milestone

Comments

@stephan-hesselmann-by
Copy link
Contributor

stephan-hesselmann-by commented Jul 9, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import pandas as pd

records = {"col1": [1, 2], "col2": [3, 4]}
df = pd.DataFrame(data=records)

df.index = range(1, 3)  # if this line is commented out it works as expected
xml = df.to_xml(index=False)
print(xml)

Problem description

This code snipped should not raise a KeyError. The index should be ignored regardless of its content.

Traceback:

Traceback (most recent call last):
  File "/Users/xxx/xml.py", line 7, in <module>
    xml = df.to_xml(index=False)
  File "/Users/xxx/pandas/core/frame.py", line 2967, in to_xml
    xml_formatter = TreeBuilder(
  File "/Users/xxx/pandas/io/formats/xml.py", line 458, in __init__
    self.handle_indexes()
  File "/Users/xxx/pandas/io/formats/xml.py", line 199, in handle_indexes
    x for x in self.frame_dicts[0].keys() if x not in self.orig_cols
KeyError: 0

Expected Output

<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <col1>1</col1>
    <col2>3</col2>
  </row>
  <row>
    <col1>2</col1>
    <col2>4</col2>
  </row>
</data>

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f00ed8f
python : 3.9.6.final.0
python-bits : 64
OS : Darwin
OS-release : 20.5.0
Version : Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.0
numpy : 1.21.0
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.3
setuptools : None
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : 4.0.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : None
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 2021.06.1
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 4.0.1
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 1.4.15
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
numba : None
None

@stephan-hesselmann-by stephan-hesselmann-by added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 9, 2021
stephan-hesselmann-by added a commit to stephan-hesselmann-by/pandas that referenced this issue Jul 9, 2021
Fixes pandas-dev#42458

It was assumed that the index contains the element `0`. This led to a
defect when the index of the input Dataframe has an offset, which is a
common use case when streaming Dataframes via generators.

This fix consists of not relying on accessing the `0` element of
`frame_dicts`.
stephan-hesselmann-by added a commit to stephan-hesselmann-by/pandas that referenced this issue Jul 9, 2021
Fixes pandas-dev#42458

It was assumed that the index contains the element `0`. This led to a
defect when the index of the input Dataframe has an offset, which is a
common use case when streaming Dataframes via generators.

This fix consists of not relying on accessing the `0` element of
`frame_dicts`.
stephan-hesselmann-by added a commit to stephan-hesselmann-by/pandas that referenced this issue Jul 9, 2021
Fixes pandas-dev#42458

It was assumed that the index contains the element `0`. This led to a
defect when the index of the input Dataframe has an offset, which is a
common use case when streaming Dataframes via generators.

This fix consists of not relying on accessing the `0` element of
`frame_dicts`.
stephan-hesselmann-by added a commit to stephan-hesselmann-by/pandas that referenced this issue Jul 9, 2021
Fixes pandas-dev#42458

It was assumed that the index contains the element `0`. This led to a
defect when the index of the input Dataframe has an offset, which is a
common use case when streaming Dataframes via generators.

This fix consists of not relying on accessing the `0` element of
`frame_dicts`.
stephan-hesselmann-by added a commit to stephan-hesselmann-by/pandas that referenced this issue Jul 11, 2021
Fixes pandas-dev#42458

It was assumed that the index contains the element `0`. This led to a
defect when the index of the input Dataframe has an offset, which is a
common use case when streaming Dataframes via generators.

This fix consists of not relying on accessing the `0` element of
`frame_dicts`.
@jreback jreback added IO XML read_xml, to_xml and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 12, 2021
@jreback jreback added this to the 1.3.1 milestone Jul 12, 2021
@ParfaitG
Copy link
Contributor

Good catch! Yes. zero should not be hard-coded in handling indexes: self.frame_dicts[0].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO XML read_xml, to_xml
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants