From 8fb552f82275a25b3f2f2356b5708f9722a8158f Mon Sep 17 00:00:00 2001 From: jswhit Date: Thu, 16 Jul 2020 16:09:58 -0600 Subject: [PATCH] fix for issue #185 (date2num should work with numpy scalar inputs) --- Changelog | 3 ++- cftime/_cftime.pyx | 7 ++----- test/test_cftime.py | 3 +++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Changelog b/Changelog index c831bb13..8b1c0dfd 100644 --- a/Changelog +++ b/Changelog @@ -1,7 +1,8 @@ -version 1.2.1 (not yeat released) +version 1.2.1 (not yet released) ================================= * num2date uses 'proleptic_gregorian' scheme when basedate is post-Gregorian but date is pre-Gregorian (issue #182). + * fix 1.2.0 regression (date2num no longer works with numpy scalar array inputs, issue #185). version 1.2.0 (release tag v1.2.0rel) ===================================== diff --git a/cftime/_cftime.pyx b/cftime/_cftime.pyx index cc0a8e09..0fd7f0c2 100644 --- a/cftime/_cftime.pyx +++ b/cftime/_cftime.pyx @@ -220,11 +220,8 @@ def date2num(dates,units,calendar='standard'): if np.ma.isMA(dates) and np.ma.is_masked(dates): mask = dates.mask ismasked = True - if isscalar: - dates = np.array([dates]) - else: - dates = np.array(dates) - shape = dates.shape + dates = np.asanyarray(dates) + shape = dates.shape # are all dates python datetime instances? all_python_datetimes = True for date in dates.flat: diff --git a/test/test_cftime.py b/test/test_cftime.py index 75f5c5dd..29d04c6d 100644 --- a/test/test_cftime.py +++ b/test/test_cftime.py @@ -805,6 +805,9 @@ def test_tz_naive(self): time2 = date2num(date,units,calendar=calendar) date2 = num2date(time2,units,calendar=calendar) assert(date2 == refdate) +# issue #185: date2num should work the numpy scalar array of dates (1.2.0 regression) + dates = np.array(datetime(2010, 2, 2, 0, 0)) + assert (date2num(dates, units="hours since 2010-02-01 00:00:00") == 24.) class TestDate2index(unittest.TestCase):