From 14ed86236d742006a7f0bd23736c289ce5cb0f8c Mon Sep 17 00:00:00 2001 From: Keewis Date: Tue, 3 Aug 2021 10:57:28 +0200 Subject: [PATCH 1/4] add a custom `repr` to the context class --- xarray/core/merge.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xarray/core/merge.py b/xarray/core/merge.py index db5b95fd415..a054e414ff3 100644 --- a/xarray/core/merge.py +++ b/xarray/core/merge.py @@ -63,6 +63,9 @@ class Context: def __init__(self, func): self.func = func + def __repr__(self): + return f"Context('{self.func}')" + def broadcast_dimension_size(variables: List[Variable]) -> Dict[Hashable, int]: """Extract dimension sizes from a dictionary of variables. From 7d3e3335b32fe1fabcc241b790ae790c68227619 Mon Sep 17 00:00:00 2001 From: Keewis Date: Tue, 3 Aug 2021 11:20:40 +0200 Subject: [PATCH 2/4] allow to pass a function to the `keep_attrs` option --- xarray/core/options.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 7104e12c29f..093cb2307f6 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -52,7 +52,7 @@ def _positive_integer(value): DISPLAY_EXPAND_DATA: lambda choice: choice in [True, False, "default"], ENABLE_CFTIMEINDEX: lambda value: isinstance(value, bool), FILE_CACHE_MAXSIZE: _positive_integer, - KEEP_ATTRS: lambda choice: choice in [True, False, "default"], + KEEP_ATTRS: lambda choice: choice in [True, False, "default"] or callable(choice), WARN_FOR_UNCLOSED_FILES: lambda value: isinstance(value, bool), } @@ -82,11 +82,13 @@ def _get_boolean_with_default(option, default): if global_choice == "default": return default - elif global_choice in [True, False]: + elif global_choice in [True, False] or callable(global_choice): + return global_choice + elif callable(global_choice): return global_choice else: raise ValueError( - f"The global option {option} must be one of True, False or 'default'." + f"The global option {option} must be one of True, False or 'default' or a callable." ) From 8fedd8433c242e53d5978b8639851aba493f296c Mon Sep 17 00:00:00 2001 From: Keewis Date: Tue, 3 Aug 2021 11:28:08 +0200 Subject: [PATCH 3/4] add the context object to the reduce methods --- xarray/core/variable.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index f69951580c7..be5e97bc613 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -1740,6 +1740,8 @@ def reduce( Array with summarized data and the indicated dimension(s) removed. """ + from .merge import Context, merge_attrs + if dim == ...: dim = None if dim is not None and axis is not None: @@ -1782,7 +1784,13 @@ def reduce( if keep_attrs is None: keep_attrs = _get_keep_attrs(default=False) - attrs = self._attrs if keep_attrs else None + + if isinstance(keep_attrs, bool): + keep_attrs = "override" if keep_attrs else "drop" + + attrs = merge_attrs( + [self._attrs], combine_attrs=keep_attrs, context=Context(func.__name__) + ) return Variable(dims, data, attrs=attrs) From 8d23032ecf20545cd320cfb552d8febef73cd69c Mon Sep 17 00:00:00 2001 From: Keewis Date: Tue, 3 Aug 2021 14:46:46 +0200 Subject: [PATCH 4/4] use `.attrs` to avoid passing `None` --- xarray/core/variable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index be5e97bc613..86450d19af5 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -1789,7 +1789,7 @@ def reduce( keep_attrs = "override" if keep_attrs else "drop" attrs = merge_attrs( - [self._attrs], combine_attrs=keep_attrs, context=Context(func.__name__) + [self.attrs], combine_attrs=keep_attrs, context=Context(func.__name__) ) return Variable(dims, data, attrs=attrs)