Skip to content

Commit 218db50

Browse files
committed
Point to Variable or DataArray constructor in non-nanosecond warning
1 parent 6179d8e commit 218db50

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

xarray/core/variable.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@
7171
NON_NANOSECOND_WARNING = (
7272
"Converting non-nanosecond precision {case} values to nanosecond precision. "
7373
"This behavior can eventually be relaxed in xarray, as it is an artifact from "
74-
"pandas which is now beginning to support non-nanosecond precision values."
74+
"pandas which is now beginning to support non-nanosecond precision values. "
75+
"This warning is caused by passing non-nanosecond np.datetime64 or "
76+
"np.timedelta64 values to the DataArray or Variable constructor; it can be "
77+
"silenced by converting the values to nanosecond precision ahead of time."
7578
)
7679

7780

@@ -191,14 +194,14 @@ def _as_nanosecond_precision(data):
191194
isinstance(dtype, pd.DatetimeTZDtype) and dtype.unit != "ns"
192195
)
193196
if non_ns_datetime64 or non_ns_datetime_tz_dtype:
194-
warnings.warn(NON_NANOSECOND_WARNING.format(case="datetime"))
197+
warnings.warn(NON_NANOSECOND_WARNING.format(case="datetime"), stacklevel=5)
195198
if isinstance(dtype, pd.DatetimeTZDtype):
196199
nanosecond_precision_dtype = pd.DatetimeTZDtype("ns", dtype.tz)
197200
else:
198201
nanosecond_precision_dtype = "datetime64[ns]"
199202
return data.astype(nanosecond_precision_dtype)
200203
elif dtype.kind == "m" and dtype != np.dtype("timedelta64[ns]"):
201-
warnings.warn(NON_NANOSECOND_WARNING.format(case="timedelta"))
204+
warnings.warn(NON_NANOSECOND_WARNING.format(case="timedelta"), stacklevel=5)
202205
return data.astype("timedelta64[ns]")
203206
else:
204207
return data

0 commit comments

Comments
 (0)