|
29 | 29 | astype_overflowsafe,
|
30 | 30 | fields,
|
31 | 31 | get_resolution,
|
| 32 | + get_unit_from_dtype, |
32 | 33 | iNaT,
|
33 | 34 | ints_to_pydatetime,
|
34 | 35 | is_date_array_normalized,
|
@@ -336,10 +337,12 @@ def _simple_new( # type: ignore[override]
|
336 | 337 | assert isinstance(values, np.ndarray)
|
337 | 338 | assert dtype.kind == "M"
|
338 | 339 | if isinstance(dtype, np.dtype):
|
339 |
| - # TODO: once non-nano DatetimeTZDtype is implemented, require that |
340 |
| - # dtype's reso match values's reso |
341 | 340 | assert dtype == values.dtype
|
342 | 341 | assert not is_unitless(dtype)
|
| 342 | + else: |
| 343 | + # DatetimeTZDtype. If we have e.g. DatetimeTZDtype[us, UTC], |
| 344 | + # then values.dtype should be M8[us]. |
| 345 | + assert dtype._reso == get_unit_from_dtype(values.dtype) |
343 | 346 |
|
344 | 347 | result = super()._simple_new(values, dtype)
|
345 | 348 | result._freq = freq
|
@@ -1186,6 +1189,9 @@ def to_perioddelta(self, freq) -> TimedeltaArray:
|
1186 | 1189 | )
|
1187 | 1190 | from pandas.core.arrays.timedeltas import TimedeltaArray
|
1188 | 1191 |
|
| 1192 | + if self._ndarray.dtype != "M8[ns]": |
| 1193 | + raise NotImplementedError("Only supported for nanosecond resolution.") |
| 1194 | + |
1189 | 1195 | i8delta = self.asi8 - self.to_period(freq).to_timestamp().asi8
|
1190 | 1196 | m8delta = i8delta.view("m8[ns]")
|
1191 | 1197 | return TimedeltaArray(m8delta)
|
@@ -1974,10 +1980,13 @@ def std(
|
1974 | 1980 | # without creating a copy by using a view on self._ndarray
|
1975 | 1981 | from pandas.core.arrays import TimedeltaArray
|
1976 | 1982 |
|
1977 |
| - tda = TimedeltaArray(self._ndarray.view("i8")) |
1978 |
| - return tda.std( |
1979 |
| - axis=axis, dtype=dtype, out=out, ddof=ddof, keepdims=keepdims, skipna=skipna |
1980 |
| - ) |
| 1983 | + # Find the td64 dtype with the same resolution as our dt64 dtype |
| 1984 | + dtype_str = self._ndarray.dtype.name.replace("datetime64", "timedelta64") |
| 1985 | + dtype = np.dtype(dtype_str) |
| 1986 | + |
| 1987 | + tda = TimedeltaArray._simple_new(self._ndarray.view(dtype), dtype=dtype) |
| 1988 | + |
| 1989 | + return tda.std(axis=axis, out=out, ddof=ddof, keepdims=keepdims, skipna=skipna) |
1981 | 1990 |
|
1982 | 1991 |
|
1983 | 1992 | # -------------------------------------------------------------------
|
|
0 commit comments