Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

potential fix for issue #328 #330

Merged
merged 3 commits into from
May 3, 2024
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
2 changes: 2 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ since version 1.6.3 release
===========================
* build musllinux wheels (issue #307).
* return empty array if one provided to date2num (issue #315).
* numpy 2.0 compatibility (issue #325).
* handle nan/inf in num2date (issue #328).

version 1.6.3 (release tag v1.6.3rel)
=====================================
Expand Down
3 changes: 3 additions & 0 deletions src/cftime/_cftime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,9 @@ def num2date(
factor = UNIT_CONVERSION_FACTORS[unit]
times = np.asanyarray(times) # Allow list as input
times = upcast_times(times)
# convert to masked array if any nan or inf values present
if not np.isfinite(times).all():
times = np.ma.masked_invalid(times)
scaled_times = scale_times(times, factor)
scaled_times = cast_to_int(scaled_times,units=unit)

Expand Down
13 changes: 13 additions & 0 deletions test/test_cftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,19 @@ def roundtrip(delta,eps,units):
# date2num should return an empty array if given one (issue #315)
d = cftime.date2num([], 'seconds since 2000-01-01 12:00:00')
assert d.size==0
# issue #328: handle nan/inf in num2date.
times = np.array([1,2,3,np.nan],dtype=np.float64)
expected = np.array([DatetimeGregorian(2000, 1, 2, 0, 0, 0, 0),
DatetimeGregorian(2000, 1, 3, 0, 0, 0, 0),
DatetimeGregorian(2000, 1, 4, 0, 0, 0, 0),
DatetimeGregorian(2000, 1, 5, 0, 0, 0, 0)])
mask = [False, False, False, True]
expected = np.ma.masked_array(expected, mask=mask)
result = cftime.num2date(times, 'days since 2000-01-01', 'standard')
np.testing.assert_equal(result, expected)
times = np.array([1,2,3,np.inf],dtype=np.float64)
result = cftime.num2date(times, 'days since 2000-01-01', 'standard')
np.testing.assert_equal(result, expected)


class TestDate2index(unittest.TestCase):
Expand Down
Loading