File tree 2 files changed +49
-8
lines changed
2 files changed +49
-8
lines changed Original file line number Diff line number Diff line change @@ -195,14 +195,16 @@ def handle_indexes(self) -> None:
195
195
This method will add indexes into attr_cols or elem_cols.
196
196
"""
197
197
198
- indexes : list [str ] = [
199
- x for x in self .frame_dicts [0 ].keys () if x not in self .orig_cols
200
- ]
198
+ if not self .index :
199
+ return
200
+
201
+ first_dict = next (iter (self .frame_dicts .values ()))
202
+ indexes : list [str ] = [x for x in first_dict .keys () if x not in self .orig_cols ]
201
203
202
- if self .attr_cols and self . index :
204
+ if self .attr_cols :
203
205
self .attr_cols = indexes + self .attr_cols
204
206
205
- if self .elem_cols and self . index :
207
+ if self .elem_cols :
206
208
self .elem_cols = indexes + self .elem_cols
207
209
208
210
def get_prefix_uri (self ) -> str :
@@ -307,7 +309,7 @@ def build_tree(self) -> bytes:
307
309
self .elem_row = SubElement (self .root , f"{ self .prefix_uri } { self .row_name } " )
308
310
309
311
if not self .attr_cols and not self .elem_cols :
310
- self .elem_cols = list (self . frame_dicts [ 0 ] .keys ())
312
+ self .elem_cols = list (d .keys ())
311
313
self .build_elems ()
312
314
313
315
else :
@@ -477,7 +479,7 @@ def build_tree(self) -> bytes:
477
479
self .elem_row = SubElement (self .root , f"{ self .prefix_uri } { self .row_name } " )
478
480
479
481
if not self .attr_cols and not self .elem_cols :
480
- self .elem_cols = list (self . frame_dicts [ 0 ] .keys ())
482
+ self .elem_cols = list (d .keys ())
481
483
self .build_elems ()
482
484
483
485
else :
Original file line number Diff line number Diff line change 11
11
12
12
import pandas .util ._test_decorators as td
13
13
14
- from pandas import DataFrame
14
+ from pandas import (
15
+ DataFrame ,
16
+ RangeIndex ,
17
+ )
15
18
import pandas ._testing as tm
16
19
17
20
from pandas .io .common import get_handle
@@ -290,6 +293,42 @@ def test_index_false_rename_row_root(datapath, parser):
290
293
assert output == expected
291
294
292
295
296
+ def test_index_false_with_offset_input_index (parser ):
297
+ """
298
+ Tests that the output does not contain the `<index>` field when the index of the
299
+ input Dataframe has an offset.
300
+
301
+ This is a regression test for issue #42458.
302
+ """
303
+
304
+ expected = """\
305
+ <?xml version='1.0' encoding='utf-8'?>
306
+ <data>
307
+ <row>
308
+ <shape>square</shape>
309
+ <degrees>360</degrees>
310
+ <sides>4.0</sides>
311
+ </row>
312
+ <row>
313
+ <shape>circle</shape>
314
+ <degrees>360</degrees>
315
+ <sides/>
316
+ </row>
317
+ <row>
318
+ <shape>triangle</shape>
319
+ <degrees>180</degrees>
320
+ <sides>3.0</sides>
321
+ </row>
322
+ </data>"""
323
+
324
+ offset_geom_df = geom_df .copy ()
325
+ offset_geom_df .index = RangeIndex (start = 10 , end = 13 , step = 1 )
326
+ output = offset_geom_df .to_xml (index = False , parser = parser )
327
+ output = equalize_decl (output )
328
+
329
+ assert output == expected
330
+
331
+
293
332
# NA_REP
294
333
295
334
na_expected = """\
You can’t perform that action at this time.
0 commit comments