Skip to content

Commit

Permalink
Updated docstring for num2date. (#467)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ukmo-ccbunney authored Sep 24, 2024
1 parent eaebbac commit 0ba8231
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1861,14 +1861,18 @@ 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):
A datetime object or a sequence of datetime objects.
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:
Expand All @@ -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)

Expand Down

0 comments on commit 0ba8231

Please sign in to comment.