itholic commented on a change in pull request #35747:
URL: https://github.com/apache/spark/pull/35747#discussion_r822410325
##########
File path: python/pyspark/pandas/series.py
##########
@@ -4440,13 +4449,45 @@ def replace(
weight 1.0
length 0.3
dtype: float64
+
+ Regular expression `to_replace`
+
+ >>> psser = ps.Series(['bat', 'foo', 'bait', 'abc', 'bar', 'zoo'])
+ >>> psser.replace(to_replace=r'^ba.$', value='new', regex=True)
+ 0 new
+ 1 foo
+ 2 bait
+ 3 abc
+ 4 new
+ 5 zoo
+ dtype: object
+
+ >>> psser.replace(value='new', regex=r'^.oo$')
+ 0 bat
+ 1 new
+ 2 bait
+ 3 abc
+ 4 bar
+ 5 new
+ dtype: object
"""
+ if isinstance(regex, str):
+ if to_replace is not None:
+ raise ValueError("'to_replace' must be 'None' if 'regex' is
not a bool")
+ to_replace = regex
+ regex = True
+ elif not isinstance(regex, bool):
+ raise NotImplementedError("regex of %s type is supported" %
type(regex).__name__)
Review comment:
Maybe `regex of %s type is "not" supported` ??
##########
File path: python/pyspark/pandas/series.py
##########
@@ -4283,17 +4283,20 @@ def keys(self) -> "ps.Index":
"""
return self.index
- # TODO: 'regex', 'method' parameter
+ # TODO: introduce 'method', 'limit', 'in_place'; fully support 'regex'
def replace(
self,
to_replace: Optional[Union[Any, List, Tuple, Dict]] = None,
value: Optional[Union[List, Tuple]] = None,
- regex: bool = False,
+ regex: Union[str, bool] = False,
) -> "Series":
"""
Replace values given in to_replace with value.
Values of the Series are replaced with other values dynamically.
+ .. note:: The API supports pattern matching (and replacement) on the
whole string only,
Review comment:
How about we also add this example to the note ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]