-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathaccessors.py
1724 lines (1435 loc) · 54.9 KB
/
accessors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# TODO is it possible to import pint-xarray from within xarray if pint is present?
import itertools
import pint
from pint import Unit
from xarray import register_dataarray_accessor, register_dataset_accessor
from xarray.core.dtypes import NA
from . import conversion
from .conversion import no_unit_values
from .errors import format_error_message
_default = object()
def setup_registry(registry):
"""set up the given registry for use with pint_xarray
Namely, it enables ``force_ndarray_like`` to make sure results are always
duck arrays.
Parameters
----------
registry : pint.UnitRegistry
The registry to modify
"""
if not registry.force_ndarray and not registry.force_ndarray_like:
registry.force_ndarray_like = True
return registry
default_registry = setup_registry(pint.get_application_registry())
# TODO could/should we overwrite xr.open_dataset and xr.open_mfdataset to make
# them apply units upon loading???
# TODO could even override the decode_cf kwarg?
# TODO docstrings
# TODO type hints
def is_dict_like(obj):
return hasattr(obj, "keys") and hasattr(obj, "__getitem__")
def zip_mappings(*mappings, fill_value=None):
"""zip mappings by combining values for common keys into a tuple
Works like itertools.zip_longest, so if a key is missing from a
mapping, it is replaced by ``fill_value``.
Parameters
----------
*mappings : dict-like
The mappings to zip
fill_value
The value to use if a key is missing from a mapping.
Returns
-------
zipped : dict-like
The zipped mapping
"""
keys = set(itertools.chain.from_iterable(mapping.keys() for mapping in mappings))
# TODO: could this be made more efficient using itertools.groupby?
zipped = {
key: tuple(mapping.get(key, fill_value) for mapping in mappings) for key in keys
}
return zipped
def units_to_str_or_none(mapping, unit_format):
formatter = str if not unit_format else lambda v: unit_format.format(v)
return {
key: formatter(value) if isinstance(value, Unit) else value
for key, value in mapping.items()
}
# based on xarray.core.utils.either_dict_or_kwargs
# https://github.com/pydata/xarray/blob/v0.15.1/xarray/core/utils.py#L249-L268
def either_dict_or_kwargs(positional, keywords, method_name):
if positional not in (_default, None):
if not is_dict_like(positional):
raise ValueError(
f"the first argument to .{method_name} must be a dictionary"
)
if keywords:
raise ValueError(
"cannot specify both keyword and positional "
f"arguments to .{method_name}"
)
return positional
else:
return keywords
def get_registry(unit_registry, new_units, existing_units):
units = itertools.chain(new_units.values(), existing_units.values())
registries = {unit._REGISTRY for unit in units if isinstance(unit, Unit)}
if unit_registry is None:
if not registries:
unit_registry = default_registry
elif len(registries) == 1:
(unit_registry,) = registries
registries.add(unit_registry)
if len(registries) > 1 or unit_registry not in registries:
raise ValueError(
"using multiple unit registries in the same object is not supported"
)
if not unit_registry.force_ndarray_like and not unit_registry.force_ndarray:
raise ValueError(
"invalid registry. Please enable 'force_ndarray_like' or 'force_ndarray'."
)
return unit_registry
def _decide_units(units, registry, unit_attribute):
if units is _default and unit_attribute in (None, _default):
# or warn and return None?
raise ValueError("no units given")
elif units in no_unit_values or isinstance(units, Unit):
# TODO what happens if they pass in a Unit from a different registry
return units
elif units is _default:
if unit_attribute in no_unit_values:
return unit_attribute
if isinstance(unit_attribute, Unit):
units = unit_attribute
else:
units = registry.parse_units(unit_attribute)
else:
units = registry.parse_units(units)
return units
class DatasetLocIndexer:
__slots__ = ("ds",)
def __init__(self, ds):
self.ds = ds
def __getitem__(self, indexers):
if not is_dict_like(indexers):
raise NotImplementedError("pandas-style indexing is not supported, yet")
dims = self.ds.dims
indexer_units = {
name: conversion.extract_indexer_units(indexer)
for name, indexer in indexers.items()
if name in dims
}
# convert the indexes to the indexer's units
try:
converted = conversion.convert_units(self.ds, indexer_units)
except ValueError as e:
raise KeyError(*e.args) from e
# index
stripped_indexers = {
name: conversion.strip_indexer_units(indexer)
for name, indexer in indexers.items()
}
return converted.loc[stripped_indexers]
class DataArrayLocIndexer:
__slots__ = ("da",)
def __init__(self, da):
self.da = da
def __getitem__(self, indexers):
if not is_dict_like(indexers):
raise NotImplementedError("pandas-style indexing is not supported, yet")
dims = self.da.dims
indexer_units = {
name: conversion.extract_indexer_units(indexer)
for name, indexer in indexers.items()
if name in dims
}
# convert the indexes to the indexer's units
try:
converted = conversion.convert_units(self.da, indexer_units)
except ValueError as e:
raise KeyError(*e.args) from e
# index
stripped_indexers = {
name: conversion.strip_indexer_units(indexer)
for name, indexer in indexers.items()
}
return converted.loc[stripped_indexers]
def __setitem__(self, indexers, values):
if not is_dict_like(indexers):
raise NotImplementedError("pandas-style indexing is not supported, yet")
dims = self.da.dims
unit_attrs = conversion.extract_unit_attributes(self.da)
index_units = {
name: units for name, units in unit_attrs.items() if name in dims
}
# convert the indexers to the index units
try:
converted = conversion.convert_indexer_units(indexers, index_units)
except ValueError as e:
raise KeyError(*e.args) from e
# index
stripped_indexers = {
name: conversion.strip_indexer_units(indexer)
for name, indexer in converted.items()
}
self.da.loc[stripped_indexers] = values
@register_dataarray_accessor("pint")
class PintDataArrayAccessor:
"""
Access methods for DataArrays with units using Pint.
Methods and attributes can be accessed through the `.pint` attribute.
"""
def __init__(self, da):
self.da = da
def quantify(self, units=_default, unit_registry=None, **unit_kwargs):
"""
Attach units to the DataArray.
Units can be specified as a pint.Unit or as a string, which will be
parsed by the given unit registry. If no units are specified then the
units will be parsed from the `'units'` entry of the DataArray's
`.attrs`. Will raise a ValueError if the DataArray already contains a
unit-aware array with a different unit.
.. note::
Be aware that unless you're using ``dask`` this will load
the data into memory. To avoid that, consider converting
to ``dask`` first (e.g. using ``chunk``).
.. warning::
As units in dimension coordinates are not supported until
``xarray`` changes the way it implements indexes, these
units will be set as attributes.
Parameters
----------
units : unit-like or mapping of hashable to unit-like, optional
Physical units to use for this DataArray. If a str or
pint.Unit, will be used as the DataArray's units. If a
dict-like, it should map a variable name to the desired
unit (use the DataArray's name to refer to its data). If
not provided, ``quantify`` will try to read them from
``DataArray.attrs['units']`` using pint's parser. The
``"units"`` attribute will be removed from all variables
except from dimension coordinates.
unit_registry : pint.UnitRegistry, optional
Unit registry to be used for the units attached to this DataArray.
If not given then a default registry will be created.
**unit_kwargs
Keyword argument form of units.
Returns
-------
quantified : DataArray
DataArray whose wrapped array data will now be a Quantity
array with the specified units.
Notes
-----
``"none"`` and ``None`` can be used to mark variables that should not
be quantified.
Examples
--------
>>> da = xr.DataArray(
... data=[0.4, 0.9, 1.7, 4.8, 3.2, 9.1],
... dims=["wavelength"],
... coords={"wavelength": [1e-4, 2e-4, 4e-4, 6e-4, 1e-3, 2e-3]},
... )
>>> da.pint.quantify(units="Hz")
<xarray.DataArray (wavelength: 6)>
<Quantity([0.4 0.9 1.7 4.8 3.2 9.1], 'hertz')>
Coordinates:
* wavelength (wavelength) float64 0.0001 0.0002 0.0004 0.0006 0.001 0.002
Don't quantify the data:
>>> da = xr.DataArray(
... data=[0.4, 0.9],
... dims=["wavelength"],
... attrs={"units": "Hz"},
... )
>>> da.pint.quantify(units=None)
<xarray.DataArray (wavelength: 2)>
array([0.4, 0.9])
Dimensions without coordinates: wavelength
Quantify with the same unit:
>>> q = da.pint.quantify()
>>> q
<xarray.DataArray (wavelength: 2)>
<Quantity([0.4 0.9], 'hertz')>
Dimensions without coordinates: wavelength
>>> q.pint.quantify("Hz")
<xarray.DataArray (wavelength: 2)>
<Quantity([0.4 0.9], 'hertz')>
Dimensions without coordinates: wavelength
"""
if units is None or isinstance(units, (str, pint.Unit)):
if self.da.name in unit_kwargs:
raise ValueError(
f"ambiguous values given for {repr(self.da.name)}:"
f" {repr(units)} and {repr(unit_kwargs[self.da.name])}"
)
unit_kwargs[self.da.name] = units
units = None
units = either_dict_or_kwargs(units, unit_kwargs, "quantify")
registry = get_registry(unit_registry, units, conversion.extract_units(self.da))
unit_attrs = conversion.extract_unit_attributes(self.da)
possible_new_units = zip_mappings(units, unit_attrs, fill_value=_default)
new_units = {}
invalid_units = {}
for name, (unit, attr) in possible_new_units.items():
if unit not in (_default, None) or attr not in (_default, None):
try:
new_units[name] = _decide_units(unit, registry, attr)
except (ValueError, pint.UndefinedUnitError) as e:
if unit not in (_default, None):
type = "parameter"
reported_unit = unit
else:
type = "attribute"
reported_unit = attr
invalid_units[name] = (reported_unit, type, e)
if invalid_units:
raise ValueError(format_error_message(invalid_units, "parse"))
existing_units = {
name: unit
for name, unit in conversion.extract_units(self.da).items()
if isinstance(unit, Unit)
}
overwritten_units = {
name: (old, new)
for name, (old, new) in zip_mappings(
existing_units, new_units, fill_value=_default
).items()
if old is not _default and new is not _default and old != new
}
if overwritten_units:
errors = {
name: (
new,
ValueError(
f"Cannot attach unit {repr(new)} to quantity: data "
f"already has units {repr(old)}"
),
)
for name, (old, new) in overwritten_units.items()
}
raise ValueError(format_error_message(errors, "attach"))
return self.da.pipe(conversion.strip_unit_attributes).pipe(
conversion.attach_units, new_units
)
def dequantify(self, format=None):
r"""
Convert the units of the DataArray to string attributes.
Will replace ``.attrs['units']`` on each variable with a string
representation of the ``pint.Unit`` instance.
Parameters
----------
format : str, default: None
The format specification (as accepted by pint) used for the string
representations. If ``None``, the registry's default
(:py:attr:`pint.UnitRegistry.default_format`) is used instead.
Returns
-------
dequantified : DataArray
DataArray whose array data is unitless, and of the type
that was previously wrapped by `pint.Quantity`.
See Also
--------
:doc:`pint:user/formatting`
pint's string formatting guide
Examples
--------
>>> da = xr.DataArray([0, 1], dims="x")
>>> q = da.pint.quantify("m / s")
>>> q
<xarray.DataArray (x: 2)>
<Quantity([0 1], 'meter / second')>
Dimensions without coordinates: x
>>> q.pint.dequantify(format="P")
<xarray.DataArray (x: 2)>
array([0, 1])
Dimensions without coordinates: x
Attributes:
units: meter/second
>>> q.pint.dequantify(format="~P")
<xarray.DataArray (x: 2)>
array([0, 1])
Dimensions without coordinates: x
Attributes:
units: m/s
Use the registry's default format
>>> pint_xarray.unit_registry.default_format = "~L"
>>> q.pint.dequantify()
<xarray.DataArray (x: 2)>
array([0, 1])
Dimensions without coordinates: x
Attributes:
units: \frac{\mathrm{m}}{\mathrm{s}}
"""
units = conversion.extract_unit_attributes(self.da)
units.update(conversion.extract_units(self.da))
unit_format = f"{{:{format}}}" if isinstance(format, str) else format
units = units_to_str_or_none(units, unit_format)
return (
self.da.pipe(conversion.strip_units)
.pipe(conversion.strip_unit_attributes)
.pipe(conversion.attach_unit_attributes, units)
)
@property
def magnitude(self):
"""the magnitude of the data or the data itself if not a quantity."""
data = self.da.data
return getattr(data, "magnitude", data)
@property
def units(self):
"""the units of the data or :py:obj:`None` if not a quantity.
Setting the units is possible, but only if the data is not already a quantity.
"""
return getattr(self.da.data, "units", None)
@units.setter
def units(self, units):
self.da.data = conversion.array_attach_units(self.da.data, units)
@property
def dimensionality(self):
"""get the dimensionality of the data or :py:obj:`None` if not a quantity."""
return getattr(self.da.data, "dimensionality", None)
@property
def registry(self):
# TODO is this a bad idea? (see GH issue #1071 in pint)
return getattr(self.da.data, "_REGISTRY", None)
@registry.setter
def registry(self, _):
raise AttributeError("Don't try to change the registry once created")
def to(self, units=None, **unit_kwargs):
"""convert the quantities in a DataArray
Parameters
----------
units : unit-like or mapping of hashable to unit-like, optional
The units to convert to. If a unit name or ``pint.Unit``
object, convert the DataArray's data. If a dict-like, it
has to map a variable name to a unit name or ``pint.Unit``
object.
**unit_kwargs
The kwargs form of ``units``. Can only be used for
variable names that are strings and valid python identifiers.
Returns
-------
object : DataArray
A new object with converted units.
Examples
--------
>>> da = xr.DataArray(
... data=np.linspace(0, 1, 5) * ureg.m,
... coords={"u": ("x", np.arange(5) * ureg.s)},
... dims="x",
... name="arr",
... )
>>> da
<xarray.DataArray 'arr' (x: 5)>
<Quantity([0. 0.25 0.5 0.75 1. ], 'meter')>
Coordinates:
u (x) int64 [s] 0 1 2 3 4
Dimensions without coordinates: x
Convert the data
>>> da.pint.to("mm")
<xarray.DataArray 'arr' (x: 5)>
<Quantity([ 0. 250. 500. 750. 1000.], 'millimeter')>
Coordinates:
u (x) int64 [s] 0 1 2 3 4
Dimensions without coordinates: x
>>> da.pint.to(ureg.mm)
<xarray.DataArray 'arr' (x: 5)>
<Quantity([ 0. 250. 500. 750. 1000.], 'millimeter')>
Coordinates:
u (x) int64 [s] 0 1 2 3 4
Dimensions without coordinates: x
>>> da.pint.to({da.name: "mm"})
<xarray.DataArray 'arr' (x: 5)>
<Quantity([ 0. 250. 500. 750. 1000.], 'millimeter')>
Coordinates:
u (x) int64 [s] 0 1 2 3 4
Dimensions without coordinates: x
Convert coordinates
>>> da.pint.to({"u": ureg.ms})
<xarray.DataArray 'arr' (x: 5)>
<Quantity([0. 0.25 0.5 0.75 1. ], 'meter')>
Coordinates:
u (x) float64 [ms] 0.0 1e+03 2e+03 3e+03 4e+03
Dimensions without coordinates: x
>>> da.pint.to(u="ms")
<xarray.DataArray 'arr' (x: 5)>
<Quantity([0. 0.25 0.5 0.75 1. ], 'meter')>
Coordinates:
u (x) float64 [ms] 0.0 1e+03 2e+03 3e+03 4e+03
Dimensions without coordinates: x
Convert both simultaneously
>>> da.pint.to("mm", u="ms")
<xarray.DataArray 'arr' (x: 5)>
<Quantity([ 0. 250. 500. 750. 1000.], 'millimeter')>
Coordinates:
u (x) float64 [ms] 0.0 1e+03 2e+03 3e+03 4e+03
Dimensions without coordinates: x
>>> da.pint.to({"arr": ureg.mm, "u": ureg.ms})
<xarray.DataArray 'arr' (x: 5)>
<Quantity([ 0. 250. 500. 750. 1000.], 'millimeter')>
Coordinates:
u (x) float64 [ms] 0.0 1e+03 2e+03 3e+03 4e+03
Dimensions without coordinates: x
>>> da.pint.to(arr="mm", u="ms")
<xarray.DataArray 'arr' (x: 5)>
<Quantity([ 0. 250. 500. 750. 1000.], 'millimeter')>
Coordinates:
u (x) float64 [ms] 0.0 1e+03 2e+03 3e+03 4e+03
Dimensions without coordinates: x
"""
if isinstance(units, (str, pint.Unit)):
unit_kwargs[self.da.name] = units
units = None
elif units is not None and not is_dict_like(units):
raise ValueError(
"units must be either a string, a pint.Unit object or a dict-like,"
f" but got {units!r}"
)
units = either_dict_or_kwargs(units, unit_kwargs, "to")
return conversion.convert_units(self.da, units)
def chunk(self, chunks, name_prefix="xarray-", token=None, lock=False):
"""unit-aware version of chunk
Like :py:meth:`xarray.DataArray.chunk`, but chunking a quantity will change the
wrapped type to ``dask``.
.. note::
It is recommended to only use this when chunking in-memory arrays. To
rechunk please use :py:meth:`xarray.DataArray.chunk`.
See Also
--------
xarray.DataArray.chunk
xarray.Dataset.pint.chunk
"""
units = conversion.extract_units(self.da)
stripped = conversion.strip_units(self.da)
chunked = stripped.chunk(
chunks, name_prefix=name_prefix, token=token, lock=lock
)
return conversion.attach_units(chunked, units)
def reindex(
self,
indexers=None,
method=None,
tolerance=None,
copy=True,
fill_value=NA,
**indexers_kwargs,
):
"""unit-aware version of reindex
Like :py:meth:`xarray.DataArray.reindex`, except the object's indexes are
converted to the units of the indexers first.
.. note::
``tolerance`` and ``fill_value`` are not supported, yet. They will be passed
through to ``DataArray.reindex`` unmodified.
See Also
--------
xarray.Dataset.pint.reindex
xarray.DataArray.pint.reindex_like
xarray.DataArray.reindex
"""
indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "reindex")
dims = self.da.dims
indexer_units = {
name: conversion.extract_indexer_units(indexer)
for name, indexer in indexers.items()
if name in dims
}
# TODO: handle tolerance
# TODO: handle fill_value
# convert the indexes to the indexer's units
converted = conversion.convert_units(self.da, indexer_units)
# index
stripped_indexers = {
name: conversion.strip_indexer_units(indexer)
for name, indexer in indexers.items()
}
indexed = converted.reindex(
stripped_indexers,
method=method,
tolerance=tolerance,
copy=copy,
fill_value=fill_value,
)
return indexed
def reindex_like(
self, other, method=None, tolerance=None, copy=True, fill_value=NA
):
"""unit-aware version of reindex_like
Like :py:meth:`xarray.DataArray.reindex_like`, except the object's indexes
are converted to the units of the indexers first.
.. note::
``tolerance`` and ``fill_value`` are not supported, yet. They will be passed
through to ``DataArray.reindex_like`` unmodified.
See Also
--------
xarray.Dataset.pint.reindex_like
xarray.DataArray.pint.reindex
xarray.DataArray.reindex_like
"""
indexer_units = conversion.extract_unit_attributes(other)
# TODO: handle tolerance
# TODO: handle fill_value
converted = conversion.convert_units(self.da, indexer_units)
return converted.reindex_like(
other,
method=method,
tolerance=tolerance,
copy=copy,
fill_value=fill_value,
)
def interp(
self,
coords=None,
method="linear",
assume_sorted=False,
kwargs=None,
**coords_kwargs,
):
"""unit-aware version of interp
Like :py:meth:`xarray.DataArray.interp`, except the object's indexes are
converted to the units of the indexers first.
.. note::
``kwargs`` is passed unmodified to ``DataArray.interp``
See Also
--------
xarray.Dataset.pint.interp
xarray.DataArray.pint.interp_like
xarray.DataArray.interp
"""
indexers = either_dict_or_kwargs(coords, coords_kwargs, "interp")
dims = self.da.dims
indexer_units = {
name: conversion.extract_indexer_units(indexer)
for name, indexer in indexers.items()
if name in dims
}
# convert the indexes to the indexer's units
converted = conversion.convert_units(self.da, indexer_units)
units = conversion.extract_units(converted)
stripped = conversion.strip_units(converted)
# index
stripped_indexers = {
name: conversion.strip_indexer_units(indexer)
for name, indexer in indexers.items()
}
interpolated = stripped.interp(
stripped_indexers,
method=method,
assume_sorted=False,
kwargs=None,
)
return conversion.attach_units(interpolated, units)
def interp_like(self, other, method="linear", assume_sorted=False, kwargs=None):
"""unit-aware version of interp_like
Like :py:meth:`xarray.DataArray.interp_like`, except the object's indexes are converted
to the units of the indexers first.
.. note::
``kwargs`` is passed unmodified to ``DataArray.interp``
See Also
--------
xarray.Dataset.pint.interp_like
xarray.DataArray.pint.interp
xarray.DataArray.interp_like
"""
indexer_units = conversion.extract_unit_attributes(other)
converted = conversion.convert_units(self.da, indexer_units)
units = conversion.extract_units(converted)
stripped = conversion.strip_units(converted)
interpolated = stripped.interp_like(
other,
method=method,
assume_sorted=assume_sorted,
kwargs=kwargs,
)
return conversion.attach_units(interpolated, units)
def sel(
self, indexers=None, method=None, tolerance=None, drop=False, **indexers_kwargs
):
"""unit-aware version of sel
Like :py:meth:`xarray.DataArray.sel`, except the object's indexes are converted
to the units of the indexers first.
.. note::
``tolerance`` is not supported, yet. It will be passed through to
``DataArray.sel`` unmodified.
See Also
--------
xarray.Dataset.pint.sel
xarray.DataArray.sel
xarray.Dataset.sel
"""
indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "sel")
dims = self.da.dims
indexer_units = {
name: conversion.extract_indexer_units(indexer)
for name, indexer in indexers.items()
if name in dims
}
# TODO: handle tolerance
# convert the indexes to the indexer's units
try:
converted = conversion.convert_units(self.da, indexer_units)
except ValueError as e:
raise KeyError(*e.args) from e
# index
stripped_indexers = {
name: conversion.strip_indexer_units(indexer)
for name, indexer in indexers.items()
}
indexed = converted.sel(
stripped_indexers,
method=method,
tolerance=tolerance,
drop=drop,
)
return indexed
@property
def loc(self):
"""Unit-aware attribute for indexing
.. note::
Position based indexing (e.g. ``ds.loc[1, 2:]``) is not supported, yet
See Also
--------
xarray.DataArray.loc
"""
return DataArrayLocIndexer(self.da)
def drop_sel(self, labels=None, *, errors="raise", **labels_kwargs):
"""unit-aware version of drop_sel
Just like :py:meth:`xarray.DataArray.drop_sel`, except the indexers are converted
to the units of the object's indexes first.
See Also
--------
xarray.Dataset.pint.drop_sel
xarray.DataArray.drop_sel
xarray.Dataset.drop_sel
"""
indexers = either_dict_or_kwargs(labels, labels_kwargs, "drop_sel")
dims = self.da.dims
unit_attrs = conversion.extract_unit_attributes(self.da)
index_units = {
name: units for name, units in unit_attrs.items() if name in dims
}
# convert the indexers to the indexes units
try:
converted_indexers = conversion.convert_indexer_units(indexers, index_units)
except ValueError as e:
raise KeyError(*e.args) from e
# index
stripped_indexers = {
name: conversion.strip_indexer_units(indexer)
for name, indexer in converted_indexers.items()
}
indexed = self.da.drop_sel(
stripped_indexers,
errors=errors,
)
return indexed
def ffill(self, dim, limit=None):
"""unit-aware version of ffill
Like :py:meth:`xarray.DataArray.ffill` but without stripping the data units.
See Also
--------
xarray.DataArray.ffill
xarray.DataArray.pint.bfill
"""
units = conversion.extract_units(self.da)
stripped = conversion.strip_units(self.da)
filled = stripped.ffill(dim=dim, limit=limit)
return conversion.attach_units(filled, units)
def bfill(self, dim, limit=None):
"""unit-aware version of bfill
Like :py:meth:`xarray.DataArray.bfill` but without stripping the data units.
See Also
--------
xarray.DataArray.bfill
xarray.DataArray.pint.ffill
"""
units = conversion.extract_units(self.da)
stripped = conversion.strip_units(self.da)
filled = stripped.bfill(dim=dim, limit=limit)
return conversion.attach_units(filled, units)
def interpolate_na(
self,
dim=None,
method="linear",
limit=None,
use_coordinate=True,
max_gap=None,
keep_attrs=None,
**kwargs,
):
"""unit-aware version of interpolate_na
Like :py:meth:`xarray.DataArray.interpolate_na` but without stripping the units
on data or coordinates.
.. note::
``max_gap`` is not supported, yet, and will be passed through to
``DataArray.interpolate_na`` unmodified.
See Also
--------
xarray.Dataset.pint.interpolate_na
xarray.DataArray.interpolate_na
"""
units = conversion.extract_units(self.da)
stripped = conversion.strip_units(self.da)
interpolated = stripped.interpolate_na(
dim=dim,
method=method,
limit=limit,
use_coordinate=use_coordinate,
max_gap=max_gap,
keep_attrs=keep_attrs,
**kwargs,
)
return conversion.attach_units(interpolated, units)
@register_dataset_accessor("pint")
class PintDatasetAccessor:
"""
Access methods for DataArrays with units using Pint.
Methods and attributes can be accessed through the `.pint` attribute.
"""
def __init__(self, ds):
self.ds = ds
def quantify(self, units=_default, unit_registry=None, **unit_kwargs):
"""
Attach units to the variables of the Dataset.
Units can be specified as a ``pint.Unit`` or as a
string, which will be parsed by the given unit registry. If no
units are specified then the units will be parsed from the
``"units"`` entry of the Dataset variable's ``.attrs``. Will
raise a ValueError if any of the variables already contain a
unit-aware array with a different unit.
.. note::
Be aware that unless you're using ``dask`` this will load
the data into memory. To avoid that, consider converting
to ``dask`` first (e.g. using ``chunk``).
.. warning::
As units in dimension coordinates are not supported until
``xarray`` changes the way it implements indexes, these
units will be set as attributes.
Parameters
----------
units : mapping of hashable to unit-like, optional
Physical units to use for particular DataArrays in this
Dataset. It should map variable names to units (unit names
or ``pint.Unit`` objects). If not provided, ``quantify``
will try to read them from ``Dataset[var].attrs['units']``
using pint's parser. The ``"units"`` attribute will be
removed from all variables except from dimension coordinates.
unit_registry : pint.UnitRegistry, optional
Unit registry to be used for the units attached to each
DataArray in this Dataset. If not given then a default
registry will be created.
**unit_kwargs