Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions cf/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
abspath,
)
from ..functions import atol as cf_atol
from ..functions import chunksize as cf_chunksize
from ..functions import default_netCDF_fillvals
from ..functions import fm_threshold as cf_fm_threshold
from ..functions import free_memory
Expand Down Expand Up @@ -9575,6 +9574,7 @@ def change_calendar(self, calendar, inplace=False, i=False):

return d

@daskified(_DASKIFIED_VERBOSE)
@_deprecated_kwarg_check("i")
@_inplace_enabled(default=False)
def override_units(self, units, inplace=False, i=False):
Expand All @@ -9598,27 +9598,28 @@ def override_units(self, units, inplace=False, i=False):
:Returns:

`Data` or `None`
The new data, or `None` if the operation was in-place.

**Examples**

>>> d = cf.Data(1012.0, 'hPa')
>>> d.override_units('km')
>>> d.Units
>>> e = d.override_units('km')
>>> e.Units
<Units: km>
>>> d.datum(0)
>>> e.datum()
1012.0
>>> d.override_units(Units('watts'))
>>> d.override_units(cf.Units('watts'), inplace=True)
>>> d.Units
<Units: watts>
>>> d.datum(0)
>>> d.datum()
1012.0

"""
d = _inplace_enabled_define_and_cleanup(self)
d._Units = Units(units)

return d

@daskified(_DASKIFIED_VERBOSE)
@_deprecated_kwarg_check("i")
@_inplace_enabled(default=False)
def override_calendar(self, calendar, inplace=False, i=False):
Expand All @@ -9642,13 +9643,23 @@ def override_calendar(self, calendar, inplace=False, i=False):
:Returns:

`Data` or `None`
The new data, or `None` if the operation was in-place.

**Examples**

>>> d = cf.Data(1, 'days since 2020-02-28')
>>> d
<CF Data(): 2020-02-29 00:00:00>
>>> d.datum()
1
>>> e = d.override_calendar('noleap')
<CF Data(): 2020-03-01 00:00:00 noleap>
>>> e.datum()
1

"""
d = _inplace_enabled_define_and_cleanup(self)
d._Units = Units(d.Units._units, calendar)

return d

def to_dask_array(self):
Expand Down
16 changes: 16 additions & 0 deletions cf/test/test_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3835,6 +3835,22 @@ def test_Data_fill_value(self):
del d.fill_value
self.assertIsNone(d.fill_value)

def test_Data_override_units(self):
d = cf.Data(1012, "hPa")
e = d.override_units("km")
self.assertEqual(e.Units, cf.Units("km"))
self.assertEqual(e.datum(), d.datum())

self.assertIsNone(d.override_units(cf.Units("watts"), inplace=True))

def test_Data_override_calendar(self):
d = cf.Data(1, "days since 2020-02-28")
e = d.override_calendar("noleap")
self.assertEqual(e.Units, cf.Units("days since 2020-02-28", "noleap"))
self.assertEqual(e.datum(), d.datum())

self.assertIsNone(d.override_calendar("all_leap", inplace=True))


if __name__ == "__main__":
print("Run date:", datetime.datetime.now())
Expand Down