Skip to content

Commit 0e9acad

Browse files
Tobias Brandtwesm
Tobias Brandt
authored andcommitted
ENH: Allow referencing of Excel columns by their Excel column names.
1 parent 9c50207 commit 0e9acad

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

pandas/io/parsers.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,8 @@ def parse(self, sheetname, header=0, skiprows=None, skip_footer=0,
13261326
If None then parse all columns,
13271327
If int then indicates last column to be parsed
13281328
If list of ints then indicates list of column numbers to be parsed
1329+
If string then indicates comma separated list of column names and
1330+
column ranges (e.g. "A:E" or "A,C,E:F")
13291331
na_values : list-like, default None
13301332
List of additional strings to recognize as NA/NaN
13311333
@@ -1350,8 +1352,34 @@ def parse(self, sheetname, header=0, skiprows=None, skip_footer=0,
13501352
skip_footer=skip_footer)
13511353

13521354
def _should_parse(self, i, parse_cols):
1355+
1356+
def _range2cols(areas):
1357+
"""
1358+
Convert comma separated list of column names and column ranges to a
1359+
list of 0-based column indexes.
1360+
1361+
>>> _range2cols('A:E')
1362+
[0, 1, 2, 3, 4]
1363+
>>> _range2cols('A,C,Z:AB')
1364+
[0, 2, 25, 26, 27]
1365+
"""
1366+
def _excel2num(x):
1367+
"Convert Excel column name like 'AB' to 0-based column index"
1368+
return reduce(lambda s,a: s*26+ord(a)-ord('A')+1, x.upper().strip(), 0)-1
1369+
1370+
cols = []
1371+
for rng in areas.split(','):
1372+
if ':' in rng:
1373+
rng = rng.split(':')
1374+
cols += range(_excel2num(rng[0]), _excel2num(rng[1])+1)
1375+
else:
1376+
cols.append(_excel2num(rng))
1377+
return cols
1378+
13531379
if isinstance(parse_cols, int):
13541380
return i <= parse_cols
1381+
if isinstance(parse_cols, basestring):
1382+
return i in _range2cols(parse_cols)
13551383
else:
13561384
return i in parse_cols
13571385

0 commit comments

Comments
 (0)