Skip to content

Commit

Permalink
take advantage of try_get_column_index
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Jul 26, 2024
1 parent 1484ef8 commit cc0440b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
7 changes: 2 additions & 5 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4755,12 +4755,9 @@ def get_column_index(self, name: str) -> int:
>>> df.get_column_index("ham")
2
>>> df.get_column_index("sandwich") # doctest: +SKIP
ColumnNotFoundError: 'sandwich' not found in DataFrame
ColumnNotFoundError: sandwich
"""
if (idx := self._df.get_column_index(name)) is None:
msg = f"{name!r} not found in DataFrame"
raise ColumnNotFoundError(msg)
return idx
return self._df.get_column_index(name)

def replace_column(self, index: int, column: Series) -> DataFrame:
"""
Expand Down
7 changes: 5 additions & 2 deletions py-polars/src/dataframe/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,11 @@ impl PyDataFrame {
}
}

pub fn get_column_index(&self, name: &str) -> Option<usize> {
self.df.get_column_index(name)
pub fn get_column_index(&self, name: &str) -> PyResult<usize> {
Ok(self
.df
.try_get_column_index(name)
.map_err(PyPolarsErr::from)?)
}

pub fn get_column(&self, name: &str) -> PyResult<PySeries> {
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/dataframe/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -2848,5 +2848,5 @@ def test_get_column_index() -> None:
assert df.get_column_index("actual") == 0
assert df.get_column_index("expected") == 1

with pytest.raises(ColumnNotFoundError, match="'missing' not found in DataFrame"):
with pytest.raises(ColumnNotFoundError, match="missing"):
df.get_column_index("missing")

0 comments on commit cc0440b

Please sign in to comment.