Skip to content

Commit

Permalink
Clean up _parse_array_of_cftime_strings (#2464)
Browse files Browse the repository at this point in the history
* Make _parse_array_of_cftime_strings more robust

* lint
  • Loading branch information
spencerkclark authored and shoyer committed Oct 5, 2018
1 parent 0f70a87 commit 3cef8d7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
7 changes: 2 additions & 5 deletions xarray/coding/cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 5 additions & 3 deletions xarray/tests/test_cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3cef8d7

Please sign in to comment.