Skip to content

Keep units in np.concatenate, hstack, vstack, stack and append - #293

Open
syahra712 wants to merge 2 commits into
python-quantities:masterfrom
syahra712:fix/concatenate-units
Open

syahra712 wants to merge 2 commits into
python-quantities:masterfrom
syahra712:fix/concatenate-units

Conversation

@syahra712

Copy link
Copy Markdown
Contributor

Fixes #50 and #88.

np.concatenate on Quantity arrays joined the bare magnitudes and returned a dimensionless result. np.hstack, np.vstack, np.stack and np.append go through it, so they lost the units too:

>>> q = [1, 2, 3] * pq.m
>>> np.hstack([q, q])
array([1., 2., 3., 1., 2., 3.]) * dimensionless   # now: * m

This handles np.concatenate in Quantity.__array_function__, like np.ptp. The result takes the units of the first array and the others are rescaled to them, so np.concatenate([[1, 2] * pq.m, [50, 300] * pq.cm]) gives [1, 2, 0.5, 3] m, and incompatible units raise ValueError.

It only applies when every item is a plain Quantity. These keep their old behaviour: a mix with non-Quantity items (e.g. np.append(q, np.nan), which test_nanargmax relies on), UncertainQuantity, and calls with out=.

Tests cover concatenate (axes, axis=None, unit conversion, incompatible units, out/dtype, plain arrays) and hstack/vstack/stack/append. Full suite passes on NumPy 2.5 and Ruff is clean. I also removed vstack from the "functions which drop units" list and added a changelog line.

@apdavison apdavison added this to the 0.18.0 milestone Nov 19, 2026
@apdavison

Copy link
Copy Markdown
Contributor

Thanks for this. Keeping units through np.concatenate and the stacking functions is clearly the right behaviour, and the implementation is clean. I've moved it to the 0.18.0 milestone rather than merging it for 0.17.0 (released today), because it breaks downstream code that relies on the current behaviour.

I ran Neo's core tests with this branch, and three fail (test_Epoch_merge, test_Event_merge and TestSpikeTrainList::test_multiplexed). Neo has several places that do something like

times = np.hstack([self.times, othertimes]) * self.times.units

which currently gives ms but with this PR gives ms**2. Other projects probably have the same pattern, so the plan is:

  1. Fix Neo first, so that it works with both behaviours (e.g. np.hstack(...).magnitude * units).
  2. Merge this for 0.18.0, with the changelog entry marked as a behaviour change, like the out= change in 0.17.0, and moved to a new "Unreleased" section (0.17.0 is now released).

There's also one inconsistency to settle before merging: np.append(q, 3 * pq.m) keeps the units, but np.append(q, 3) still returns a dimensionless result. I'm not sure yet what the right behaviour is for a mix of Quantity and non-Quantity arguments, especially since np.append(q, np.nan) is a common idiom (one of our own tests uses it). What do you think?

@syahra712

Copy link
Copy Markdown
Contributor Author

Thanks for running Neo against it, that's a really useful check. The plan sounds good to me. I merged master in, which resolved the conflict, and moved the changelog entry to a new "Unreleased" section, marked as a Behaviour change. It now names the np.hstack([a, b]) * a.units pattern and says to use .magnitude instead.

On the mixed case, I'd keep what the PR does now: as soon as any argument isn't a Quantity, it falls back to the old behaviour. My reasons:

  • np.append(q, np.nan) is common, and for anything other than dimensionless q there's no unit we could give nan without guessing. Treating plain values as dimensionless would make that idiom raise. Treating them as having q's units would quietly turn np.append(q_in_m, 3) into 3 m, and that's exactly the kind of silent unit assumption the library otherwise avoids.
  • It keeps the behaviour change limited to calls where every argument is a Quantity. Those are the calls where the old dimensionless result was clearly wrong, so the downstream breakage stays as small as possible.

If you'd rather have mixed calls be consistent too, I think raising for a non-Quantity argument when q isn't dimensionless is the cleanest option. That could go in later as its own behaviour change, though. Happy to add a test that pins the current mixed behaviour so it doesn't change by accident.

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.

numpy.hstack and numpy.vstack discard units

2 participants