Skip to content

Commit 5cc7342

Browse files
committed
dev
1 parent c99ba6a commit 5cc7342

File tree

6 files changed

+319
-48
lines changed

6 files changed

+319
-48
lines changed

cf/data/data.py

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5653,11 +5653,19 @@ def change_calendar(self, calendar, inplace=False, i=False):
56535653
def override_units(self, units, inplace=False, i=False):
56545654
"""Override the data array units.
56555655
5656-
Not to be confused with setting the `Units` attribute to units
5657-
which are equivalent to the original units. This is different
5658-
because in this case the new units need not be equivalent to the
5659-
original ones and the data array elements will not be changed to
5660-
reflect the new units.
5656+
The new units need not be equivalent to the original ones, and
5657+
the data array elements will not be changed to reflect the new
5658+
units. Therefore, this method should only be used when it is
5659+
known that the data array values are correct but the units
5660+
have incorrectly encoded.
5661+
5662+
Not to be confused with changing to equivalent units with the
5663+
`to_units` method or the `Units`, `units`, or `calendar`
5664+
attributes. These approaches also convert the data to have the
5665+
new units.
5666+
5667+
.. seealso:: `override_calendar`, `to_units`, `Units`,
5668+
`units`, `calendar`
56615669
56625670
:Parameters:
56635671
@@ -5695,13 +5703,21 @@ def override_units(self, units, inplace=False, i=False):
56955703
@_deprecated_kwarg_check("i", version="3.0.0", removed_at="4.0.0")
56965704
@_inplace_enabled(default=False)
56975705
def override_calendar(self, calendar, inplace=False, i=False):
5698-
"""Override the calendar of the data array elements.
5706+
"""Override the calendar of date-time units.
5707+
5708+
The new calendar need not be equivalent to the original one,
5709+
and the data array elements will not be changed to reflect the
5710+
new calendar. Therefore, this method should only be used when
5711+
it is known that the data array values are correct but the
5712+
calendar has been incorrectly encoded.
5713+
5714+
Not to be confused with changing to equivalent units with the
5715+
`to_units` method or the `Units`, `units`, or `calendar`
5716+
attributes. These approaches also convert the data to have the
5717+
new units.
56995718
5700-
Not to be confused with using the `change_calendar` method or
5701-
setting the `d.Units.calendar`. `override_calendar` is different
5702-
because the new calendar need not be equivalent to the original
5703-
ones and the data array elements will not be changed to reflect
5704-
the new units.
5719+
.. seealso:: `override_units`, `to_units`, `Units`, `units`,
5720+
`calendar`
57055721
57065722
:Parameters:
57075723
@@ -7365,6 +7381,57 @@ def to_memory(self):
73657381
"Consider using 'Data.persist' instead."
73667382
)
73677383

7384+
@_inplace_enabled(default=False)
7385+
def to_units(self, units, inplace=False):
7386+
"""Change the data array units.
7387+
7388+
Changing the units will causes the data values to be changed
7389+
to match the new units, so the new units must be equivalent to
7390+
the existing ones.
7391+
7392+
Not to be confused with overriding the units with
7393+
`override_units`
7394+
7395+
.. versionadded:: NEXTVERSION
7396+
7397+
.. seealso:: `override_units`, `override_calendar`, `Units`,
7398+
`units`, `calendar`
7399+
7400+
:Parameters:
7401+
7402+
units: `str` or `Units`
7403+
The new units for the data array.
7404+
7405+
{{inplace: `bool`, optional}}
7406+
7407+
:Returns:
7408+
7409+
`Data` or `None`
7410+
The new data, or `None` if the operation was in-place.
7411+
7412+
**Examples**
7413+
7414+
>>> d = cf.Data([1, 2], 'km')
7415+
>>> e = d.to_units('metre')
7416+
>>> print(e.Units)
7417+
'metre'
7418+
>>> print(e.array)
7419+
[1000. 2000.]
7420+
>>> e.to_units('miles', inplace=True)
7421+
>>> print(e.Units)
7422+
'miles'
7423+
>>> print(e.array)
7424+
[0.62137119 1.24274238]
7425+
>>> e.to_units('degC')
7426+
Traceback (most recent call last)
7427+
...
7428+
ValueError: Can't set Units to <Units: degC> that are not equivalent to the current units <Units: miles>. Consider using the override_units method instead.
7429+
7430+
"""
7431+
d = _inplace_enabled_define_and_cleanup(self)
7432+
d.Units = self._Units_class(units)
7433+
return d
7434+
73687435
@_deprecated_kwarg_check("i", version="3.0.0", removed_at="4.0.0")
73697436
@_inplace_enabled(default=False)
73707437
def trunc(self, inplace=False, i=False):

cf/mixin/propertiesdata.py

Lines changed: 92 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4431,6 +4431,71 @@ def to_dask_array(self):
44314431

44324432
return data.to_dask_array()
44334433

4434+
@_inplace_enabled(default=False)
4435+
def to_units(self, units, inplace=False):
4436+
"""Change the data array units.
4437+
4438+
Changing the units will causes the data values to be changed
4439+
to match the new units, so the new units must be equivalent to
4440+
the existing ones.
4441+
4442+
Not to be confused with overriding the units with
4443+
`override_units`
4444+
4445+
.. versionadded:: NEXTVERSION
4446+
4447+
.. seealso:: `override_units`, `override_calendar`, `Units`,
4448+
`units`, `calendar`
4449+
4450+
:Parameters:
4451+
4452+
units: `str` or `Units`
4453+
The new units for the data array.
4454+
4455+
{{inplace: `bool`, optional}}
4456+
4457+
:Returns:
4458+
4459+
`{{class}}` or `None`
4460+
The construct with data converted to the new units, or
4461+
`None` if the operation was in-place.
4462+
4463+
**Examples**
4464+
4465+
>>> print(f.Units)
4466+
'km'
4467+
>>> print(f.array)
4468+
[1 2]
4469+
>>> g = f.to_units('metre')
4470+
>>> print(g.Units)
4471+
'metre'
4472+
>>> print(g.array)
4473+
[1000. 2000.]
4474+
>>> g.to_units('miles', inplace=True)
4475+
>>> print(g.array)
4476+
[0.62137119 1.24274238]
4477+
>>> g.to_units('degC')
4478+
Traceback (most recent call last)
4479+
...
4480+
ValueError: Can't set Units to <Units: degC> that are not equivalent to the current units <Units: miles>. Consider using the override_units method instead.
4481+
4482+
"""
4483+
f = _inplace_enabled_define_and_cleanup(self)
4484+
4485+
data = f.get_data(None)
4486+
if data is not None:
4487+
data.to_units(units, inplace=True)
4488+
f.set_data(data, copy=False)
4489+
else:
4490+
f._custom["Units"] = Units(units)
4491+
4492+
# Change the Units on the period
4493+
period = f.period()
4494+
if period is not None:
4495+
f.period(period=period.to_units(units))
4496+
4497+
return f
4498+
44344499
@_deprecated_kwarg_check("i", version="3.0.0", removed_at="4.0.0")
44354500
@_inplace_enabled(default=False)
44364501
def trunc(self, inplace=False, i=False):
@@ -4954,15 +5019,17 @@ def override_calendar(self, calendar, inplace=False, i=False):
49545019
49555020
The new calendar need not be equivalent to the original one,
49565021
and the data array elements will not be changed to reflect the
4957-
new units. Therefore, this method should only be used when it
4958-
is known that the data array values are correct but the
5022+
new calendar. Therefore, this method should only be used when
5023+
it is known that the data array values are correct but the
49595024
calendar has been incorrectly encoded.
49605025
4961-
Not to be confused with setting the `calendar` or `Units`
4962-
attributes to a calendar which is equivalent to the original
4963-
calendar
5026+
Not to be confused with changing to equivalent units with the
5027+
`to_units` method or the `Units`, `units`, or `calendar`
5028+
attributes. These approaches also convert the data to have the
5029+
new units.
49645030
4965-
.. seealso:: `calendar`, `override_units`, `units`, `Units`
5031+
.. seealso:: `override_units`, `to_units`, `units`,
5032+
`Units`,`calendar`
49665033
49675034
:Parameters:
49685035
@@ -4976,13 +5043,20 @@ def override_calendar(self, calendar, inplace=False, i=False):
49765043
:Returns:
49775044
49785045
`{{class}}` or `None`
4979-
TODO
5046+
The construct with data converted to the new units, or
5047+
`None` if the operation was in-place.
49805048
49815049
**Examples**
49825050
4983-
TODO
4984-
5051+
>>> f.Units
5052+
<Units: days since 2020-02-28>
5053+
>>> print(f.array)
5054+
1
49855055
>>> g = f.override_calendar('noleap')
5056+
>>> g.Units
5057+
<Units: days since 2020-02-28 noleap>
5058+
>>> print(f.array)
5059+
1
49865060
49875061
"""
49885062
v = _inplace_enabled_define_and_cleanup(self)
@@ -5007,19 +5081,21 @@ def override_calendar(self, calendar, inplace=False, i=False):
50075081
@_deprecated_kwarg_check("i", version="3.0.0", removed_at="4.0.0")
50085082
@_inplace_enabled(default=False)
50095083
def override_units(self, units, inplace=False, i=False):
5010-
"""Override the units.
5084+
"""Override the units. TODOUNITS
50115085
50125086
The new units need not be equivalent to the original ones, and
50135087
the data array elements will not be changed to reflect the new
50145088
units. Therefore, this method should only be used when it is
50155089
known that the data array values are correct but the units
50165090
have incorrectly encoded.
50175091
5018-
Not to be confused with setting the `units` or `Units`
5019-
attributes to units which are equivalent to the original
5020-
units.
5092+
Not to be confused with changing to equivalent units with the
5093+
`to_units` method or the `Units`, `units`, or `calendar`
5094+
attributes. These approaches also convert the data to have the
5095+
new units.
50215096
5022-
.. seealso:: `calendar`, `override_calendar`, `units`, `Units`
5097+
.. seealso:: `override_calendar`, `to_units`, `units`,
5098+
`Units`,`calendar`
50235099
50245100
:Parameters:
50255101
@@ -5033,8 +5109,8 @@ def override_units(self, units, inplace=False, i=False):
50335109
:Returns:
50345110
50355111
`{{class}}` or `None`
5036-
5037-
TODO
5112+
The construct with data converted to the new units, or
5113+
`None` if the operation was in-place.
50385114
50395115
**Examples**
50405116

0 commit comments

Comments
 (0)