Skip to content

Commit 4c695ec

Browse files
committed
Fix drop() method to handle quoted column names consistently
- Strip quotes from column names in drop() method - Maintains consistency with other DataFrame operations - Both drop('col') and drop('col') now work Fixes #1212
1 parent c609dfa commit 4c695ec

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

python/datafusion/dataframe.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,14 @@ def drop(self, *columns: str) -> DataFrame:
415415
Returns:
416416
DataFrame with those columns removed in the projection.
417417
"""
418-
return DataFrame(self.df.drop(*columns))
418+
normalized_columns = []
419+
for col in columns:
420+
if col.startswith('"') and col.endswith('"'):
421+
normalized_columns.append(col.strip('"')) # Removes quotes from both sides of col
422+
else:
423+
normalized_columns.append(col)
424+
425+
return DataFrame(self.df.drop(*normalized_columns))
419426

420427
def filter(self, *predicates: Expr) -> DataFrame:
421428
"""Return a DataFrame for which ``predicate`` evaluates to ``True``.

python/tests/test_dataframe.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,16 @@ def test_select(df):
216216
assert result.column(0) == pa.array([4, 5, 6])
217217
assert result.column(1) == pa.array([1, 2, 3])
218218

219-
219+
def test_drop_quoted_columns():
220+
ctx = SessionContext()
221+
batch = pa.RecordBatch.from_arrays([pa.array([1, 2, 3])], names=["ID_For_Students"])
222+
df = ctx.create_dataframe([[batch]])
223+
224+
# Both should work
225+
assert df.drop('"ID_For_Students"').schema().names == []
226+
assert df.drop('ID_For_Students').schema().names == []
227+
228+
220229
def test_select_mixed_expr_string(df):
221230
df = df.select(column("b"), "a")
222231

0 commit comments

Comments
 (0)