From 92f671f6bb587fe09d26db0648492e9e9421f45a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20M=C3=BCller?= <mueller.seb@posteo.de> Date: Sat, 16 Nov 2024 16:47:31 +0100 Subject: [PATCH] data.tools: move 'filled' to mask tools --- src/finam/data/tools/__init__.py | 2 +- src/finam/data/tools/core.py | 35 -------------------------------- src/finam/data/tools/mask.py | 35 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/finam/data/tools/__init__.py b/src/finam/data/tools/__init__.py index 0acdc4e0..7a6ea7fd 100644 --- a/src/finam/data/tools/__init__.py +++ b/src/finam/data/tools/__init__.py @@ -3,7 +3,6 @@ from .core import ( assert_type, check, - filled, full, full_like, has_time_axis, @@ -15,6 +14,7 @@ from .info import Info from .mask import ( Mask, check_data_covers_domain, + filled, from_compressed, has_masked_values, is_masked_array, diff --git a/src/finam/data/tools/core.py b/src/finam/data/tools/core.py index 6f7f5730..fc397374 100644 --- a/src/finam/data/tools/core.py +++ b/src/finam/data/tools/core.py @@ -337,41 +337,6 @@ def _check_shape(shape, grid): ) -def filled(data, fill_value=None): - """ - Return input as an array with masked data replaced by a fill value. - - This routine respects quantified and un-quantified data. - - Parameters - ---------- - data : :class:`pint.Quantity` or :class:`numpy.ndarray` or :class:`numpy.ma.MaskedArray` - The reference object input. - fill_value : array_like, optional - The value to use for invalid entries. Can be scalar or non-scalar. - If non-scalar, the resulting ndarray must be broadcastable over - input array. Default is None, in which case, the `fill_value` - attribute of the array is used instead. - - Returns - ------- - pint.Quantity or numpy.ndarray - New object with the same shape and type as other, - with the data filled with fill_value. - Units will be taken from the input if present. - - See also - -------- - :func:`numpy.ma.filled`: - Numpy routine doing the same. - """ - if not is_masked_array(data): - return data - if is_quantified(data): - return UNITS.Quantity(data.magnitude.filled(fill_value), data.units) - return data.filled(fill_value) - - def assert_type(cls, slot, obj, types): """Type assertion.""" for t in types: diff --git a/src/finam/data/tools/mask.py b/src/finam/data/tools/mask.py index 666260f0..444776b4 100644 --- a/src/finam/data/tools/mask.py +++ b/src/finam/data/tools/mask.py @@ -55,6 +55,41 @@ def has_masked_values(data): return np.ma.is_masked(data) +def filled(data, fill_value=None): + """ + Return input as an array with masked data replaced by a fill value. + + This routine respects quantified and un-quantified data. + + Parameters + ---------- + data : :class:`pint.Quantity` or :class:`numpy.ndarray` or :class:`numpy.ma.MaskedArray` + The reference object input. + fill_value : array_like, optional + The value to use for invalid entries. Can be scalar or non-scalar. + If non-scalar, the resulting ndarray must be broadcastable over + input array. Default is None, in which case, the `fill_value` + attribute of the array is used instead. + + Returns + ------- + pint.Quantity or numpy.ndarray + New object with the same shape and type as other, + with the data filled with fill_value. + Units will be taken from the input if present. + + See also + -------- + :func:`numpy.ma.filled`: + Numpy routine doing the same. + """ + if not is_masked_array(data): + return data + if is_quantified(data): + return UNITS.Quantity(data.magnitude.filled(fill_value), data.units) + return data.filled(fill_value) + + def to_masked(data, **kwargs): """ Return a masked version of the data. -- GitLab