Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions python/ray/data/namespace_expressions/string_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,28 @@ def center(
self._expr, width, padding, *args, **kwargs
)

def lpad(
self, width: int, padding: str = " ", *args: Any, **kwargs: Any
) -> "UDFExpr":
"""Right-align strings by padding with a given character while respecting ``width``.

If the string is longer than the specified width, it remains intact (no truncation occurs).
"""
return _create_str_udf(pc.utf8_lpad, DataType.string())(
self._expr, width, padding, *args, **kwargs
)

def rpad(
self, width: int, padding: str = " ", *args: Any, **kwargs: Any
) -> "UDFExpr":
"""Left-align strings by padding with a given character while respecting ``width``.

If the string is longer than the specified width, it remains intact (no truncation occurs).
"""
return _create_str_udf(pc.utf8_rpad, DataType.string())(
self._expr, width, padding, *args, **kwargs
)

# Custom methods that need special logic beyond simple PyArrow function calls
def strip(self, characters: str | None = None) -> "UDFExpr":
"""Remove leading and trailing whitespace or specified characters.
Expand Down
2 changes: 2 additions & 0 deletions python/ray/data/tests/test_namespace_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ def test_string_trimming(
("pad", {"width": 5, "fillchar": "*", "side": "right"}, "hi***"),
("pad", {"width": 5, "fillchar": "*", "side": "left"}, "***hi"),
("pad", {"width": 6, "fillchar": "*", "side": "both"}, "**hi**"),
("lpad", {"width": 5, "padding": "*"}, "***hi"),
("rpad", {"width": 5, "padding": "*"}, "hi***"),
("center", {"width": 6, "padding": "*"}, "**hi**"),
],
)
Expand Down