UnitBase

class astropy.units.core.UnitBase [edit on github][source]

Bases: object

Abstract base class for units.

Most of the arithmetic operations on units are defined in this base class.

Should not be instantiated by users directly.

Attributes Summary

physical_type Return the physical type on the unit.

Methods Summary

is_dimensionless() Returns True if this unit translates into a scalar quantity without a unit.
get_converter(other[, equivalencies]) Return the conversion function to convert values from self to the specified unit.
to(other[, value, equivalencies]) Return the converted values in the specified unit.
simplify() Compresses a possibly composite unit down to a single instance.
is_equivalent(other[, equivalencies]) Returns True if this unit is equivalent to other.
find_equivalent_units([equivalencies]) Return a list of all the units that are the same type as the specified unit.
to_string([format]) Output the unit in the given format as a string.
decompose() Return a unit object composed of only irreducible units.
in_units(other[, value, equivalencies]) Alias for to for backward compatibility with pynbody.

Attributes Documentation

physical_type[source]

Return the physical type on the unit.

Examples

>>> u.m.physical_type
'length'

Methods Documentation

is_dimensionless() [edit on github][source]

Returns True if this unit translates into a scalar quantity without a unit.

Examples

>>> ((2 * u.m) / (3 * u.m)).is_dimensionless()
True
>>> (2 * u.m).is_dimensionless()
False
get_converter(other, equivalencies=[]) [edit on github][source]

Return the conversion function to convert values from self to the specified unit.

Parameters :

other : unit object or string

The unit to convert to.

equivalencies : list of equivalence pairs, optional

A list of equivalence pairs to try if the units are not directly convertible. See Equivalencies.

Returns :

func : callable

A callable that normally expects a single argument that is a scalar value or an array of values (or anything that may be converted to an array).

Raises :

UnitsException :

If units are inconsistent

to(other, value=1.0, equivalencies=[]) [edit on github][source]

Return the converted values in the specified unit.

Parameters :

other : unit object or string

The unit to convert to.

value : scalar int or float, or sequence that can be converted to array, optional

Value(s) in the current unit to be converted to the specified unit. If not provided, defaults to 1.0

equivalencies : list of equivalence pairs, optional

A list of equivalence pairs to try if the units are not directly convertible. See Equivalencies.

Returns :

values : scalar or array

Converted value(s). Input value sequences are returned as numpy arrays.

Raises :

UnitException :

If units are inconsistent

simplify() [edit on github][source]

Compresses a possibly composite unit down to a single instance.

is_equivalent(other, equivalencies=[]) [edit on github][source]

Returns True if this unit is equivalent to other.

Parameters :

other : unit object or string

The unit to convert to.

equivalencies : list of equivalence pairs, optional

A list of equivalence pairs to try if the units are not directly convertible. See Equivalencies.

Returns :

bool :

find_equivalent_units(equivalencies=[]) [edit on github][source]

Return a list of all the units that are the same type as the specified unit.

Parameters :

u : Unit instance or string

The Unit to find similar units to.

equivalencies : list of equivalence pairs, optional

A list of equivalence pairs to also list. See Equivalencies.

Returns :

units : list of UnitBase

A list of unit objects that match u. A subclass of list (EquivalentUnitsList) is returned that pretty-prints the list of units when output.

to_string(format=u'generic') [edit on github][source]

Output the unit in the given format as a string.

Parameters :

format : astropy.format.Base instance or str

The name of a format or a formatter object. If not provided, defaults to the generic format.

decompose() [edit on github][source]

Return a unit object composed of only irreducible units.

Parameters :

None :

Returns :

unit : CompositeUnit object

New object containing only irreducible unit objects.

in_units(other, value=1.0, equivalencies=[]) [edit on github][source]

Alias for to for backward compatibility with pynbody.

Page Contents