Skip to content

Commit

Permalink
Improve get_column accessor (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche authored Jan 26, 2024
1 parent 72abd38 commit 11fb937
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/biocframe/BiocFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,22 @@ def get_column(self, column: Union[str, int]) -> Any:
The contents of the specified column.
"""
if isinstance(column, int):
column = self._column_names[column]
elif not isinstance(column, str):
raise TypeError("`column` must be either an integer index or column name.")
return self._data[column]
if column < 0:
raise IndexError("Index cannot be negative.")

if column > len(self._column_names):
raise IndexError("Index greater than the number of columns.")

return self._data[self._column_names[column]]
elif isinstance(column, str):
if column not in self._column_names:
raise AttributeError(f"Column: {column} does not exist.")

return self._data[column]

raise TypeError(
f"'column' must be a string or integer, provided '{type(column)}'."
)

def column(self, column: Union[str, int]) -> Any:
"""Alias for :py:meth:`~get_column`, provided for back-compatibility only."""
Expand Down

0 comments on commit 11fb937

Please sign in to comment.