From 3cef8d730d5bbd699a393fa15266064ebb9849e2 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Fri, 5 Oct 2018 04:02:17 -0400 Subject: [PATCH] Clean up _parse_array_of_cftime_strings (#2464) * Make _parse_array_of_cftime_strings more robust * lint --- xarray/coding/cftimeindex.py | 7 ++----- xarray/tests/test_cftimeindex.py | 8 +++++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index 341ecfed262..75a1fc9bd1a 100644 --- a/xarray/coding/cftimeindex.py +++ b/xarray/coding/cftimeindex.py @@ -389,8 +389,5 @@ def _parse_array_of_cftime_strings(strings, date_type): ------- np.array """ - if strings.ndim == 0: - return np.array(_parse_iso8601_without_reso(date_type, strings.item())) - else: - return np.array([_parse_iso8601_without_reso(date_type, s) - for s in strings]) + return np.array([_parse_iso8601_without_reso(date_type, s) + for s in strings.ravel()]).reshape(strings.shape) diff --git a/xarray/tests/test_cftimeindex.py b/xarray/tests/test_cftimeindex.py index a558ab9a784..33bf2cbce0d 100644 --- a/xarray/tests/test_cftimeindex.py +++ b/xarray/tests/test_cftimeindex.py @@ -689,9 +689,11 @@ def test_cftimeindex_shift_invalid_freq(): def test_parse_array_of_cftime_strings(): from cftime import DatetimeNoLeap - strings = np.array(['2000-01-01', '2000-01-02']) - expected = np.array([DatetimeNoLeap(2000, 1, 1), - DatetimeNoLeap(2000, 1, 2)]) + strings = np.array([['2000-01-01', '2000-01-02'], + ['2000-01-03', '2000-01-04']]) + expected = np.array( + [[DatetimeNoLeap(2000, 1, 1), DatetimeNoLeap(2000, 1, 2)], + [DatetimeNoLeap(2000, 1, 3), DatetimeNoLeap(2000, 1, 4)]]) result = _parse_array_of_cftime_strings(strings, DatetimeNoLeap) np.testing.assert_array_equal(result, expected)