Skip to content

Commit 682aa73

Browse files
myandprlee1258561
authored andcommitted
[Data] Compute Expression-str Padding (ray-project#59552)
Signed-off-by: lee1258561 <lee1258561@gmail.com>
1 parent edba569 commit 682aa73

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

python/ray/data/namespace_expressions/string_namespace.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,28 @@ def center(
258258
self._expr, width, padding, *args, **kwargs
259259
)
260260

261+
def lpad(
262+
self, width: int, padding: str = " ", *args: Any, **kwargs: Any
263+
) -> "UDFExpr":
264+
"""Right-align strings by padding with a given character while respecting ``width``.
265+
266+
If the string is longer than the specified width, it remains intact (no truncation occurs).
267+
"""
268+
return _create_str_udf(pc.utf8_lpad, DataType.string())(
269+
self._expr, width, padding, *args, **kwargs
270+
)
271+
272+
def rpad(
273+
self, width: int, padding: str = " ", *args: Any, **kwargs: Any
274+
) -> "UDFExpr":
275+
"""Left-align strings by padding with a given character while respecting ``width``.
276+
277+
If the string is longer than the specified width, it remains intact (no truncation occurs).
278+
"""
279+
return _create_str_udf(pc.utf8_rpad, DataType.string())(
280+
self._expr, width, padding, *args, **kwargs
281+
)
282+
261283
# Custom methods that need special logic beyond simple PyArrow function calls
262284
def strip(self, characters: str | None = None) -> "UDFExpr":
263285
"""Remove leading and trailing whitespace or specified characters.

python/ray/data/tests/test_namespace_expressions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ def test_string_trimming(
234234
("pad", {"width": 5, "fillchar": "*", "side": "right"}, "hi***"),
235235
("pad", {"width": 5, "fillchar": "*", "side": "left"}, "***hi"),
236236
("pad", {"width": 6, "fillchar": "*", "side": "both"}, "**hi**"),
237+
("lpad", {"width": 5, "padding": "*"}, "***hi"),
238+
("rpad", {"width": 5, "padding": "*"}, "hi***"),
237239
("center", {"width": 6, "padding": "*"}, "**hi**"),
238240
],
239241
)

0 commit comments

Comments
 (0)