Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): detect and warn about usage of json.loads in conjunction with apply #10023

Merged
merged 2 commits into from
Jul 22, 2023

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jul 21, 2023

Ref: #9968.

Detects/handles both formulations...

  • pl.col("foo").apply(json.loads)
  • pl.col("foo").apply(lambda x: json.loads(x))

...resulting in (json.loads)...

# PolarsInefficientApplyWarning: 
# Expr.apply is significantly slower than the native expressions API.
# Only use if you absolutely CANNOT implement your logic otherwise.
# In this case, you can replace your `apply` with an expression:
#   -  pl.col("foo").apply(json.loads)
#   +  pl.col("foo").str.json_extract()

...or (wrapped in lambda):

# In this case, you can replace your `apply` with an expression:
#   -  pl.col("foo").apply(lambda x: ...)
#   +  pl.col("foo").str.json_extract()

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Jul 21, 2023
@alexander-beedie
Copy link
Collaborator Author

alexander-beedie commented Jul 21, 2023

Ahh… Will fix-up the unit tests that now fail (as they use json.loads via apply) in the morning.

Update: done 👍

Copy link
Collaborator

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it keeps getting better

py-polars/polars/utils/udfs.py Show resolved Hide resolved
@alexander-beedie alexander-beedie merged commit 9d79dbf into pola-rs:main Jul 22, 2023
@alexander-beedie alexander-beedie deleted the detect-json-in-apply branch July 22, 2023 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants