Skip to content

Commit 3f366e4

Browse files
authored
DEPR: Timedelta.freq, Timedelta.is_populated (#46430)
1 parent 24652cf commit 3f366e4

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ Other Deprecations
342342
- Deprecated allowing non-keyword arguments in :meth:`ExtensionArray.argsort` (:issue:`46134`)
343343
- 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`)
344344
- Deprecated behavior of method :meth:`DataFrame.quantile`, attribute ``numeric_only`` will default False. Including datetime/timedelta columns in the result (:issue:`7308`).
345+
- Deprecated :attr:`Timedelta.freq` and :attr:`Timedelta.is_populated` (:issue:`46430`)
345346
-
346347

347348
.. ---------------------------------------------------------------------------

pandas/_libs/tslibs/timedeltas.pxd

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ cdef bint is_any_td_scalar(object obj)
1111
cdef class _Timedelta(timedelta):
1212
cdef readonly:
1313
int64_t value # nanoseconds
14-
object freq # frequency reference
15-
bint is_populated # are my components populated
14+
bint _is_populated # are my components populated
1615
int64_t _d, _h, _m, _s, _ms, _us, _ns
1716

1817
cpdef timedelta to_pytimedelta(_Timedelta self)

pandas/_libs/tslibs/timedeltas.pyi

+4
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,7 @@ class Timedelta(timedelta):
146146
def __hash__(self) -> int: ...
147147
def isoformat(self) -> str: ...
148148
def to_numpy(self) -> np.timedelta64: ...
149+
@property
150+
def freq(self) -> None: ...
151+
@property
152+
def is_populated(self) -> bool: ...

pandas/_libs/tslibs/timedeltas.pyx

+24-5
Original file line numberDiff line numberDiff line change
@@ -826,13 +826,32 @@ cdef _to_py_int_float(v):
826826
cdef class _Timedelta(timedelta):
827827
# cdef readonly:
828828
# int64_t value # nanoseconds
829-
# object freq # frequency reference
830-
# bint is_populated # are my components populated
829+
# bint _is_populated # are my components populated
831830
# int64_t _d, _h, _m, _s, _ms, _us, _ns
832831

833832
# higher than np.ndarray and np.matrix
834833
__array_priority__ = 100
835834

835+
@property
836+
def freq(self) -> None:
837+
# GH#46430
838+
warnings.warn(
839+
"Timedelta.freq is deprecated and will be removed in a future version",
840+
FutureWarning,
841+
stacklevel=1,
842+
)
843+
return None
844+
845+
@property
846+
def is_populated(self) -> bool:
847+
# GH#46430
848+
warnings.warn(
849+
"Timedelta.is_populated is deprecated and will be removed in a future version",
850+
FutureWarning,
851+
stacklevel=1,
852+
)
853+
return self._is_populated
854+
836855
def __hash__(_Timedelta self):
837856
if self._has_ns():
838857
return hash(self.value)
@@ -881,7 +900,7 @@ cdef class _Timedelta(timedelta):
881900
"""
882901
compute the components
883902
"""
884-
if self.is_populated:
903+
if self._is_populated:
885904
return
886905

887906
cdef:
@@ -898,7 +917,7 @@ cdef class _Timedelta(timedelta):
898917
self._seconds = tds.seconds
899918
self._microseconds = tds.microseconds
900919

901-
self.is_populated = 1
920+
self._is_populated = 1
902921

903922
cpdef timedelta to_pytimedelta(_Timedelta self):
904923
"""
@@ -1389,7 +1408,7 @@ class Timedelta(_Timedelta):
13891408
# make timedelta happy
13901409
td_base = _Timedelta.__new__(cls, microseconds=int(value) // 1000)
13911410
td_base.value = value
1392-
td_base.is_populated = 0
1411+
td_base._is_populated = 0
13931412
return td_base
13941413

13951414
def __setstate__(self, state):

pandas/tests/scalar/timedelta/test_timedelta.py

+22
Original file line numberDiff line numberDiff line change
@@ -659,3 +659,25 @@ def test_timedelta_attribute_precision():
659659
result += td.nanoseconds
660660
expected = td.value
661661
assert result == expected
662+
663+
664+
def test_freq_deprecated():
665+
# GH#46430
666+
td = Timedelta(123456546, unit="ns")
667+
with tm.assert_produces_warning(FutureWarning, match="Timedelta.freq"):
668+
freq = td.freq
669+
670+
assert freq is None
671+
672+
with pytest.raises(AttributeError, match="is not writable"):
673+
td.freq = offsets.Day()
674+
675+
676+
def test_is_populated_deprecated():
677+
# GH#46430
678+
td = Timedelta(123456546, unit="ns")
679+
with tm.assert_produces_warning(FutureWarning, match="Timedelta.is_populated"):
680+
td.is_populated
681+
682+
with pytest.raises(AttributeError, match="is not writable"):
683+
td.is_populated = 1

0 commit comments

Comments
 (0)