Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Implement select_columns #39

Merged
merged 1 commit into from
Mar 15, 2022
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
10 changes: 10 additions & 0 deletions datafusion/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def test_select(df):
assert result.column(1) == pa.array([-3, -3, -3])


def test_select_colums(df):
df = df.select_columns("b", "a")

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

assert result.column(0) == pa.array([4, 5, 6])
assert result.column(1) == pa.array([1, 2, 3])


def test_filter(df):
df = df.select(
column("a") + column("b"),
Expand Down
6 changes: 6 additions & 0 deletions src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ impl PyDataFrame {
self.df.schema().into()
}

#[args(args = "*")]
fn select_columns(&self, args: Vec<&str>) -> PyResult<Self> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can instead provide __getitem__ which is more pythonic

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, i think we could have both 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been looking at the python documentation for __getitem__ but it isn't clear to me how this would apply here for selecting multiple columns?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the idea is to index by list:

df[["a", "b"]]

let df = self.df.select_columns(&args)?;
Ok(Self::new(df))
}

#[args(args = "*")]
fn select(&self, args: Vec<PyExpr>) -> PyResult<Self> {
let expr = args.into_iter().map(|e| e.into()).collect();
Expand Down