pycontrails.utils.types

Convienence types.

Module Attributes

DatetimeLike

Datetime like input (datetime, pd.Timestamp, np.datetime64)

Functions

apply_nan_mask_to_arraylike(arr, nan_mask)

Apply nan_mask to arr while maintaining the type.

type_guard(obj, type_[, error_message])

Shortcut utility to type guard a variable with custom error message.

type pycontrails.utils.types.DatetimeLike = datetime | Timestamp | datetime64 | str

Datetime like input (datetime, pd.Timestamp, np.datetime64)

pycontrails.utils.types.apply_nan_mask_to_arraylike[T: (ndarray, DataArray)](arr, nan_mask)

Apply nan_mask to arr while maintaining the type.

The parameter arr should have a float dtype.

This function is tested against DataArray, Series, and ndarray types.

Parameters:
Returns:

T – Array arr with values in nan_mask set to np.nan. The arr is mutated in place if it is a ndarray. For DataArray, a copy is returned.

Notes

When arr is a DataArray, this function keeps any attrs from arr in the returned instance.

pycontrails.utils.types.type_guard[T](obj, type_, error_message=None)

Shortcut utility to type guard a variable with custom error message.

Parameters:
  • obj (Any) – Any variable object

  • type_ (type[T] | tuple[type[T], ]) – Type of variable. Can be a tuple of types

  • error_message (str, optional) – Custom error message

Returns:

T – Returns the input object ensured to be type_

Raises:

ValueError – Raises ValueError if obj is not type_