Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
ARROW-8314: [Python] Add a Table.select method to select a subset of columns#7272
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
fabec56f9fbe816fb066e5ae2e05042689ae476911File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -362,6 +362,26 @@ Result<std::shared_ptr<Table>> Table::RenameColumns( | ||
| return Table::Make(::arrow::schema(std::move(fields)), std::move(columns), num_rows()); | ||
| } | ||
| Result<std::shared_ptr<Table>> Table::SelectColumns( | ||
| const std::vector<int>& indices) const { | ||
| int n = static_cast<int>(indices.size()); | ||
| std::vector<std::shared_ptr<ChunkedArray>> columns(n); | ||
| std::vector<std::shared_ptr<Field>> fields(n); | ||
| for (int i = 0; i < n; i++) { | ||
| int pos = indices[i]; | ||
| if (pos < 0 || pos > num_columns() - 1) { | ||
| return Status::Invalid("Invalid column index ", pos, " to select columns."); | ||
| } | ||
| columns[i] = column(pos); | ||
| fields[i] = field(pos); | ||
| } | ||
| auto new_schema = | ||
| std::make_shared<arrow::Schema>(std::move(fields), schema()->metadata()); | ||
jorisvandenbossche marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| return Table::Make(new_schema, std::move(columns), num_rows()); | ||
| } | ||
| std::string Table::ToString() const { | ||
| std::stringstream ss; | ||
| ARROW_CHECK_OK(PrettyPrint(*this, 0, &ss)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -559,6 +559,22 @@ TEST_F(TestTable, RenameColumns) { | ||
| ASSERT_RAISES(Invalid, table->RenameColumns({"hello", "world"})); | ||
| } | ||
| TEST_F(TestTable, SelectColumns) { | ||
| MakeExample1(10); | ||
| auto table = Table::Make(schema_, columns_); | ||
| ASSERT_OK_AND_ASSIGN(auto subset, table->SelectColumns({0, 2})); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| ASSERT_OK(subset->ValidateFull()); | ||
| auto expexted_schema = ::arrow::schema({schema_->field(0), schema_->field(2)}); | ||
| auto expected = Table::Make(expexted_schema, {table->column(0), table->column(2)}); | ||
| ASSERT_TRUE(subset->Equals(*expected)); | ||
| // Out of bounds indices | ||
| ASSERT_RAISES(Invalid, table->SelectColumns({0, 3})); | ||
| ASSERT_RAISES(Invalid, table->SelectColumns({-1})); | ||
| } | ||
| TEST_F(TestTable, RemoveColumnEmpty) { | ||
| // ARROW-1865 | ||
| const int64_t length = 10; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is returning
Resultanyway we might as well boundscheck the indices, thoughts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a boundscheck