astropy / astropy/quantity-2.0

How to implement unit conversion?

Open
#4 10 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
5
Forks
4
PR merge metrics
No merged PRs in 30d

Description

A question is how to best do unit conversion. We could follow astropy `Quantity` have methods like `.to()` and perhaps `.to_value()`, or could let the `Quantity` class not worry about it. E.g., astropy's `Unit` allows construction with `array * unit` (array copied) or `array << unit` (array not copied). Currently, `Quantity2.0` does not copy, so the two might become equivalent.

Following astropy, one could imagine doing unit conversion via `q << new_unit` (which could be implemented either via `Quantity.__lshift__` or `Unit.__rlshift__`).

As an operator equivalent of `.to_value()`, one option might be to use the reverse direction, `q >> value_unit` (shift out the value with the given unit, where `<<` shifts the input to the unit).

Another might be `q // unit` - the logic here would be that one "truncate after dividing by the unit", inspired by `q1 // q2` being the truncated dimensionless ratio of the two quantities [1]. Again, it could either be `Quantity.__floordiv__` or `Unit.__rfloordiv__` that takes care of this. A disadvantage is that `q1 // unit` is then different from `q1 // (1*unit)`, but perhaps not too bad -- we may want to avoid more generally to have units behave like quantities. Also, it might then make sense to have `q1 % unit` return `0*q1.unit` so that `q1 % unit + (q1 // unit) * unit== q` will always hold. (If we go this route, it might make sense to let `q1 // q2` return a plain array rather than a dimensionless `Quantity`.)

Alternatively, as apparently is done by Julia, we could just leave it to the unit to have some `convert` function (a bit like the array API has `astype` for conversion of data types).

1. In fairness, I should point out that I suggested `// unit` a long time ago for astropy (https://github.com/astropy/astropy/pull/1412) and it was roundly rejected.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing the Quantity and Unit entry points and compare their proposed behavior with astropy's Quantity and Unit APIs. The issue presents several competing conversion designs but names no implementation files or tests; done would require an agreed conversion API and corresponding scope before coding can begin.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.