Skip to content

Commit 8c30fad

Browse files
committed
Enhance DataFrame.sort method to accept column names as sorting expressions
1 parent 33e61a7 commit 8c30fad

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

python/datafusion/dataframe.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -521,19 +521,22 @@ def aggregate(
521521
aggs = [e.expr for e in aggs]
522522
return DataFrame(self.df.aggregate(group_by, aggs))
523523

524-
def sort(self, *exprs: Expr | SortExpr) -> DataFrame:
525-
"""Sort the DataFrame by the specified sorting expressions.
524+
def sort(self, *exprs: Expr | SortExpr | str) -> DataFrame:
525+
"""Sort the DataFrame by the specified sorting expressions or column names.
526526
527527
Note that any expression can be turned into a sort expression by
528-
calling its` ``sort`` method.
528+
calling its ``sort`` method.
529529
530530
Args:
531-
exprs: Sort expressions, applied in order.
531+
exprs: Sort expressions or column names, applied in order.
532532
533533
Returns:
534534
DataFrame after sorting.
535535
"""
536-
exprs_raw = [sort_or_default(expr) for expr in exprs]
536+
exprs_raw = []
537+
for expr in exprs:
538+
expr_obj = Expr.column(expr).sort() if isinstance(expr, str) else expr
539+
exprs_raw.append(sort_or_default(expr_obj))
537540
return DataFrame(self.df.sort(*exprs_raw))
538541

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

0 commit comments

Comments
 (0)