Skip to content

Commit 767df54

Browse files
committed
Refactor sorting logic in DataFrame to handle string and Expr types for improved error handling and clarity
1 parent 79caa6f commit 767df54

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

python/datafusion/dataframe.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,14 +552,20 @@ def sort(self, *exprs: Expr | SortExpr | str) -> DataFrame:
552552
Returns:
553553
DataFrame after sorting.
554554
"""
555-
expr_seq = [e for e in exprs if not isinstance(e, SortExpr)]
556-
raw_exprs_iter = iter(expr_list_to_raw_expr_list(expr_seq))
557555
exprs_raw = []
558556
for e in exprs:
559557
if isinstance(e, SortExpr):
560558
exprs_raw.append(sort_or_default(e))
559+
elif isinstance(e, str):
560+
exprs_raw.append(sort_or_default(Expr.column(e)))
561+
elif isinstance(e, Expr):
562+
exprs_raw.append(sort_or_default(e))
561563
else:
562-
exprs_raw.append(sort_or_default(Expr(next(raw_exprs_iter))))
564+
error = (
565+
"Expected Expr or column name, found:"
566+
f" {type(e).__name__}. Use col() or lit() to construct expressions."
567+
)
568+
raise TypeError(error)
563569
return DataFrame(self.df.sort(*exprs_raw))
564570

565571
def cast(self, mapping: dict[str, pa.DataType[Any]]) -> DataFrame:

0 commit comments

Comments
 (0)