Skip to content

Commit 9c9f1b0

Browse files
committed
dev
1 parent 32edc83 commit 9c9f1b0

File tree

4 files changed

+38
-38
lines changed

4 files changed

+38
-38
lines changed

Changelog.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ Version NEXTVERSION
33

44
**2025-??-??**
55

6-
* New methods: `cf.Field.to_units`, `cf.Data.to_units`
7-
(https://github.com/NCAS-CMS/cf-python/issues/???)
6+
* New methods to allow changing units in a chain: `cf.Field.to_units`,
7+
`cf.Data.to_units`
8+
(https://github.com/NCAS-CMS/cf-python/issues/874)
89

910
----
1011

cf/data/data.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5651,7 +5651,7 @@ def change_calendar(self, calendar, inplace=False, i=False):
56515651
@_deprecated_kwarg_check("i", version="3.0.0", removed_at="4.0.0")
56525652
@_inplace_enabled(default=False)
56535653
def override_units(self, units, inplace=False, i=False):
5654-
"""Override the data array units.
5654+
"""Override the units.
56555655
56565656
The new units need not be equivalent to the original ones, and
56575657
the data array elements will not be changed to reflect the new
@@ -5711,10 +5711,9 @@ def override_calendar(self, calendar, inplace=False, i=False):
57115711
it is known that the data array values are correct but the
57125712
calendar has been incorrectly encoded.
57135713
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.
5714+
Not to be confused with changing to an equivalent calendar
5715+
with the `to_units` method or the `Units` or `calendar`
5716+
attributes.
57185717
57195718
.. seealso:: `override_units`, `to_units`, `Units`, `units`,
57205719
`calendar`
@@ -7386,8 +7385,8 @@ def to_units(self, units, inplace=False):
73867385
"""Change the data array units.
73877386
73887387
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.
7388+
to match the new units, therefore the new units must be
7389+
equivalent to the existing ones.
73917390
73927391
Not to be confused with overriding the units with
73937392
`override_units`

cf/mixin/propertiesdata.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4436,8 +4436,8 @@ def to_units(self, units, inplace=False):
44364436
"""Change the data array units.
44374437
44384438
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.
4439+
to match the new units, therefore the new units must be
4440+
equivalent to the existing ones.
44414441
44424442
Not to be confused with overriding the units with
44434443
`override_units`
@@ -5023,13 +5023,12 @@ def override_calendar(self, calendar, inplace=False, i=False):
50235023
it is known that the data array values are correct but the
50245024
calendar has been incorrectly encoded.
50255025
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.
5026+
Not to be confused with changing to an equivalent calendar
5027+
with the `to_units` method or the `Units` or `calendar`
5028+
attributes.
50305029
5031-
.. seealso:: `override_units`, `to_units`, `units`,
5032-
`Units`,`calendar`
5030+
.. seealso:: `override_units`, `to_units`, `units`, `Units`,
5031+
`calendar`
50335032
50345033
:Parameters:
50355034
@@ -5081,7 +5080,7 @@ def override_calendar(self, calendar, inplace=False, i=False):
50815080
@_deprecated_kwarg_check("i", version="3.0.0", removed_at="4.0.0")
50825081
@_inplace_enabled(default=False)
50835082
def override_units(self, units, inplace=False, i=False):
5084-
"""Override the units. TODOUNITS
5083+
"""Override the units.
50855084
50865085
The new units need not be equivalent to the original ones, and
50875086
the data array elements will not be changed to reflect the new

cf/mixin/propertiesdatabounds.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,10 +2188,9 @@ def override_calendar(self, calendar, inplace=False, i=False):
21882188
it is known that the data array values are correct but the
21892189
calendar has been incorrectly encoded.
21902190
2191-
Not to be confused with changing to equivalent units with the
2192-
`to_units` method or the `Units`, `units`, or `calendar`
2193-
attributes. These approaches also convert the data to have the
2194-
new units.
2191+
Not to be confused with changing to an equivalent calendar
2192+
with the `to_units` method or the `Units` or `calendar`
2193+
attributes.
21952194
21962195
.. seealso:: `override_units`, `to_units`, `Units`, `units`,
21972196
`calendar`
@@ -3326,8 +3325,8 @@ def to_units(self, units, inplace=False):
33263325
"""Change the data array units.
33273326
33283327
Changing the units will causes the data values to be changed
3329-
to match the new units, so the new units must be equivalent to
3330-
the existing ones.
3328+
to match the new units, therefore the new units must be
3329+
equivalent to the existing ones.
33313330
33323331
Not to be confused with overriding the units with
33333332
`override_units`
@@ -3352,20 +3351,22 @@ def to_units(self, units, inplace=False):
33523351
33533352
**Examples**
33543353
3355-
>>> f.Units TODOUNITS
3356-
<Units: hPa>
3357-
>>> f.datum(0)
3358-
100000.0
3359-
>>> f.override_units('km')
3360-
>>> f.Units
3361-
<Units: km>
3362-
>>> f.datum(0)
3363-
100000.0
3364-
>>> f.override_units(Units('watts'))
3365-
>>> f.Units
3366-
<Units: watts>
3367-
>>> f.datum(0)
3368-
100000.0
3354+
>>> print(f.Units)
3355+
'km'
3356+
>>> print(f.array)
3357+
[1 2]
3358+
>>> g = f.to_units('metre')
3359+
>>> print(g.Units)
3360+
'metre'
3361+
>>> print(g.array)
3362+
[1000. 2000.]
3363+
>>> g.to_units('miles', inplace=True)
3364+
>>> print(g.array)
3365+
[0.62137119 1.24274238]
3366+
>>> g.to_units('degC')
3367+
Traceback (most recent call last)
3368+
...
3369+
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.
33693370
33703371
"""
33713372
return self._apply_superclass_data_oper(

0 commit comments

Comments
 (0)