Skip to content

Commit 420c127

Browse files
topper-123jreback
authored andcommitted
CLN: remove __bytes__ (#26447)
1 parent 80bddaf commit 420c127

File tree

9 files changed

+30
-35
lines changed

9 files changed

+30
-35
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ Other API Changes
250250
- Bug in :meth:`DatetimeIndex.snap` which didn't preserving the ``name`` of the input :class:`Index` (:issue:`25575`)
251251
- The ``arg`` argument in :meth:`pandas.core.groupby.DataFrameGroupBy.agg` has been renamed to ``func`` (:issue:`26089`)
252252
- The ``arg`` argument in :meth:`pandas.core.window._Window.aggregate` has been renamed to ``func`` (:issue:`26372`)
253+
- 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`)
253254

254255
.. _whatsnew_0250.deprecations:
255256

pandas/core/base.py

-9
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,6 @@ def __str__(self):
4848
"""
4949
raise AbstractMethodError(self)
5050

51-
def __bytes__(self):
52-
"""
53-
Return a bytes representation for a particular object.
54-
"""
55-
from pandas._config import get_option
56-
57-
encoding = get_option("display.encoding")
58-
return str(self).encode(encoding, 'replace')
59-
6051
def __repr__(self):
6152
"""
6253
Return a string representation for a particular object.

pandas/core/dtypes/dtypes.py

-9
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,6 @@ def __str__(self):
132132
"""
133133
return self.name
134134

135-
def __bytes__(self):
136-
"""
137-
Return a string representation for a particular object.
138-
"""
139-
from pandas._config import get_option
140-
141-
encoding = get_option("display.encoding")
142-
return str(self).encode(encoding, 'replace')
143-
144135
def __repr__(self):
145136
"""
146137
Return a string representation for a particular object.

pandas/tests/frame/test_repr_info.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,12 @@ def test_unicode_string_with_unicode(self):
140140
df = DataFrame({'A': ["\u05d0"]})
141141
str(df)
142142

143-
def test_bytestring_with_unicode(self):
144-
df = DataFrame({'A': ["\u05d0"]})
145-
bytes(df)
143+
def test_str_to_bytes_raises(self):
144+
# GH 26447
145+
df = DataFrame({'A': ["abc"]})
146+
msg = "^'str' object cannot be interpreted as an integer$"
147+
with pytest.raises(TypeError, match=msg):
148+
bytes(df)
146149

147150
def test_very_wide_info_repr(self):
148151
df = DataFrame(np.random.randn(10, 20),

pandas/tests/indexes/multi/test_format.py

-6
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,6 @@ def test_unicode_string_with_unicode():
8888
str(idx)
8989

9090

91-
def test_bytestring_with_unicode():
92-
d = {"a": ["\u05d0", 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}
93-
idx = pd.DataFrame(d).set_index(["a", "b"]).index
94-
bytes(idx)
95-
96-
9791
def test_repr_max_seq_item_setting(idx):
9892
# GH10182
9993
idx = idx.repeat(50)

pandas/tests/indexes/test_base.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -2388,10 +2388,12 @@ def test_print_unicode_columns(self):
23882388
"c": [7, 8, 9]})
23892389
repr(df.columns) # should not raise UnicodeDecodeError
23902390

2391-
@pytest.mark.parametrize("func", [str, bytes])
2392-
def test_with_unicode(self, func):
2393-
index = Index(list(range(1000)))
2394-
func(index)
2391+
def test_str_to_bytes_raises(self):
2392+
# GH 26447
2393+
index = Index([str(x) for x in range(10)])
2394+
msg = "^'str' object cannot be interpreted as an integer$"
2395+
with pytest.raises(TypeError, match=msg):
2396+
bytes(index)
23952397

23962398
def test_intersect_str_dates(self):
23972399
dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)]

pandas/tests/indexes/test_frozen.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import warnings
22

33
import numpy as np
4+
import pytest
45

56
from pandas.core.indexes.frozen import FrozenList, FrozenNDArray
67
from pandas.tests.test_base import CheckImmutable, CheckStringMixin
@@ -49,6 +50,12 @@ def test_difference_dupe(self):
4950
expected = FrozenList([1, 3])
5051
self.check_result(result, expected)
5152

53+
def test_tricky_container_to_bytes_raises(self):
54+
# GH 26447
55+
msg = "^'str' object cannot be interpreted as an integer$"
56+
with pytest.raises(TypeError, match=msg):
57+
bytes(self.unicode_container)
58+
5259

5360
class TestFrozenNDArray(CheckImmutable, CheckStringMixin):
5461
mutable_methods = ('put', 'itemset', 'fill')
@@ -68,6 +75,9 @@ def test_constructor_warns(self):
6875
with tm.assert_produces_warning(FutureWarning):
6976
FrozenNDArray([1, 2, 3])
7077

78+
def test_tricky_container_to_bytes(self):
79+
bytes(self.unicode_container)
80+
7181
def test_shallow_copying(self):
7282
original = self.container.copy()
7383
assert isinstance(self.container.view(), FrozenNDArray)

pandas/tests/series/test_repr.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import datetime, timedelta
22

33
import numpy as np
4+
import pytest
45

56
import pandas as pd
67
from pandas import (
@@ -152,9 +153,12 @@ def test_unicode_string_with_unicode(self):
152153
df = Series(["\u05d0"], name="\u05d1")
153154
str(df)
154155

155-
def test_bytestring_with_unicode(self):
156-
df = Series(["\u05d0"], name="\u05d1")
157-
bytes(df)
156+
def test_str_to_bytes_raises(self):
157+
# GH 26447
158+
df = Series(["abc"], name="abc")
159+
msg = "^'str' object cannot be interpreted as an integer$"
160+
with pytest.raises(TypeError, match=msg):
161+
bytes(df)
158162

159163
def test_timeseries_repr_object_dtype(self):
160164
index = Index([datetime(2000, 1, 1) + timedelta(i)

pandas/tests/test_base.py

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def test_tricky_container(self):
3838
pytest.skip('Need unicode_container to test with this')
3939
repr(self.unicode_container)
4040
str(self.unicode_container)
41-
bytes(self.unicode_container)
4241

4342

4443
class CheckImmutable:

0 commit comments

Comments
 (0)