Skip to content
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

DEPR: Timedelta.freq, Timedelta.is_populated #46430

Merged
merged 4 commits into from
Mar 20, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ Other Deprecations
- Deprecated allowing non-keyword arguments in :meth:`ExtensionArray.argsort` (:issue:`46134`)
- Deprecated treating all-bool ``object``-dtype columns as bool-like in :meth:`DataFrame.any` and :meth:`DataFrame.all` with ``bool_only=True``, explicitly cast to bool instead (:issue:`46188`)
- Deprecated behavior of method :meth:`DataFrame.quantile`, attribute ``numeric_only`` will default False. Including datetime/timedelta columns in the result (:issue:`7308`).
- Deprecated :attr:`Timedelta.freq` and :attr:`Timedelta.is_populated` (:issue:`46430`)
-

.. ---------------------------------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/tslibs/timedeltas.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ cdef bint is_any_td_scalar(object obj)
cdef class _Timedelta(timedelta):
cdef readonly:
int64_t value # nanoseconds
object freq # frequency reference
bint is_populated # are my components populated
bint _is_populated # are my components populated
int64_t _d, _h, _m, _s, _ms, _us, _ns

cpdef timedelta to_pytimedelta(_Timedelta self)
Expand Down
4 changes: 4 additions & 0 deletions pandas/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,7 @@ class Timedelta(timedelta):
def __hash__(self) -> int: ...
def isoformat(self) -> str: ...
def to_numpy(self) -> np.timedelta64: ...
@property
def freq(self) -> None: ...
@property
def is_populated(self) -> bool: ...
twoertwein marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 24 additions & 5 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -826,13 +826,32 @@ cdef _to_py_int_float(v):
cdef class _Timedelta(timedelta):
# cdef readonly:
# int64_t value # nanoseconds
# object freq # frequency reference
# bint is_populated # are my components populated
# bint _is_populated # are my components populated
# int64_t _d, _h, _m, _s, _ms, _us, _ns

# higher than np.ndarray and np.matrix
__array_priority__ = 100

@property
def freq(self) -> None:
# GH#46430
warnings.warn(
Copy link
Contributor

Choose a reason for hiding this comment

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

what does this do if its called? return None?

Copy link
Member Author

Choose a reason for hiding this comment

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

correct

Copy link
Contributor

Choose a reason for hiding this comment

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

can you make that explcit (and add the annotation)

"Timedelta.freq is deprecated and will be removed in a future version",
FutureWarning,
stacklevel=1,
)
return None

@property
def is_populated(self) -> bool:
# GH#46430
warnings.warn(
"Timedelta.is_populated is deprecated and will be removed in a future version",
FutureWarning,
stacklevel=1,
)
return self._is_populated

def __hash__(_Timedelta self):
if self._has_ns():
return hash(self.value)
Expand Down Expand Up @@ -881,7 +900,7 @@ cdef class _Timedelta(timedelta):
"""
compute the components
"""
if self.is_populated:
if self._is_populated:
return

cdef:
Expand All @@ -898,7 +917,7 @@ cdef class _Timedelta(timedelta):
self._seconds = tds.seconds
self._microseconds = tds.microseconds

self.is_populated = 1
self._is_populated = 1

cpdef timedelta to_pytimedelta(_Timedelta self):
"""
Expand Down Expand Up @@ -1389,7 +1408,7 @@ class Timedelta(_Timedelta):
# make timedelta happy
td_base = _Timedelta.__new__(cls, microseconds=int(value) // 1000)
td_base.value = value
td_base.is_populated = 0
td_base._is_populated = 0
return td_base

def __setstate__(self, state):
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,25 @@ def test_timedelta_attribute_precision():
result += td.nanoseconds
expected = td.value
assert result == expected


def test_freq_deprecated():
# GH#46430
td = Timedelta(123456546, unit="ns")
with tm.assert_produces_warning(FutureWarning, match="Timedelta.freq"):
freq = td.freq

assert freq is None

with pytest.raises(AttributeError, match="is not writable"):
td.freq = offsets.Day()


def test_is_populated_deprecated():
# GH#46430
td = Timedelta(123456546, unit="ns")
with tm.assert_produces_warning(FutureWarning, match="Timedelta.is_populated"):
td.is_populated

with pytest.raises(AttributeError, match="is not writable"):
td.is_populated = 1