Skip to content

Calling quantify() on already-quantified data arrays #47

Closed
@lukelbd

Description

@lukelbd

Currently, if data_array.pint.quantify() is called on an object that is already quantified, a ValueError is always raised:

if isinstance(self.da.data, Quantity):
raise ValueError(
f"Cannot attach unit {units} to quantity: data "
f"already has units {self.da.data.units}"
)

IMHO it would be preferable to have the following behavior changes when calling quantify on an already-quantified data arrays, to make things more robust:

  1. If units is None and data_array has no 'units' attribute, do nothing; just return a shallow copy. It makes sense to me if quantify behaves like an identify operator on already-quantified arrays. Sort of like np.array(np.array([1])).
  2. If units is not None, but they match the existing pint.Quantity units, behave like step 1.
  3. If units is not None, but they do not match the existing units, replace the units and emit a warning (or raise an error...?).
  4. If data_array has a 'units' attribute, but they match the existing pint.Quantity units, behave like step 1.
  5. If data_array has a 'units' attribute, but they do not match the existing units, replace the units and emit a warning (or raise an error...?).

Also, the following behavior might be useful whether or not the data is already quantified:

  1. If both units is not None and data_array has a 'units' attribute, ignore the attribute after emitting a warning.

Any objections to these changes? If I (...eventually) submit a PR implementing them, would it be accepted?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions