Skip to content

Commit 3cef8d7

Browse files
spencerkclarkshoyer
authored andcommitted
Clean up _parse_array_of_cftime_strings (#2464)
* Make _parse_array_of_cftime_strings more robust * lint
1 parent 0f70a87 commit 3cef8d7

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

xarray/coding/cftimeindex.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,5 @@ def _parse_array_of_cftime_strings(strings, date_type):
389389
-------
390390
np.array
391391
"""
392-
if strings.ndim == 0:
393-
return np.array(_parse_iso8601_without_reso(date_type, strings.item()))
394-
else:
395-
return np.array([_parse_iso8601_without_reso(date_type, s)
396-
for s in strings])
392+
return np.array([_parse_iso8601_without_reso(date_type, s)
393+
for s in strings.ravel()]).reshape(strings.shape)

xarray/tests/test_cftimeindex.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,11 @@ def test_cftimeindex_shift_invalid_freq():
689689
def test_parse_array_of_cftime_strings():
690690
from cftime import DatetimeNoLeap
691691

692-
strings = np.array(['2000-01-01', '2000-01-02'])
693-
expected = np.array([DatetimeNoLeap(2000, 1, 1),
694-
DatetimeNoLeap(2000, 1, 2)])
692+
strings = np.array([['2000-01-01', '2000-01-02'],
693+
['2000-01-03', '2000-01-04']])
694+
expected = np.array(
695+
[[DatetimeNoLeap(2000, 1, 1), DatetimeNoLeap(2000, 1, 2)],
696+
[DatetimeNoLeap(2000, 1, 3), DatetimeNoLeap(2000, 1, 4)]])
695697

696698
result = _parse_array_of_cftime_strings(strings, DatetimeNoLeap)
697699
np.testing.assert_array_equal(result, expected)

0 commit comments

Comments
 (0)