Skip to content

Commit

Permalink
revert some tests to align with scalar logic handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kmuehlbauer committed Dec 16, 2024
1 parent 5de8d0d commit fc985d9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
9 changes: 6 additions & 3 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,17 @@ def _possibly_convert_objects(values):
"""
as_series = pd.Series(values.ravel(), copy=False)
# When receiving objects which pd.Series can't resolve by its own
# we try astype-conversion to "us"-resolution for datetimes and pd.Timestamp.
# we try astype-conversion to "ns"-resolution for datetimes and pd.Timestamp.
if (
values.dtype.kind == "O"
and as_series.dtype.kind == "O"
and as_series.size > 0
and isinstance(as_series[0], datetime | pd.Timestamp)
and (
isinstance(as_series[0], datetime | pd.Timestamp)
or pd.api.types.is_datetime64_dtype(as_series[0])
)
):
as_series = as_series.astype("=M8[us]")
as_series = as_series.astype("=M8[ns]")
result = np.asarray(as_series).reshape(values.shape)
if not result.flags.writeable:
# GH8843, pandas copy-on-write mode creates read-only arrays by default
Expand Down
18 changes: 9 additions & 9 deletions xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,8 +1086,8 @@ def test_numpy_same_methods(self):
"values, unit",
[
(np.datetime64("2000-01-01"), "s"),
(pd.Timestamp("2000-01-01T00"), "s"),
(datetime(2000, 1, 1), "us"),
(pd.Timestamp("2000-01-01T00"), "ns"),
(datetime(2000, 1, 1), "ns"),
],
)
def test_datetime64_conversion_scalar(self, values, unit):
Expand All @@ -1102,8 +1102,8 @@ def test_datetime64_conversion_scalar(self, values, unit):
"values, unit",
[
(np.timedelta64(1, "D"), "s"),
(pd.Timedelta("1 day"), "us"),
(timedelta(days=1), "us"),
(pd.Timedelta("1 day"), "ns"),
(timedelta(days=1), "ns"),
],
)
def test_timedelta64_conversion_scalar(self, values, unit):
Expand All @@ -1126,12 +1126,12 @@ def test_0d_str(self):
def test_0d_datetime(self):
# todo: check, if this test is OK
v = Variable([], pd.Timestamp("2000-01-01"))
assert v.dtype == np.dtype("datetime64[s]")
assert v.dtype == np.dtype("datetime64[ns]")
assert v.values == np.datetime64("2000-01-01", "s")

@pytest.mark.filterwarnings("ignore:Converting non-default")
@pytest.mark.parametrize(
"values, unit", [(pd.to_timedelta("1s"), "us"), (np.timedelta64(1, "s"), "s")]
"values, unit", [(pd.to_timedelta("1s"), "ns"), (np.timedelta64(1, "s"), "s")]
)
def test_0d_timedelta(self, values, unit):
# todo: check, if this test is OK
Expand Down Expand Up @@ -2679,7 +2679,7 @@ def test_datetime(self):
actual = as_compatible_data(datetime(2000, 1, 1))
assert np.asarray(expected) == actual
assert np.ndarray is type(actual)
assert np.dtype("datetime64[us]") == actual.dtype
assert np.dtype("datetime64[ns]") == actual.dtype

def test_tz_datetime(self) -> None:
# todo: check, if this test is OK
Expand Down Expand Up @@ -3002,7 +3002,7 @@ def test_from_pint_wrapping_dask(self, Var):
(np.array([np.datetime64("2000-01-01", "ns")]), "ns"),
(np.array([np.datetime64("2000-01-01", "s")]), "s"),
(pd.date_range("2000", periods=1), "ns"),
(datetime(2000, 1, 1), "us"),
(datetime(2000, 1, 1), "ns"),
(np.array([datetime(2000, 1, 1)]), "ns"),
(pd.date_range("2000", periods=1, tz=pytz.timezone("America/New_York")), "ns"),
(
Expand Down Expand Up @@ -3079,7 +3079,7 @@ def test_pandas_two_only_datetime_conversion_warnings(
(np.array([np.timedelta64(10, "ns")]), "ns"),
(np.array([np.timedelta64(10, "s")]), "s"),
(pd.timedelta_range("1", periods=1), "ns"),
(timedelta(days=1), "us"),
(timedelta(days=1), "ns"),
(np.array([timedelta(days=1)]), "ns"),
(pd.timedelta_range("1", periods=1).astype("timedelta64[s]"), "s"),
],
Expand Down

0 comments on commit fc985d9

Please sign in to comment.