polars
polars copied to clipboard
Allow `.drop()` without argument as a no-op
Description
Currently applying .drop()
without an argument raises a TypeError saying it misses the required positional argument column
.
It can play a role of sanity check in some cases, but most times in programmable EDA user do not expect that dropping 0 column is an error.
Of course it can be fixed by checking length of columns to drop, but it is easy-to-miss and requires some mental memory (at least for me). Furthermore, this behavior is inconsistent with other projection-type operations in polars
:
df = pl.DataFrame({"A": [1, 2, 3]})
df.select()
# shape: (0, 0)
# ┌┐
# ╞╡
# └┘
df.with_columns()
# shape: (3, 1)
# ┌─────┐
# │ A │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 1 │
# │ 2 │
# │ 3 │
# └─────┘
df.drop()
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: DataFrame.drop() missing 1 required positional argument: 'columns'
So I'd like to suggest allowing 0 column drop operation as a no-op.
You can already do df.drop([])
as a no-op.
We can change the signature to *columns
instead of having columns
and *more_columns
separately, but this would lose the ability to specify df.drop(columns=["a", "b"])
.
I prefer the 'simple' function signature with only *columns
, so I'll probably go ahead and make this change.
Is it possible to avoid the breaking change by making columns
default to an empty iterable?
Is it possible to avoid the breaking change by making
columns
default to an empty iterable?
It's possible, but I am using this as an excuse to get rid of the complex function signature :)