From 0ba82319a94b3d6b73ae8512507e04709869b4f9 Mon Sep 17 00:00:00 2001 From: Chris Bunney <48915820+ukmo-ccbunney@users.noreply.github.com> Date: Tue, 24 Sep 2024 20:37:10 +0100 Subject: [PATCH] Updated docstring for num2date. (#467) Change in cfunits mean it will now preferentially return an integer rather than a float if if encoded datetime(s) can be expressed exactly using ints. --- cf_units/__init__.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cf_units/__init__.py b/cf_units/__init__.py index 33366729..34ff409c 100644 --- a/cf_units/__init__.py +++ b/cf_units/__init__.py @@ -1861,6 +1861,10 @@ def date2num(self, date): Works for scalars, sequences and numpy arrays. Returns a scalar if input is a scalar, else returns a numpy array. + Return type will be of type `integer` if (all) the times can be + encoded exactly as an integer with the specified units, + otherwise a float type will be returned. + Args: * date (datetime): @@ -1868,7 +1872,7 @@ def date2num(self, date): The datetime objects should not include a time-zone offset. Returns: - float or numpy.ndarray of float. + float/integer or numpy.ndarray of floats/integers For example: @@ -1882,6 +1886,11 @@ def date2num(self, date): ... datetime.datetime(1970, 1, 1, 6, 30)]) array([5.5, 6.5]) + # Integer type preferentially returned if possible: + >>> u.date2num([datetime.datetime(1970, 1, 1, 5, 0), + ... datetime.datetime(1970, 1, 1, 6, 0)]) + array([5, 6]) + """ return cftime.date2num(date, self.cftime_unit, self.calendar)