polars icon indicating copy to clipboard operation
polars copied to clipboard

Allow `.drop()` without argument as a no-op

Open cjackal opened this issue 1 year ago • 3 comments

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.

cjackal avatar Oct 15 '23 15:10 cjackal

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.

stinodego avatar Oct 15 '23 16:10 stinodego

Is it possible to avoid the breaking change by making columns default to an empty iterable?

rancomp avatar Oct 16 '23 07:10 rancomp

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 :)

stinodego avatar Jan 05 '24 12:01 stinodego