diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index dc87ae8f48b8a..cf46a7f193b5a 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -250,6 +250,7 @@ Other API Changes - Bug in :meth:`DatetimeIndex.snap` which didn't preserving the ``name`` of the input :class:`Index` (:issue:`25575`) - The ``arg`` argument in :meth:`pandas.core.groupby.DataFrameGroupBy.agg` has been renamed to ``func`` (:issue:`26089`) - The ``arg`` argument in :meth:`pandas.core.window._Window.aggregate` has been renamed to ``func`` (:issue:`26372`) +- Most Pandas classes had a ``__bytes__`` method, which was used for getting a python2-style bytestring representation of the object. This method has been removed as a part of dropping Python2 (:issue:`26447`) .. _whatsnew_0250.deprecations: diff --git a/pandas/core/base.py b/pandas/core/base.py index b5b3f8118f473..f7837c60c0b82 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -48,15 +48,6 @@ def __str__(self): """ raise AbstractMethodError(self) - def __bytes__(self): - """ - Return a bytes representation for a particular object. - """ - from pandas._config import get_option - - encoding = get_option("display.encoding") - return str(self).encode(encoding, 'replace') - def __repr__(self): """ Return a string representation for a particular object. diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index f93c445b26841..32047c3fbb5e1 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -132,15 +132,6 @@ def __str__(self): """ return self.name - def __bytes__(self): - """ - Return a string representation for a particular object. - """ - from pandas._config import get_option - - encoding = get_option("display.encoding") - return str(self).encode(encoding, 'replace') - def __repr__(self): """ Return a string representation for a particular object. diff --git a/pandas/tests/frame/test_repr_info.py b/pandas/tests/frame/test_repr_info.py index 044160ac6f5e8..32594c856a236 100644 --- a/pandas/tests/frame/test_repr_info.py +++ b/pandas/tests/frame/test_repr_info.py @@ -140,9 +140,12 @@ def test_unicode_string_with_unicode(self): df = DataFrame({'A': ["\u05d0"]}) str(df) - def test_bytestring_with_unicode(self): - df = DataFrame({'A': ["\u05d0"]}) - bytes(df) + def test_str_to_bytes_raises(self): + # GH 26447 + df = DataFrame({'A': ["abc"]}) + msg = "^'str' object cannot be interpreted as an integer$" + with pytest.raises(TypeError, match=msg): + bytes(df) def test_very_wide_info_repr(self): df = DataFrame(np.random.randn(10, 20), diff --git a/pandas/tests/indexes/multi/test_format.py b/pandas/tests/indexes/multi/test_format.py index f6edb1d722321..c320cb32b856c 100644 --- a/pandas/tests/indexes/multi/test_format.py +++ b/pandas/tests/indexes/multi/test_format.py @@ -88,12 +88,6 @@ def test_unicode_string_with_unicode(): str(idx) -def test_bytestring_with_unicode(): - d = {"a": ["\u05d0", 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]} - idx = pd.DataFrame(d).set_index(["a", "b"]).index - bytes(idx) - - def test_repr_max_seq_item_setting(idx): # GH10182 idx = idx.repeat(50) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 32f3dcff0ba73..7b507a9de6b5d 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -2388,10 +2388,12 @@ def test_print_unicode_columns(self): "c": [7, 8, 9]}) repr(df.columns) # should not raise UnicodeDecodeError - @pytest.mark.parametrize("func", [str, bytes]) - def test_with_unicode(self, func): - index = Index(list(range(1000))) - func(index) + def test_str_to_bytes_raises(self): + # GH 26447 + index = Index([str(x) for x in range(10)]) + msg = "^'str' object cannot be interpreted as an integer$" + with pytest.raises(TypeError, match=msg): + bytes(index) def test_intersect_str_dates(self): dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)] diff --git a/pandas/tests/indexes/test_frozen.py b/pandas/tests/indexes/test_frozen.py index 885d078b16f56..56efd4bbfd62a 100644 --- a/pandas/tests/indexes/test_frozen.py +++ b/pandas/tests/indexes/test_frozen.py @@ -1,6 +1,7 @@ import warnings import numpy as np +import pytest from pandas.core.indexes.frozen import FrozenList, FrozenNDArray from pandas.tests.test_base import CheckImmutable, CheckStringMixin @@ -49,6 +50,12 @@ def test_difference_dupe(self): expected = FrozenList([1, 3]) self.check_result(result, expected) + def test_tricky_container_to_bytes_raises(self): + # GH 26447 + msg = "^'str' object cannot be interpreted as an integer$" + with pytest.raises(TypeError, match=msg): + bytes(self.unicode_container) + class TestFrozenNDArray(CheckImmutable, CheckStringMixin): mutable_methods = ('put', 'itemset', 'fill') @@ -68,6 +75,9 @@ def test_constructor_warns(self): with tm.assert_produces_warning(FutureWarning): FrozenNDArray([1, 2, 3]) + def test_tricky_container_to_bytes(self): + bytes(self.unicode_container) + def test_shallow_copying(self): original = self.container.copy() assert isinstance(self.container.view(), FrozenNDArray) diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 032c730fea408..92b6fb0610979 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -1,6 +1,7 @@ from datetime import datetime, timedelta import numpy as np +import pytest import pandas as pd from pandas import ( @@ -152,9 +153,12 @@ def test_unicode_string_with_unicode(self): df = Series(["\u05d0"], name="\u05d1") str(df) - def test_bytestring_with_unicode(self): - df = Series(["\u05d0"], name="\u05d1") - bytes(df) + def test_str_to_bytes_raises(self): + # GH 26447 + df = Series(["abc"], name="abc") + msg = "^'str' object cannot be interpreted as an integer$" + with pytest.raises(TypeError, match=msg): + bytes(df) def test_timeseries_repr_object_dtype(self): index = Index([datetime(2000, 1, 1) + timedelta(i) diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index 7a8ef11bafb85..3b4f85e680f6e 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -38,7 +38,6 @@ def test_tricky_container(self): pytest.skip('Need unicode_container to test with this') repr(self.unicode_container) str(self.unicode_container) - bytes(self.unicode_container) class CheckImmutable: