8000 feat: expose drop method (#913) · kylebarron/datafusion-python@72f2743 · GitHub
[go: up one dir, main page]

Skip to content

Commit 72f2743

Browse files
authored
feat: expose drop method (apache#913)
1 parent 3d75172 commit 72f2743

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

python/datafusion/dataframe.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ def select(self, *exprs: Expr | str) -> DataFrame:
129129
]
130130
return DataFrame(self.df.select(*exprs_internal))
131131

132+
def drop(self, *columns: str) -> DataFrame:
133+
"""Drop arbitrary amount of columns.
134+
135+
Args:
136+
columns: Column names to drop from the dataframe.
137+
138+
Returns:
139+
DataFrame with those columns removed in the projection.
140+
"""
141+
return DataFrame(self.df.drop(*columns))
142+
132143
def filter(self, *predicates: Expr) -> DataFrame:
133144
"""Return a DataFrame for which ``predicate`` evaluates to ``True``.
134145

python/tests/test_dataframe.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ def test_sort(df):
169169
assert table.to_pydict() == expected
170170

171171

172+
def test_drop(df):
173+
df = df.drop("c")
174+
175+
# execute and collect the first (and only) batch
176+
result = df.collect()[0]
177+
178+
assert df.schema().names == ["a", "b"]
179+
assert result.column(0) == pa.array([1, 2, 3])
180+
assert result.column(1) == pa.array([4, 5, 6])
181+
182+
172183
def test_limit(df):
173184
df = df.limit(1)
174185

src/dataframe.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ impl PyDataFrame {
170170
Ok(Self::new(df))
171171
}
172172

173+
#[pyo3(signature = (*args))]
174+
fn drop(&self, args: Vec<PyBackedStr>) -> PyResult<Self> {
175+
let cols = args.iter().map(|s| s.as_ref()).collect::<Vec<&str>>();
176+
let df = self.df.as_ref().clone().drop_columns(&cols)?;
177+
Ok(Self::new(df))
178+
}
179+
173180
fn filter(&self, predicate: PyExpr) -> PyResult<Self> {
174181
let df = self.df.as_ref().clone().filter(predicate.into())?;
175182
Ok(Self::new(df))

0 commit comments

Comments
 (0)
0