Determine what column a single column id (name or index) matches in a series of column names. Note that integer values are *always* treated as positional identifiers. If you happen to have column names which are also integers, you must specify them using a positional index.
(column_names, c, column_offset=1)
| 485 | |
| 486 | |
| 487 | def match_column_identifier(column_names, c, column_offset=1): |
| 488 | """ |
| 489 | Determine what column a single column id (name or index) matches in a series of column names. |
| 490 | Note that integer values are *always* treated as positional identifiers. If you happen to have |
| 491 | column names which are also integers, you must specify them using a positional index. |
| 492 | """ |
| 493 | if isinstance(c, str) and not c.isdigit() and c in column_names: |
| 494 | return column_names.index(c) |
| 495 | |
| 496 | try: |
| 497 | c = int(c) - column_offset |
| 498 | # Fail out if neither a column name nor an integer |
| 499 | except ValueError: |
| 500 | raise ColumnIdentifierError("Column '%s' is invalid. It is neither an integer nor a column name. " |
| 501 | "Column names are: %s" % (c, repr(column_names)[1:-1])) |
| 502 | |
| 503 | # Fail out if index is 0-based |
| 504 | if c < 0: |
| 505 | raise ColumnIdentifierError("Column %i is invalid. Columns are 1-based." % (c + column_offset)) |
| 506 | |
| 507 | # Fail out if index is out of range |
| 508 | if c >= len(column_names): |
| 509 | raise ColumnIdentifierError("Column %i is invalid. The last column is '%s' at index %i." % ( |
| 510 | c + column_offset, column_names[-1], len(column_names) - 1 + column_offset)) |
| 511 | |
| 512 | return c |
| 513 | |
| 514 | |
| 515 | def parse_column_identifiers(ids, column_names, column_offset=1, excluded_columns=None): |