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

fix(python): Release GIL in gather_with_series() and friend #19383

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions crates/polars-python/src/dataframe/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,16 @@ impl PyDataFrame {
Ok(PyDataFrame::new(df))
}

pub fn gather(&self, indices: Wrap<Vec<IdxSize>>) -> PyResult<Self> {
pub fn gather(&self, py: Python, indices: Wrap<Vec<IdxSize>>) -> PyResult<Self> {
let indices = indices.0;
let indices = IdxCa::from_vec("".into(), indices);
let df = self.df.take(&indices).map_err(PyPolarsErr::from)?;
let df = Python::allow_threads(py, || self.df.take(&indices).map_err(PyPolarsErr::from))?;
Ok(PyDataFrame::new(df))
}

pub fn gather_with_series(&self, indices: &PySeries) -> PyResult<Self> {
pub fn gather_with_series(&self, py: Python, indices: &PySeries) -> PyResult<Self> {
let indices = indices.series.idx().map_err(PyPolarsErr::from)?;
let df = self.df.take(indices).map_err(PyPolarsErr::from)?;
let df = Python::allow_threads(py, || self.df.take(indices).map_err(PyPolarsErr::from))?;
Ok(PyDataFrame::new(df))
}

Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/dataframe/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,9 @@ def test_df_getitem_5343() -> None:
assert df[4, 5] == 1024
assert_frame_equal(df[4, [2]], pl.DataFrame({"foo2": [16]}))
assert_frame_equal(df[4, [5]], pl.DataFrame({"foo5": [1024]}))


def test_no_deadlock_19358() -> None:
s = pl.Series(["text"] * 100 + [1] * 100, dtype=pl.Object)
result = s.to_frame()[[0, -1]]
assert result[""].to_list() == ["text", 1]