Comments (8)
Isn't that the reason for its existence?
It's supposed to perform multiple replacements in each string, i.e. as if you did:
df.with_columns(
pl.col("text")
.str.replace_many(["123", "abc"], ["888", "zzz"])
.alias("replaced")
)
# shape: (2, 3)
# ┌────────┬─────────────┬──────────┐
# │ text ┆ replacement ┆ replaced │
# │ --- ┆ --- ┆ --- │
# │ str ┆ str ┆ str │
# ╞════════╪═════════════╪══════════╡
# │ 123abc ┆ 888 ┆ 888zzz │
# │ abc456 ┆ zzz ┆ zzz456 │
# └────────┴─────────────┴──────────┘
(Perhaps some simpler examples could be added to the docs?)
from polars.
Yes, but what if I wanted to provide a different replacement value for many string patterns per row? Otherwise, it seems kind of pointless to accept expression arguments.
How should I otherwise achieve the expected result?
df = pl.DataFrame({
"text": ["123abc", "abc456"],
"replacement": ["888", "zzz"],
})
df.with_columns(
pl.col("text")
.str.replace_many(["123", "abc"], pl.col("replacement"))
.alias("replaced")
)
# Actual
shape: (2, 3)
┌────────┬─────────────┬──────────┐
│ text ┆ replacement ┆ replaced │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞════════╪═════════════╪══════════╡
│ 123abc ┆ 888 ┆ 888zzz │ # <---- note the zzz from the below row
│ abc456 ┆ zzz ┆ zzz456 │
└────────┴─────────────┴──────────┘
# Expected
shape: (2, 3)
┌────────┬─────────────┬──────────┐
│ text ┆ replacement ┆ replaced │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞════════╪═════════════╪══════════╡
│ 123abc ┆ 888 ┆ 888888 │
│ abc456 ┆ zzz ┆ zzz456 │
└────────┴─────────────┴──────────┘
from polars.
Yeah, you want something like:
df.with_columns(
pl.col("text").str.replace(pl.col("text").head(3), pl.col("replacement"))
.alias("replaced")
)
# ComputeError: dynamic pattern length in 'str.replace' expressions is not supported yet
Whereas replace_many
is for doing similar to:
pl.col("text")
.str.replace("123", "888")
.str.replace("abc", "zzz")
from polars.
I don't think I am after pl.col("text").head(3)
as I would like to replace both "123" and "abc" on each row.
My apologies if I didn't get that across well.
Like so:
df.with_columns(
pl.col("text")
.str.replace_many(["123", "abc"], "888")
.alias("replaced")
)
shape: (2, 3)
┌────────┬─────────────┬──────────┐
│ text ┆ replacement ┆ replaced │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞════════╪═════════════╪══════════╡
│ 123abc ┆ 888 ┆ 888888 │
│ abc456 ┆ zzz ┆ 888456 │
└────────┴─────────────┴──────────┘
but instead of replacing both of the strings with "888" for every row, I would like the replacement value (for both "123" and "abc") to be based on pl.col("replacement")
(i.e. the value in the given row)
from polars.
To be honest, I just came across this when trying to give expression inputs to Expr.str.replace_many
. I don't really need to do this type of replacement, but was more so seeing what was possible. If expressions are supported here, the behaviour definitely seems unexpected to me.
Looking at Series.str.replace_many
, expressions are not supported, so perhaps the type signature for Expr.str.replace_many
is more generous than what is (currently) supported...? 🤷♂️
Same thing goes for str.contains_any
with the different signatures between Series
and Expr
from polars.
I really don't know if this is how it is intended to work but this gets your intended result (at least for example 1 and 2)
def cust_replace_many(source: pl.Expr|str, patterns:list, replace_with:pl.Expr|str):
if isinstance(source, str):
source=pl.col(source)
if isinstance(replace_with, str):
replace_with=pl.col(replace_with)
for x in patterns:
source=source.str.replace(x, replace_with)
return source
df.with_columns(replaced=cust_replace_many('text', ['123','abc'], 'replacement'))
shape: (2, 3)
┌────────┬─────────────┬──────────┐
│ text ┆ replacement ┆ replaced │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞════════╪═════════════╪══════════╡
│ 123abc ┆ 888 ┆ 888888 │
│ abc456 ┆ zzz ┆ zzz456 │
└────────┴─────────────┴──────────┘
If you monkey patch it to pl.Expr
then it works off of the source column
pl.Expr.cust_replace_many=cust_replace_many
df.with_columns(replaced=pl.col('text').cust_replace_many(['123','abc'], 'replacement'))
shape: (2, 3)
┌────────┬─────────────┬──────────┐
│ text ┆ replacement ┆ replaced │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞════════╪═════════════╪══════════╡
│ 123abc ┆ 888 ┆ 888888 │
│ abc456 ┆ zzz ┆ zzz456 │
└────────┴─────────────┴──────────┘
For example 3 you could do this:
(
df
.group_by('replace', maintain_order=True)
.agg(
pl.col('text').first(),
replaced=pl.col('text').str.replace_many('replace','woo').first()
)
)
shape: (2, 3)
┌─────────┬────────┬──────────┐
│ replace ┆ text ┆ replaced │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞═════════╪════════╪══════════╡
│ 123 ┆ 123abc ┆ woowoo │
│ abc ┆ abc456 ┆ woo456 │
└─────────┴────────┴──────────┘
Actually the group_by approach works for example 1 too
(
df
.group_by('replacement', maintain_order=True)
.agg(
pl.col('text').first(),
replaced=pl.col('text').str.replace_many(["123", "abc"],pl.col("replacement")).first()
)
)
shape: (2, 3)
┌─────────────┬────────┬──────────┐
│ replacement ┆ text ┆ replaced │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞═════════════╪════════╪══════════╡
│ 888 ┆ 123abc ┆ 888zzz │
│ zzz ┆ abc456 ┆ zzz456 │
└─────────────┴────────┴──────────┘
from polars.
I would like the replacement value to be based on the value in the given row
You can mimic the behaviour with a window expression:
df.with_columns(
pl.col("text")
.str.replace_many(["123", "abc"], pl.col("replacement").first())
.over("replacement")
.alias("replaced")
)
shape: (2, 3)
┌────────┬─────────────┬──────────┐
│ text ┆ replacement ┆ replaced │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞════════╪═════════════╪══════════╡
│ 123abc ┆ 888 ┆ 888888 │
│ abc456 ┆ zzz ┆ zzz456 │
└────────┴─────────────┴──────────┘
But that specific task is just not what replace_many
is "designed" for. (Or perhaps I am mistaken?)
from polars.
Thank you both for being forthcoming with suggestions.
I will say I am still confused about how expressions are intended to be used (if at all) in Expr.str.replace_many
It does not seem like they work "out of the box" unless the column being referred to only has 1 unique value (at which point you might as well use a string literal)
Example 2 works when a string literal is passed as the 2nd argument instead of an expression.
I am starting to question whether expressions were intended to be supported at all? Or whether the signature was intended to be more like Series.str.replace_many
from polars.
Related Issues (20)
- Sorting a LazyFrame in streaming mode with a `Struct` column on a Mac, panics and then hangs HOT 1
- Make pivot args as positional instead of keyword only
- Get random state (python)
- Support native writes to s3 and gs
- Right-align numerical headers in `write_excel` HOT 2
- `write_excel(..., autofit=True)` auto-fits to source, not formatted data HOT 2
- Wrong (probably Typo) word in the output HOT 3
- Is it possible to resolve the schema, but not expand the paths when doing a `collect_schema`? HOT 2
- `unnest()` should have option to rename fields with separator HOT 3
- Converting Spark to Polars and back HOT 1
- Explanation does not match code example in User Guide / Expressions / Aggregation
- Ensure column names are unique for horizontal concat in the IR
- PanicException on reading Parquet file from S3 HOT 4
- add an `ignore_nulls` option to `json_encode` for the `pl.Struct` column
- Change all columns one time HOT 2
- map_elements replaces all array elements with nulls
- Let arr.reshape use Expr as inputs instead of just python tuple
- Import and Export Schema Objects to JSON
- error[E0599]: no variant or associated item named `Struct` found for enum `polars_core::datatypes::DataType` in the current scope
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from polars.