diff --git a/xarray/coding/times.py b/xarray/coding/times.py index b9324d8395e..f0a4c195263 100644 --- a/xarray/coding/times.py +++ b/xarray/coding/times.py @@ -830,7 +830,7 @@ def cftime_to_nptime( f"standard calendar. Reason: {e}." ) from e else: - dt = np.datetime64("NaT") + dt = np.datetime64("NaT", time_unit) new.append(dt) return np.asarray(new).reshape(times.shape) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 750fff0bd67..b4cdf5cf6ca 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -238,7 +238,7 @@ def _possibly_convert_objects(values): and values.dtype.kind == "O" and Version(pd.__version__) >= Version("3.0.0dev0") ): - result.dtype = values.dtype + result = result.view(values.dtype) return result diff --git a/xarray/tests/test_accessor_dt.py b/xarray/tests/test_accessor_dt.py index 061898296e6..de8181f6a28 100644 --- a/xarray/tests/test_accessor_dt.py +++ b/xarray/tests/test_accessor_dt.py @@ -266,7 +266,7 @@ def test_seasons(self) -> None: dates = xr.date_range( start="2000/01/01", freq="ME", periods=12, use_cftime=False ) - dates = dates.append(pd.Index([np.datetime64("NaT")])) + dates = dates.append(pd.Index([np.datetime64("NaT", "us")])) dates = xr.DataArray(dates) seasons = xr.DataArray( [ diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 15e38233c4b..e03f15a5119 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -1391,7 +1391,11 @@ def test_roundtrip_datetime64_nanosecond_precision( ) -> None: # test for GH7817 time = np.datetime64(timestr, format) - times = [np.datetime64("1970-01-01T00:00:00", format), np.datetime64("NaT"), time] + times = [ + np.datetime64("1970-01-01T00:00:00", format), + np.datetime64("NaT", format), + time, + ] if use_encoding: encoding = dict(dtype=dtype, _FillValue=fill_value) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index cad40e78821..d09061f2258 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -1335,7 +1335,7 @@ def test_chunk_by_frequency(self, freq: str, calendar: str, add_gap: bool) -> No ).to_numpy(copy=True) if add_gap: # introduce an empty bin - time[31 : 31 + ΔN] = np.datetime64("NaT") + time[31 : 31 + ΔN] = np.datetime64("NaT", "us") time = time[~np.isnat(time)] else: time = time[:N] diff --git a/xarray/tests/test_duck_array_ops.py b/xarray/tests/test_duck_array_ops.py index a9a2d377449..cbf30515e94 100644 --- a/xarray/tests/test_duck_array_ops.py +++ b/xarray/tests/test_duck_array_ops.py @@ -723,11 +723,11 @@ def test_argmin_max_error(): ["array", "expected"], [ ( - np.array([np.datetime64("2000-01-01"), np.datetime64("NaT")]), + np.array([np.datetime64("2000-01-01"), np.datetime64("NaT", "D")]), np.array([False, True]), ), ( - np.array([np.timedelta64(1, "h"), np.timedelta64("NaT")]), + np.array([np.timedelta64(1, "h"), np.timedelta64("NaT", "h")]), np.array([False, True]), ), ( diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index fcf1124c18e..7c06f7c1525 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -726,7 +726,7 @@ def test_groupby_drops_nans(shuffle: bool, chunk: Literal[False] | dict) -> None [ np.datetime64("2001-01-01"), np.datetime64("2001-01-01"), - np.datetime64("NaT"), + np.datetime64("NaT", "D"), ], ) expected5 = xr.DataArray(3, [("t", [np.datetime64("2001-01-01")])]) @@ -2018,7 +2018,7 @@ def test_resample_first_last(self, use_cftime) -> None: # missing periods, GH10169 actual = array.isel(time=[0, 1, 2, 3, 8, 9]).resample(time="1D").last() expected = DataArray( - np.array([times[3], np.datetime64("NaT"), times[9]]), + np.array([times[3], np.datetime64("NaT", "us"), times[9]]), dims="time", coords={"time": times[::4]}, name="time", @@ -3836,7 +3836,7 @@ def test_mean_datetime_edge_cases(): # Test with NaT values dates_with_nat = pd.date_range("2021-01-01", periods=4, freq="D") dates_with_nat_array = dates_with_nat.values.copy() - dates_with_nat_array[1] = np.datetime64("NaT") + dates_with_nat_array[1] = np.datetime64("NaT", "us") ds = xr.Dataset( {