Skip to content

feat: preserve field order when replacing existing fields in with_field#3681

Merged
ianna merged 6 commits intoscikit-hep:mainfrom
DylanModesitt:dcm/preserve-field-order-with-field
Oct 17, 2025
Merged

feat: preserve field order when replacing existing fields in with_field#3681
ianna merged 6 commits intoscikit-hep:mainfrom
DylanModesitt:dcm/preserve-field-order-with-field

Conversation

@DylanModesitt
Copy link
Copy Markdown
Contributor

@DylanModesitt DylanModesitt commented Oct 16, 2025

Closes: #3680

This PR modifies ak.with_field to preserve field order when replacing existing fields. Previously, replacing a field would remove it from its original position and append it to the end.

Changes

  • Modified _impl in src/awkward/operations/ak_with_field.py to detect when replacing vs. adding fields
  • Added some tests tests/test_3681_preserve_field_order_with_field.py

Before:

array = ak.Array([{"x": 1, "y": 2, "z": 3}])
result = ak.with_field(array, [100], "y")
# Result: {"x": 1, "z": 3, "y": 100} 
# Fields: ["x", "z", "y"]

After:

array = ak.Array([{"x": 1, "y": 2, "z": 3}])
result = ak.with_field(array, [100], "y")
# Result: {"x": 1, "y": 100, "z": 3} 
# Fields: ["x", "y", "z"]

@github-actions
Copy link
Copy Markdown

The documentation preview is ready to be viewed at http://preview.awkward-array.org.s3-website.us-east-1.amazonaws.com/PR3681

@codecov
Copy link
Copy Markdown

codecov bot commented Oct 16, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.68%. Comparing base (b749e49) to head (4b986f9).
⚠️ Report is 443 commits behind head on main.

Additional details and impacted files
Files with missing lines Coverage Δ
src/awkward/operations/ak_with_field.py 100.00% <100.00%> (ø)

... and 196 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ikrommyd
Copy link
Copy Markdown
Collaborator

@DylanModesitt It may be worth extracting base.fields in a variable before the "if-else" stuff since you're using it multiple times inside these blocks.

@ianna
Copy link
Copy Markdown
Member

ianna commented Oct 17, 2025

@all-contributors please add @DylanModesitt for code

@allcontributors
Copy link
Copy Markdown
Contributor

@ianna

I've put up a pull request to add @DylanModesitt! 🎉

Copy link
Copy Markdown
Member

@ianna ianna left a comment

Choose a reason for hiding this comment

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

@DylanModesitt - This is great! Thank you for fixing it. The tests pass, please, go ahead and merge it if you have finished with it. Thanks!

@DylanModesitt
Copy link
Copy Markdown
Contributor Author

@ianna Thanks you! Think it will need another review approval with write access (required a merge commit). I'm happy to merge it once there is another approval or feel free to do so (I am finished).

@ianna ianna enabled auto-merge (squash) October 17, 2025 13:07
@ianna ianna merged commit 1a90068 into scikit-hep:main Oct 17, 2025
41 checks passed
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

Successfully merging this pull request may close these issues.

Preserve field order in with_field

3 participants