From 156aa34870cd60d5fd1ccc943591bd83ed8224e5 Mon Sep 17 00:00:00 2001 From: "ukmo-chris.bunney" Date: Wed, 25 Sep 2024 12:18:10 +0100 Subject: [PATCH 1/3] Updated docstring for module function date2num to reflect underlying cftime changes. --- cf_units/__init__.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/cf_units/__init__.py b/cf_units/__init__.py index 34ff409c..05374f92 100644 --- a/cf_units/__init__.py +++ b/cf_units/__init__.py @@ -336,6 +336,10 @@ def date2num(date, unit, calendar): time-zone offset. If there is a time-zone offset in unit, it will be applied to the returned numeric values. + 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): @@ -350,7 +354,7 @@ def date2num(date, unit, calendar): Name of the calendar, see cf_units.CALENDARS. Returns: - float, or numpy.ndarray of float. + float/integer or numpy.ndarray of floats/integers For example: @@ -365,6 +369,13 @@ def date2num(date, unit, calendar): ... cf_units.CALENDAR_STANDARD) array([6.5, 7.5]) + # Integer type preferentially returned if possible: + >>> dt1 = datetime.datetime(1970, 1, 1, 5, 0) + >>> + >>> cf_units.date2num([dt1, dt2], 'hours since 1970-01-01 00:00:00', + ... cf_units.CALENDAR_STANDARD) + array([5, 6]) + """ # From a8443fee81de468a2d3f8bd8726726dac997530d Mon Sep 17 00:00:00 2001 From: "ukmo-chris.bunney" Date: Wed, 25 Sep 2024 12:21:56 +0100 Subject: [PATCH 2/3] Fix doctest that .precommit messed up... --- cf_units/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cf_units/__init__.py b/cf_units/__init__.py index 05374f92..7e4bb19b 100644 --- a/cf_units/__init__.py +++ b/cf_units/__init__.py @@ -371,7 +371,7 @@ def date2num(date, unit, calendar): # Integer type preferentially returned if possible: >>> dt1 = datetime.datetime(1970, 1, 1, 5, 0) - >>> + >>> dt2 = datetime.datetime(1970, 1, 1, 6, 0) >>> cf_units.date2num([dt1, dt2], 'hours since 1970-01-01 00:00:00', ... cf_units.CALENDAR_STANDARD) array([5, 6]) From ae4fc0c3bb14a38af188955bcdcbe16ba57b9a71 Mon Sep 17 00:00:00 2001 From: "ukmo-chris.bunney" Date: Thu, 26 Sep 2024 13:50:53 +0100 Subject: [PATCH 3/3] Updated date2num doctests to fix failing test (and bad rendering in docs) --- cf_units/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cf_units/__init__.py b/cf_units/__init__.py index 7e4bb19b..003bf458 100644 --- a/cf_units/__init__.py +++ b/cf_units/__init__.py @@ -368,8 +368,7 @@ def date2num(date, unit, calendar): >>> cf_units.date2num([dt1, dt2], 'hours since 1970-01-01 00:00:00', ... cf_units.CALENDAR_STANDARD) array([6.5, 7.5]) - - # Integer type preferentially returned if possible: + >>> # Integer type preferentially returned if possible: >>> dt1 = datetime.datetime(1970, 1, 1, 5, 0) >>> dt2 = datetime.datetime(1970, 1, 1, 6, 0) >>> cf_units.date2num([dt1, dt2], 'hours since 1970-01-01 00:00:00', @@ -1896,8 +1895,7 @@ def date2num(self, date): >>> u.date2num([datetime.datetime(1970, 1, 1, 5, 30), ... datetime.datetime(1970, 1, 1, 6, 30)]) array([5.5, 6.5]) - - # Integer type preferentially returned if possible: + >>> # 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])