Skip to content

Commit 6bac008

Browse files
scls19frjorisvandenbossche
scls19fr
authored andcommitted
ENH: return an OrderedDict from read_excel with sheetname=None (pandas-dev#14571)
1 parent 62b2ff3 commit 6bac008

File tree

6 files changed

+9
-5
lines changed

6 files changed

+9
-5
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ New features
3030
Other enhancements
3131
^^^^^^^^^^^^^^^^^^
3232

33-
33+
- ``pd.read_excel`` now preserves sheet order when using ``sheetname=None`` (:issue:`9930`)
3434

3535

3636
.. _whatsnew_0200.api_breaking:

pandas/io/excel.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from pandas.tseries.period import Period
2222
from pandas import json
2323
from pandas.compat import (map, zip, reduce, range, lrange, u, add_metaclass,
24-
string_types)
24+
string_types, OrderedDict)
2525
from pandas.core import config
2626
from pandas.formats.printing import pprint_thing
2727
import pandas.compat as compat
@@ -418,9 +418,9 @@ def _parse_cell(cell_contents, cell_typ):
418418
sheets = [sheetname]
419419

420420
# handle same-type duplicates.
421-
sheets = list(set(sheets))
421+
sheets = list(OrderedDict.fromkeys(sheets).keys())
422422

423-
output = {}
423+
output = OrderedDict()
424424

425425
for asheetname in sheets:
426426
if verbose:
-5 KB
Binary file not shown.
296 Bytes
Binary file not shown.
315 Bytes
Binary file not shown.

pandas/io/tests/test_excel.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,12 @@ def test_reading_all_sheets(self):
379379
# See PR #9450
380380
basename = 'test_multisheet'
381381
dfs = self.get_exceldf(basename, sheetname=None)
382-
expected_keys = ['Alpha', 'Beta', 'Charlie']
382+
# ensure this is not alphabetical to test order preservation
383+
expected_keys = ['Charlie', 'Alpha', 'Beta']
383384
tm.assert_contains_all(expected_keys, dfs.keys())
385+
# Issue 9930
386+
# Ensure sheet order is preserved
387+
tm.assert_equal(expected_keys, list(dfs.keys()))
384388

385389
def test_reading_multiple_specific_sheets(self):
386390
# Test reading specific sheetnames by specifying a mixed list

0 commit comments

Comments
 (0)