Skip to content

Commit 31b8d48

Browse files
committed
DEPR: deprecate __bytes__ methods
1 parent ebe6b91 commit 31b8d48

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ Deprecations
258258
- Deprecated the ``units=M`` (months) and ``units=Y`` (year) parameters for ``units`` of :func:`pandas.to_timedelta`, :func:`pandas.Timedelta` and :func:`pandas.TimedeltaIndex` (:issue:`16344`)
259259
- The functions :func:`pandas.to_datetime` and :func:`pandas.to_timedelta` have deprecated the ``box`` keyword. Instead, use :meth:`to_numpy` or :meth:`Timestamp.to_datetime64` or :meth:`Timedelta.to_timedelta64`. (:issue:`24416`)
260260
- The :meth:`DataFrame.compound` and :meth:`Series.compound` methods are deprecated and will be removed in a future version.
261-
261+
- Deprecated the ``__bytes__`` method of :class:`pandas.core.base.StringMixin` and :class:`pandas.core.dtypes.PandasExtensionDtype` and their subclasses. The method was needed for Python2 compatibility (:issue:`xxxxx`).
262262

263263
.. _whatsnew_0250.prior_deprecations:
264264

pandas/core/base.py

+5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ def __str__(self):
5151
def __bytes__(self):
5252
"""
5353
Return a bytes representation for a particular object.
54+
55+
.. deprecated:: 0.25.0
5456
"""
57+
warnings.warn("{obj}.__bytes__ is deprecated and will be removed "
58+
"in a future version".format(obj=type(self).__name__),
59+
FutureWarning, stacklevel=2)
5560
from pandas._config import get_option
5661

5762
encoding = get_option("display.encoding")

pandas/core/dtypes/dtypes.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,13 @@ def __str__(self):
134134

135135
def __bytes__(self):
136136
"""
137-
Return a string representation for a particular object.
137+
Return a bytes representation for a particular object.
138+
139+
.. deprecated:: 0.25.0
138140
"""
141+
warnings.warn("{obj}.__bytes__ is deprecated and will be removed "
142+
"in a future version".format(obj=type(self).__name__),
143+
FutureWarning, stacklevel=2)
139144
from pandas._config import get_option
140145

141146
encoding = get_option("display.encoding")

pandas/tests/dtypes/test_dtypes.py

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def test_pickle(self):
5050
assert not len(self.dtype._cache)
5151
assert result == self.dtype
5252

53+
def test_bytes_deprecated(self):
54+
with tm.assert_produces_warning(FutureWarning):
55+
bytes(self.dtype)
56+
5357

5458
class TestCategoricalDtype(Base):
5559

pandas/tests/test_base.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ class CheckStringMixin:
3131
def test_string_methods_dont_fail(self):
3232
repr(self.container)
3333
str(self.container)
34-
bytes(self.container)
3534

3635
def test_tricky_container(self):
3736
if not hasattr(self, 'unicode_container'):
3837
pytest.skip('Need unicode_container to test with this')
3938
repr(self.unicode_container)
4039
str(self.unicode_container)
41-
bytes(self.unicode_container)
40+
41+
def test_bytes_deprecated(self):
42+
with tm.assert_produces_warning(FutureWarning):
43+
bytes(self.container)
4244

4345

4446
class CheckImmutable:

0 commit comments

Comments
 (0)