Skip to content

DEPR: deprecate __bytes__ methods #26444

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Deprecations
- 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`)
- 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`)
- The :meth:`DataFrame.compound` and :meth:`Series.compound` methods are deprecated and will be removed in a future version.

- 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:`26444`).

.. _whatsnew_0250.prior_deprecations:

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ def __str__(self):
def __bytes__(self):
"""
Return a bytes representation for a particular object.

.. deprecated:: 0.25.0
"""
warnings.warn("{obj}.__bytes__ is deprecated and will be removed "
"in a future version".format(obj=type(self).__name__),
FutureWarning, stacklevel=2)
from pandas._config import get_option

encoding = get_option("display.encoding")
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,13 @@ def __str__(self):

def __bytes__(self):
"""
Return a string representation for a particular object.
Return a bytes representation for a particular object.

.. deprecated:: 0.25.0
"""
warnings.warn("{obj}.__bytes__ is deprecated and will be removed "
"in a future version".format(obj=type(self).__name__),
FutureWarning, stacklevel=2)
from pandas._config import get_option

encoding = get_option("display.encoding")
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def test_pickle(self):
assert not len(self.dtype._cache)
assert result == self.dtype

def test_bytes_deprecated(self):
with tm.assert_produces_warning(FutureWarning):
bytes(self.dtype)


class TestCategoricalDtype(Base):

Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ class CheckStringMixin:
def test_string_methods_dont_fail(self):
repr(self.container)
str(self.container)
bytes(self.container)

def test_tricky_container(self):
if not hasattr(self, 'unicode_container'):
pytest.skip('Need unicode_container to test with this')
repr(self.unicode_container)
str(self.unicode_container)
bytes(self.unicode_container)

def test_bytes_deprecated(self):
with tm.assert_produces_warning(FutureWarning):
bytes(self.container)


class CheckImmutable:
Expand Down