From d40ff68e0f2180203fdc8e273b6b181c8409e906 Mon Sep 17 00:00:00 2001 From: tp Date: Sat, 18 May 2019 01:58:23 +0200 Subject: [PATCH] DEPR: deprecate __bytes__ methods --- doc/source/whatsnew/v0.25.0.rst | 2 +- pandas/core/base.py | 5 +++++ pandas/core/dtypes/dtypes.py | 7 ++++++- pandas/tests/dtypes/test_dtypes.py | 4 ++++ pandas/tests/test_base.py | 6 ++++-- 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 895af0cd5b89a..b2994c5be4047 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -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: diff --git a/pandas/core/base.py b/pandas/core/base.py index 1d0e7fc413eb9..9455e8a6f41cd 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -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") diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index f93c445b26841..d7228d697a779 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -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") diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index decd0091e2ce8..da9485565d4d9 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -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): diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index 7a8ef11bafb85..fe8416e1744e4 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -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: