Skip to content

Commit fca0ae0

Browse files
committed
Changes merged
1 parent 4fb963b commit fca0ae0

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

pandas/io/excel.py

+7
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,15 @@ def _excel2num(x):
477477

478478
if isinstance(usecols, int):
479479
return i <= usecols
480+
# check if usecols is a string that indicates a comma separated list of
481+
# Excel column letters and column ranges
480482
elif isinstance(usecols, compat.string_types):
481483
return i in _range2cols(usecols)
484+
# check if usecols is a list of strings, each one indicating a Excel
485+
# column letter or a column range
486+
elif all(isinstance(x, compat.string_types) for x in usecols):
487+
usecols_str = ",".join(usecols)
488+
return i in _range2cols(usecols_str)
482489
else:
483490
return i in usecols
484491

pandas/tests/io/test_excel.py

+26
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,32 @@ def test_usecols_str(self, ext):
179179
tm.assert_frame_equal(df2, df1, check_names=False)
180180
tm.assert_frame_equal(df3, df1, check_names=False)
181181

182+
@pytest.mark.parametrize("columns,usecols,parse_cols", [
183+
(['A', 'B', 'C'], ['A:D'], ['A:D']),
184+
(['B', 'C'], ['A', 'C', 'D'], ['A', 'C', 'D']),
185+
(['B', 'C'], ['A', 'C:D'], ['A', 'C:D'])
186+
])
187+
# GH18273 - read_excel return empty dataframe when using usecols as a list
188+
# of strings
189+
def test_usecols_str_list(self, ext, columns, usecols, parse_cols):
190+
191+
dfref = self.get_csv_refdf('test1')
192+
193+
df1 = dfref.reindex(columns=columns)
194+
df2 = self.get_exceldf('test1', ext, 'Sheet1', index_col=0,
195+
usecols=usecols)
196+
df3 = self.get_exceldf('test1', ext, 'Sheet2', skiprows=[1],
197+
index_col=0, usecols=usecols)
198+
199+
with tm.assert_produces_warning(FutureWarning):
200+
df4 = self.get_exceldf('test1', ext, 'Sheet2', skiprows=[1],
201+
index_col=0, parse_cols=parse_cols)
202+
203+
# TODO add index to xls, read xls ignores index name ?
204+
tm.assert_frame_equal(df2, df1, check_names=False)
205+
tm.assert_frame_equal(df3, df1, check_names=False)
206+
tm.assert_frame_equal(df4, df1, check_names=False)
207+
182208
def test_excel_stop_iterator(self, ext):
183209

184210
parsed = self.get_exceldf('test2', ext, 'Sheet1')

0 commit comments

Comments
 (0)