Skip to content
13 changes: 13 additions & 0 deletions common/polars.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import collections.abc
import re
import typing
from dataclasses import dataclass
Expand Down Expand Up @@ -181,6 +182,18 @@ def drop(
*columns: ColumnNameOrSelector | Iterable[ColumnNameOrSelector],
strict: bool = True,
) -> PathsDataFrame:
if strict:
requested: list[str] = []
for col in columns:
if isinstance(col, str):
requested.append(col)
elif isinstance(col, collections.abc.Iterable):
requested.extend(c for c in col if isinstance(c, str))
missing = [col for col in requested if col not in self.columns]
if missing:
raise Exception(
'Cannot drop non-existent column(s) %s; available columns: %s' % (', '.join(missing), ', '.join(self.columns))
)
Comment on lines +194 to +196

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve Polars' missing-column exception type

For a strict drop of a missing literal column, this now raises a generic Exception before Polars can raise its documented ColumnNotFoundError. Any caller that catches the Polars exception to distinguish a missing column from unrelated failures will no longer handle this case, despite the change being intended to improve only the error message. Raise pl.exceptions.ColumnNotFoundError with the enhanced text instead.

Useful? React with 👍 / 👎.

meta = self.get_meta()
df = super().drop(*columns, strict=strict)
for col in list(meta.units.keys()):
Expand Down
Loading
Loading