Skip to content

Commit 42ed6d3

Browse files
authored
promote floating-point numeric datetimes to 64-bit before decoding (pydata#9182)
* promote floating-point dates to 64-bit while decoding * add a test to make sure we don't regress * whats-new entry
1 parent f4183ec commit 42ed6d3

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/whats-new.rst

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Bug fixes
3737
~~~~~~~~~
3838
- Make :py:func:`testing.assert_allclose` work with numpy 2.0 (:issue:`9165`, :pull:`9166`).
3939
By `Pontus Lurcock <https://github.com/pont-us>`_.
40+
- Promote floating-point numeric datetimes before decoding (:issue:`9179`, :pull:`9182`).
41+
By `Justus Magin <https://github.com/keewis>`_.
4042

4143

4244
Documentation

xarray/coding/times.py

+2
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ def _decode_datetime_with_pandas(
278278
# timedelta64 value, and therefore would raise an error in the lines above.
279279
if flat_num_dates.dtype.kind in "iu":
280280
flat_num_dates = flat_num_dates.astype(np.int64)
281+
elif flat_num_dates.dtype.kind in "f":
282+
flat_num_dates = flat_num_dates.astype(np.float64)
281283

282284
# Cast input ordinals to integers of nanoseconds because pd.to_timedelta
283285
# works much faster when dealing with integers (GH 1399).

xarray/tests/test_coding_times.py

+16
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,22 @@ def test_decode_0size_datetime(use_cftime):
11821182
np.testing.assert_equal(expected, actual)
11831183

11841184

1185+
def test_decode_float_datetime():
1186+
num_dates = np.array([1867128, 1867134, 1867140], dtype="float32")
1187+
units = "hours since 1800-01-01"
1188+
calendar = "standard"
1189+
1190+
expected = np.array(
1191+
["2013-01-01T00:00:00", "2013-01-01T06:00:00", "2013-01-01T12:00:00"],
1192+
dtype="datetime64[ns]",
1193+
)
1194+
1195+
actual = decode_cf_datetime(
1196+
num_dates, units=units, calendar=calendar, use_cftime=False
1197+
)
1198+
np.testing.assert_equal(actual, expected)
1199+
1200+
11851201
@requires_cftime
11861202
def test_scalar_unit() -> None:
11871203
# test that a scalar units (often NaN when using to_netcdf) does not raise an error

0 commit comments

Comments
 (0)