Skip to content

Test for preserved dtype during datetime roundtrip #10159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions xarray/tests/test_coding_times.py
Original file line number Diff line number Diff line change
@@ -1304,6 +1304,19 @@
np.testing.assert_equal(actual, expected)


def test_roundtrip_float32_datetime():
expected = np.array([1867128, 1867134, 1867140], dtype="float32")
units = "hours since 1800-01-01"
calendar = "standard"

arr = decode_cf_datetime(expected, units=units, calendar=calendar, use_cftime=False)
actual, _, _ = encode_cf_datetime(
arr, units=units, calendar=calendar, dtype=expected.dtype
)
np.testing.assert_equal(actual, expected)
assert actual.dtype == expected.dtype

Check failure on line 1317 in xarray/tests/test_coding_times.py

GitHub Actions / ubuntu-latest py3.10 bare-minimum

test_roundtrip_float32_datetime AssertionError: assert dtype('float64') == dtype('float32') + where dtype('float64') = array([1867128., 1867134., 1867140.]).dtype + and dtype('float32') = array([1867128., 1867134., 1867140.], dtype=float32).dtype

Check failure on line 1317 in xarray/tests/test_coding_times.py

GitHub Actions / ubuntu-latest py3.12 all-but-dask

test_roundtrip_float32_datetime AssertionError: assert dtype('float64') == dtype('float32') + where dtype('float64') = array([1867128., 1867134., 1867140.]).dtype + and dtype('float32') = array([1867128., 1867134., 1867140.], dtype=float32).dtype

Check failure on line 1317 in xarray/tests/test_coding_times.py

GitHub Actions / macos-latest py3.13

test_roundtrip_float32_datetime AssertionError: assert dtype('float64') == dtype('float32') + where dtype('float64') = array([1867128., 1867134., 1867140.]).dtype + and dtype('float32') = array([1867128., 1867134., 1867140.], dtype=float32).dtype

Check failure on line 1317 in xarray/tests/test_coding_times.py

GitHub Actions / ubuntu-latest py3.13 all-but-numba

test_roundtrip_float32_datetime AssertionError: assert dtype('float64') == dtype('float32') + where dtype('float64') = array([1867128., 1867134., 1867140.]).dtype + and dtype('float32') = array([1867128., 1867134., 1867140.], dtype=float32).dtype

Check failure on line 1317 in xarray/tests/test_coding_times.py

GitHub Actions / ubuntu-latest py3.10

test_roundtrip_float32_datetime AssertionError: assert dtype('float64') == dtype('float32') + where dtype('float64') = array([1867128., 1867134., 1867140.]).dtype + and dtype('float32') = array([1867128., 1867134., 1867140.], dtype=float32).dtype

Check failure on line 1317 in xarray/tests/test_coding_times.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

test_roundtrip_float32_datetime AssertionError: assert dtype('float64') == dtype('float32') + where dtype('float64') = array([1867128., 1867134., 1867140.]).dtype + and dtype('float32') = array([1867128., 1867134., 1867140.], dtype=float32).dtype

Check failure on line 1317 in xarray/tests/test_coding_times.py

GitHub Actions / macos-latest py3.10

test_roundtrip_float32_datetime AssertionError: assert dtype('float64') == dtype('float32') + where dtype('float64') = array([1867128., 1867134., 1867140.]).dtype + and dtype('float32') = array([1867128., 1867134., 1867140.], dtype=float32).dtype

Check failure on line 1317 in xarray/tests/test_coding_times.py

GitHub Actions / ubuntu-latest py3.13

test_roundtrip_float32_datetime AssertionError: assert dtype('float64') == dtype('float32') + where dtype('float64') = array([1867128., 1867134., 1867140.]).dtype + and dtype('float32') = array([1867128., 1867134., 1867140.], dtype=float32).dtype


@pytest.mark.parametrize("time_unit", ["ms", "us", "ns"])
def test_decode_float_datetime_with_decimals(
time_unit: PDDatetimeUnitOptions,

Unchanged files with check annotations Beta

# otherwise numpy unsigned ints will silently cast to the signed counterpart
fill_value = fill_value.item()
# passes if provided fill value fits in encoded on-disk type
new_fill = encoded_dtype.type(fill_value)

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).

Check warning on line 237 in xarray/coding/variables.py

GitHub Actions / ubuntu-latest py3.10 min-all-deps

NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays. The conversion of 255 to int8 will fail in the future. For the old behavior, usually: np.array(value).astype(dtype)` will give the desired result (the cast overflows).
except OverflowError:
encoded_kind_str = "signed" if encoded_dtype.kind == "i" else "unsigned"
warnings.warn(
if xp == np:
# numpy currently doesn't have a astype:
return data.astype(dtype, **kwargs)

Check warning on line 237 in xarray/core/duck_array_ops.py

GitHub Actions / macos-latest py3.10

invalid value encountered in cast

Check warning on line 237 in xarray/core/duck_array_ops.py

GitHub Actions / macos-latest py3.10

invalid value encountered in cast

Check warning on line 237 in xarray/core/duck_array_ops.py

GitHub Actions / windows-latest py3.10

invalid value encountered in cast

Check warning on line 237 in xarray/core/duck_array_ops.py

GitHub Actions / windows-latest py3.10

invalid value encountered in cast
return xp.astype(data, dtype, **kwargs)