Skip to content

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

Closed
@stephan-hesselmann-by

Description

@stephan-hesselmann-by
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions