Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up get_column accessor #97

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading