{"id":8573,"library":"quantities","title":"Quantities","description":"Quantities is a Python library designed to provide support for physical quantities with units, building on the popular NumPy library. It handles arithmetic and conversions of physical quantities, including magnitude, dimensionality, and uncertainty. The current version is 0.16.4. While actively developed with a stable API, test coverage is incomplete, and it is not recommended for mission-critical or production applications. It has an irregular, but active, release cadence.","status":"active","version":"0.16.4","language":"en","source_language":"en","source_url":"https://github.com/python-quantities/python-quantities","tags":["scientific","units","quantities","numpy","physical constants","data analysis"],"install":[{"cmd":"pip install quantities","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Quantities is built on NumPy and designed to work with its ufuncs.","package":"numpy"}],"imports":[{"note":"It is strongly suggested to import quantities to its own namespace to avoid accidental overwrites of unit and constant variables. 'pq' stands for 'physical quantities' or 'python quantities'.","symbol":"quantities","correct":"import quantities as pq"}],"quickstart":{"code":"import quantities as pq\n\ndistance = 42 * pq.meter\ntime = 17 * pq.second\nvelocity = distance / time\n\nprint(f\"Velocity: {velocity:.3f} {velocity.dimensionality}\")\n\n# Unit conversion\nvelocity_kmh = velocity.rescale(pq.km / pq.hour)\nprint(f\"Velocity in km/h: {velocity_kmh:.3f} {velocity_kmh.dimensionality}\")","lang":"python","description":"This quickstart demonstrates creating quantities with units, performing basic arithmetic, and converting between different units. The recommended practice is to import the library as `pq`."},"warnings":[{"fix":"Use with caution in critical systems; thoroughly test specific use cases or consider more mature alternatives like 'Pint' for production environments.","message":"Quantities is not suggested for mission-critical or production applications due to incomplete test coverage, despite having a stable API.","severity":"gotcha","affected_versions":"All versions up to 0.16.4"},{"fix":"Ensure that both operands have compatible units. If adding a scalar, it must either be multiplied by a unit or the quantity's magnitude extracted first. Example: `velocity + 3*pq.m/pq.s` or `velocity.magnitude + 3`.","message":"Directly adding a dimensionless number to a quantity with units will raise a ValueError due to unit incompatibility.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware that temperature quantities represent differences (e.g., a change of 20 degC) rather than absolute points (e.g., 20 degC on the Celsius scale). Calculations involving absolute temperatures might require manual handling or a different library.","message":"Quantities only treats temperatures as temperature differences and does not support absolute temperature scales or conversions between them.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For mathematical functions that expect dimensionless inputs (like trigonometric functions), ensure the quantity is dimensionless or extract its magnitude before passing it. For functions where unit-aware behavior is expected, verify the outcome carefully and consider wrapping the function to handle units explicitly.","message":"Many NumPy ufuncs (e.g., `np.sin`, `np.exp`) may ignore the dimensions of quantities, treating them as normal arrays and potentially raising 'not implemented' warnings.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to `quantities` version 0.16.4 or newer to resolve these compatibility issues with NumPy 2.x.","message":"Older versions (prior to 0.16.4) had issues with `deepcopy` and in-place arithmetic when used with NumPy 2.x.","severity":"breaking","affected_versions":"<0.16.4"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure all operands in an arithmetic expression have compatible units. For example, `velocity + 3 * pq.meter / pq.second` or perform the operation on magnitudes: `velocity.magnitude + 3`.","cause":"Attempting to perform an arithmetic operation (e.g., addition) between a quantity with units and a dimensionless number without explicitly providing units for the number.","error":"ValueError: Unable to convert between units of \"dimensionless\" and \"m/s\""},{"fix":"You cannot modify the definition of a base unit. To change the units of a `Quantity` object, assign a new unit string or `Unit` object to the `units` attribute of the `Quantity` instance, e.g., `q.units = 'feet'` or `q.units = pq.foot` (note the singular form for `pq.foot`).","cause":"Attempting to modify the `units` attribute of a fundamental unit directly (e.g., `pq.meter.units = 'feet'`). Fundamental units are immutable.","error":"AttributeError: can not modify protected units"},{"fix":"To get a scalar numerical value from a quantity, use `.magnitude` to access the underlying NumPy array (which might be a 0-d array for scalars) or `.item()` for a Python scalar from a 0-d array. Ensure the quantity is dimensionless if directly converting to a scalar without unit stripping.","cause":"Attempting to convert a quantity with units or a non-scalar quantity directly into a Python scalar (e.g., `float()`) without first removing its units or extracting its magnitude.","error":"TypeError: only dimensionless scalar quantities can be converted to Python scalars"}]}