Skip to content

ENH: support non-nano in DTA.std #47245

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

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
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
21 changes: 15 additions & 6 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
conversion,
fields,
get_resolution,
get_unit_from_dtype,
iNaT,
ints_to_pydatetime,
is_date_array_normalized,
Expand Down Expand Up @@ -338,10 +339,12 @@ def _simple_new( # type: ignore[override]
assert isinstance(values, np.ndarray)
assert dtype.kind == "M"
if isinstance(dtype, np.dtype):
# TODO: once non-nano DatetimeTZDtype is implemented, require that
# dtype's reso match values's reso
assert dtype == values.dtype
assert not is_unitless(dtype)
else:
# DatetimeTZDtype. If we have e.g. DatetimeTZDtype[us, UTC],
# then values.dtype should be M8[us].
assert dtype._reso == get_unit_from_dtype(values.dtype)

result = super()._simple_new(values, dtype)
result._freq = freq
Expand Down Expand Up @@ -1231,6 +1234,9 @@ def to_perioddelta(self, freq) -> TimedeltaArray:
)
from pandas.core.arrays.timedeltas import TimedeltaArray

if self._ndarray.dtype != "M8[ns]":
raise NotImplementedError("Only supported for nanosecond resolution.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any test hits this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, but this is deprecated anyway. mostly a note to myself not to bother implementing it for non-nano


i8delta = self.asi8 - self.to_period(freq).to_timestamp().asi8
m8delta = i8delta.view("m8[ns]")
return TimedeltaArray(m8delta)
Expand Down Expand Up @@ -2019,10 +2025,13 @@ def std(
# without creating a copy by using a view on self._ndarray
from pandas.core.arrays import TimedeltaArray

tda = TimedeltaArray(self._ndarray.view("i8"))
return tda.std(
axis=axis, dtype=dtype, out=out, ddof=ddof, keepdims=keepdims, skipna=skipna
)
# Find the td64 dtype with the same resolution as our dt64 dtype
dtype_str = self._ndarray.dtype.name.replace("datetime64", "timedelta64")
dtype = np.dtype(dtype_str)

tda = TimedeltaArray._simple_new(self._ndarray.view(dtype), dtype=dtype)

return tda.std(axis=axis, out=out, ddof=ddof, keepdims=keepdims, skipna=skipna)


# -------------------------------------------------------------------
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ def test_normalize(self, unit):
res = dta.normalize()
tm.assert_extension_array_equal(res, expected)

def test_simple_new_requires_match(self, unit):
arr = np.arange(5, dtype=np.int64).view(f"M8[{unit}]")
dtype = DatetimeTZDtype(unit, "UTC")

dta = DatetimeArray._simple_new(arr, dtype=dtype)
assert dta.dtype == dtype

wrong = DatetimeTZDtype("ns", "UTC")
with pytest.raises(AssertionError, match=""):
DatetimeArray._simple_new(arr, dtype=wrong)

def test_std_non_nano(self, unit):
dti = pd.date_range("2016-01-01", periods=55, freq="D")
arr = np.asarray(dti).astype(f"M8[{unit}]")

dta = DatetimeArray._simple_new(arr, dtype=arr.dtype)

# we should match the nano-reso std, but floored to our reso.
res = dta.std()
assert res._reso == dta._reso
assert res == dti.std().floor(unit)


class TestDatetimeArrayComparisons:
# TODO: merge this into tests/arithmetic/test_datetime64 once it is
Expand Down