Skip to content

Commit

Permalink
feat: expose drop method (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
ion-elgreco authored Oct 15, 2024
1 parent 3d75172 commit 72f2743
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions python/datafusion/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ def select(self, *exprs: Expr | str) -> DataFrame:
]
return DataFrame(self.df.select(*exprs_internal))

def drop(self, *columns: str) -> DataFrame:
"""Drop arbitrary amount of columns.
Args:
columns: Column names to drop from the dataframe.
Returns:
DataFrame with those columns removed in the projection.
"""
return DataFrame(self.df.drop(*columns))

def filter(self, *predicates: Expr) -> DataFrame:
"""Return a DataFrame for which ``predicate`` evaluates to ``True``.
Expand Down
11 changes: 11 additions & 0 deletions python/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ def test_sort(df):
assert table.to_pydict() == expected


def test_drop(df):
df = df.drop("c")

# execute and collect the first (and only) batch
result = df.collect()[0]

assert df.schema().names == ["a", "b"]
assert result.column(0) == pa.array([1, 2, 3])
assert result.column(1) == pa.array([4, 5, 6])


def test_limit(df):
df = df.limit(1)

Expand Down
7 changes: 7 additions & 0 deletions src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ impl PyDataFrame {
Ok(Self::new(df))
}

#[pyo3(signature = (*args))]
fn drop(&self, args: Vec<PyBackedStr>) -> PyResult<Self> {
let cols = args.iter().map(|s| s.as_ref()).collect::<Vec<&str>>();
let df = self.df.as_ref().clone().drop_columns(&cols)?;
Ok(Self::new(df))
}

fn filter(&self, predicate: PyExpr) -> PyResult<Self> {
let df = self.df.as_ref().clone().filter(predicate.into())?;
Ok(Self::new(df))
Expand Down

0 comments on commit 72f2743

Please sign in to comment.