Skip to content

Commit f8fee90

Browse files
authored
Allow decoding of size-0 datetimes (#6882)
1 parent bdd1da8 commit f8fee90

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Deprecations
3434
Bug fixes
3535
~~~~~~~~~
3636

37+
- Allow decoding of 0 sized datetimes(:issue:`1329`, :pull:`6882`)
38+
By `Deepak Cherian <https://github.com/dcherian>`_.
3739
- Make sure DataArray.name is always a string when used as label for plotting.
3840
(:issue:`6826`, :pull:`6832`)
3941
By `Jimmy Westling <https://github.com/illviljan>`_.

xarray/coding/times.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,12 @@ def _decode_cf_datetime_dtype(data, units, calendar, use_cftime):
197197
def _decode_datetime_with_cftime(num_dates, units, calendar):
198198
if cftime is None:
199199
raise ModuleNotFoundError("No module named 'cftime'")
200-
return np.asarray(
201-
cftime.num2date(num_dates, units, calendar, only_use_cftime_datetimes=True)
202-
)
200+
if num_dates.size > 0:
201+
return np.asarray(
202+
cftime.num2date(num_dates, units, calendar, only_use_cftime_datetimes=True)
203+
)
204+
else:
205+
return np.array([], dtype=object)
203206

204207

205208
def _decode_datetime_with_pandas(flat_num_dates, units, calendar):
@@ -220,8 +223,10 @@ def _decode_datetime_with_pandas(flat_num_dates, units, calendar):
220223

221224
with warnings.catch_warnings():
222225
warnings.filterwarnings("ignore", "invalid value encountered", RuntimeWarning)
223-
pd.to_timedelta(flat_num_dates.min(), delta) + ref_date
224-
pd.to_timedelta(flat_num_dates.max(), delta) + ref_date
226+
if flat_num_dates.size > 0:
227+
# avoid size 0 datetimes GH1329
228+
pd.to_timedelta(flat_num_dates.min(), delta) + ref_date
229+
pd.to_timedelta(flat_num_dates.max(), delta) + ref_date
225230

226231
# To avoid integer overflow when converting to nanosecond units for integer
227232
# dtypes smaller than np.int64 cast all integer and unsigned integer dtype

xarray/tests/test_coding_times.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,3 +1150,20 @@ def test_decode_cf_datetime_uint64_with_cftime_overflow_error():
11501150
num_dates = np.uint64(1_000_000 * 86_400 * 360 * 500_000)
11511151
with pytest.raises(OverflowError):
11521152
decode_cf_datetime(num_dates, units, calendar)
1153+
1154+
1155+
@pytest.mark.parametrize("use_cftime", [True, False])
1156+
def test_decode_0size_datetime(use_cftime):
1157+
# GH1329
1158+
if use_cftime and not has_cftime:
1159+
pytest.skip()
1160+
1161+
dtype = object if use_cftime else "M8[ns]"
1162+
expected = np.array([], dtype=dtype)
1163+
actual = decode_cf_datetime(
1164+
np.zeros(shape=0, dtype=np.int64),
1165+
units="days since 1970-01-01 00:00:00",
1166+
calendar="proleptic_gregorian",
1167+
use_cftime=use_cftime,
1168+
)
1169+
np.testing.assert_equal(expected, actual)

0 commit comments

Comments
 (0)