8000 Suggest replacing .apply(int) with .astype(int) for performance · Issue #586 · pypest/pyemu · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Suggest replacing .apply(int) with .astype(int) for performance #586

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

Open
SaFE-APIOpt opened this issue Apr 21, 2025 · 1 comment
Open

Comments

@SaFE-APIOpt
Copy link

https://github.com/pypest/pyemu/blob/22c26e96761952fc95845213eb628885e8a7fde8/autotest/utils/to_pestpp.py#L42C1-L45C50
Hi, I’d like to suggest a performance improvement to the following assignments:

flow_df.loc[:,"k"] = flow_df.lay.apply(int) - 1
flow_df.loc[:,"i"] = flow_df.row.apply(int) - 1
flow_df.loc[:,"j"] = flow_df.col.apply(int) - 1
flow_df.loc[:,"wsp"] = flow_df.wsp.apply(int) - 1

These can be more efficiently rewritten using Pandas’ vectorized .astype(int) method:

flow_df["k"] = flow_df["lay"].astype(int) - 1
flow_df["i"] = flow_df["row"].astype(int) - 1
flow_df["j"] = flow_df["col"].astype(int) - 1
flow_df["wsp"] = flow_df["wsp"].astype(int) - 1

.apply(int) invokes a Python-level loop with individual function calls per element, which is much slower and more memory-intensive than .astype(int), especially for large DataFrames. Using .astype() leverages optimized, compiled code for bulk operations, making it both faster and more memory-efficient.

@jtwhite79
Copy link
Collaborator

sounds like a winner to me. @dbsi-pinkman wanna have a shot at a PR for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0