Skip to content

Commit

Permalink
Changed drop method to ignore missing columns - #61
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Apr 14, 2024
1 parent be17aaa commit 844dc45
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Added `data_page_size` option to `write_parquet` method
- Added `truncate_ragged_lines` option to `read_csv`, `read_csv_batched`, and `scan_csv` methods
- Added precompiled gem for Linux x86-64 MUSL
- Changed `drop` method to ignore missing columns

## 0.9.0 (2024-03-03)

Expand Down
40 changes: 30 additions & 10 deletions lib/polars/data_frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2861,16 +2861,36 @@ def extend(other)
# # │ 2 ┆ 7.0 │
# # │ 3 ┆ 8.0 │
# # └─────┴─────┘
def drop(columns)
if columns.is_a?(::Array)
df = clone
columns.each do |n|
df._df.drop_in_place(n)
end
df
else
_from_rbdf(_df.drop(columns))
end
#
# @example Drop multiple columns by passing a list of column names.
# df.drop(["bar", "ham"])
# # =>
# # shape: (3, 1)
# # ┌─────┐
# # │ foo │
# # │ --- │
# # │ i64 │
# # ╞═════╡
# # │ 1 │
# # │ 2 │
# # │ 3 │
# # └─────┘
#
# @example Use positional arguments to drop multiple columns.
# df.drop("foo", "ham")
# # =>
# # shape: (3, 1)
# # ┌─────┐
# # │ bar │
# # │ --- │
# # │ f64 │
# # ╞═════╡
# # │ 6.0 │
# # │ 7.0 │
# # │ 8.0 │
# # └─────┘
def drop(*columns)
lazy.drop(*columns).collect(_eager: true)
end

# Drop in place.
Expand Down
8 changes: 3 additions & 5 deletions lib/polars/lazy_frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2001,11 +2001,9 @@ def with_column(column)
# - List of column names.
#
# @return [LazyFrame]
def drop(columns)
if columns.is_a?(::String)
columns = [columns]
end
_from_rbldf(_ldf.drop(columns))
def drop(*columns)
drop_cols = Utils._expand_selectors(self, *columns)
self._from_rbldf(_ldf.drop(drop_cols))
end

# Rename column names.
Expand Down
20 changes: 20 additions & 0 deletions lib/polars/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,5 +364,25 @@ def self._check_arg_is_1byte(arg_name, arg, can_be_empty = false)
end
end
end

def self._expand_selectors(frame, *items)
items_iter = _parse_inputs_as_iterable(items)

expanded = []
items_iter.each do |item|
if is_selector(item)
selector_cols = expand_selector(frame, item)
expanded.concat(selector_cols)
else
expanded << item
end
end
expanded
end

# TODO
def self.is_selector(obj)
false
end
end
end

0 comments on commit 844dc45

Please sign in to comment.