Optimization: Replace shift().fillna(0) with shift(fill_value=0) in cumulative time-weight calculation
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 148
- Forks
- 55
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 18
Description
https://github.com/PyPSA/pypsa-usa/blob/1655e3408b740c4d7ce65d9d951bb39881f6ac4c/workflow/scripts/prepare_network.py#L92
Hi, I’d like to suggest a small performance and clarity improvement in the following line:
start = time_weighting.cumsum().shift().fillna(0)
This can be simplified to:
start = time_weighting.cumsum().shift(fill_value=0)
The shift(fill_value=0) version performs the shift and fill in a single pass, avoiding the creation of an intermediate Series containing NaNs and the overhead of a second pass for fillna(0). Internally, this is implemented more efficiently using lower-level C operations within Pandas.
While functionally identical, the optimized version improves runtime performance and reduces memory allocation, especially when applied in time-critical loops or large-scale pipelines. It also improves code readability by making the default fill behavior explicit in the shift step.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Open workflow/scripts/prepare_network.py at line 92 and inspect the cumulative time-weight calculation. Replace the chained shift().fillna(0) expression with the specified shift(fill_value=0) form, then verify that the calculation remains functionally equivalent. Done means the targeted line uses the single shift call without changing its result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- pandas, python
- Domain
- performance
- Issue type
- Refactor
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100