Skip to content

Commit 549f577

Browse files
committed
Remove the bytes_encoding parameter
1 parent 52a3f93 commit 549f577

File tree

4 files changed

+16
-24
lines changed

4 files changed

+16
-24
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ I/O
10301030
- Bug in :meth:`read_excel` for ODS files removes 0.0 values (:issue:`27222`)
10311031
- Bug in :meth:`ujson.encode` was raising an `OverflowError` with numbers larger than sys.maxsize (:issue: `34395`)
10321032
- Bug in :meth:`HDFStore.append_to_multiple` was raising a ``ValueError`` when the min_itemsize parameter is set (:issue:`11238`)
1033-
- Bug in :meth:`to_csv` which emitted b'' around bytes. It now has an optional `bytes_encoding` parameter that allows to pass a specific encoding scheme according to which the bytes are decoded. (:issue:`9712`)
1033+
- Bug in :meth:`to_csv` which emitted b'' around bytes (:issue:`9712`)
10341034

10351035
Plotting
10361036
^^^^^^^^

pandas/core/generic.py

-6
Original file line numberDiff line numberDiff line change
@@ -3000,7 +3000,6 @@ def to_csv(
30003000
index_label: Optional[Union[bool_t, str, Sequence[Label]]] = None,
30013001
mode: str = "w",
30023002
encoding: Optional[str] = None,
3003-
bytes_encoding: Optional[str] = None,
30043003
compression: Optional[Union[str, Mapping[str, str]]] = "infer",
30053004
quoting: Optional[int] = None,
30063005
quotechar: str = '"',
@@ -3058,10 +3057,6 @@ def to_csv(
30583057
encoding : str, optional
30593058
A string representing the encoding to use in the output file,
30603059
defaults to 'utf-8'.
3061-
bytes_encoding : str, optional
3062-
A string representing the encoding to use to decode the bytes
3063-
in the output file, defaults to using the 'encoding' parameter or the
3064-
encoding specified by the file object.
30653060
compression : str or dict, default 'infer'
30663061
If str, represents compression mode. If dict, value at 'method' is
30673062
the compression mode. Compression mode may be any of the following
@@ -3152,7 +3147,6 @@ def to_csv(
31523147
line_terminator=line_terminator,
31533148
sep=sep,
31543149
encoding=encoding,
3155-
bytes_encoding=bytes_encoding,
31563150
errors=errors,
31573151
compression=compression,
31583152
quoting=quoting,

pandas/io/formats/csvs.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def __init__(
6262
index_label: Optional[Union[bool, Hashable, Sequence[Hashable]]] = None,
6363
mode: str = "w",
6464
encoding: Optional[str] = None,
65-
bytes_encoding: Optional[str] = None,
6665
errors: str = "strict",
6766
compression: Union[str, Mapping[str, str], None] = "infer",
6867
quoting: Optional[int] = None,
@@ -115,11 +114,6 @@ def __init__(
115114
self.errors = errors
116115
self.compression = infer_compression(self.path_or_buf, compression)
117116

118-
if bytes_encoding is None:
119-
bytes_encoding = self.encoding
120-
121-
self.bytes_encoding = bytes_encoding
122-
123117
if quoting is None:
124118
quoting = csvlib.QUOTE_MINIMAL
125119
self.quoting = quoting
@@ -147,7 +141,7 @@ def __init__(
147141
if isinstance(cols, ABCIndexClass):
148142
cols = cols.to_native_types(
149143
na_rep=na_rep,
150-
bytes_encoding=bytes_encoding,
144+
bytes_encoding=self.encoding,
151145
float_format=float_format,
152146
date_format=date_format,
153147
quoting=self.quoting,
@@ -162,7 +156,7 @@ def __init__(
162156
if isinstance(cols, ABCIndexClass):
163157
cols = cols.to_native_types(
164158
na_rep=na_rep,
165-
bytes_encoding=bytes_encoding,
159+
bytes_encoding=self.encoding,
166160
float_format=float_format,
167161
date_format=date_format,
168162
quoting=self.quoting,
@@ -384,7 +378,7 @@ def _save_chunk(self, start_i: int, end_i: int) -> None:
384378
b = blocks[i]
385379
d = b.to_native_types(
386380
na_rep=self.na_rep,
387-
bytes_encoding=self.bytes_encoding,
381+
bytes_encoding=self.encoding,
388382
float_format=self.float_format,
389383
decimal=self.decimal,
390384
date_format=self.date_format,
@@ -398,7 +392,7 @@ def _save_chunk(self, start_i: int, end_i: int) -> None:
398392
ix = data_index.to_native_types(
399393
slicer=slicer,
400394
na_rep=self.na_rep,
401-
bytes_encoding=self.bytes_encoding,
395+
bytes_encoding=self.encoding,
402396
float_format=self.float_format,
403397
decimal=self.decimal,
404398
date_format=self.date_format,
@@ -413,4 +407,4 @@ def _bytes_to_str(self, values):
413407
np_values = np.array(values, dtype=object)
414408
if lib.is_bytes_array(np_values, skipna=True, mixing_allowed=False):
415409
for i, value in enumerate(values):
416-
values[i] = value.decode(self.bytes_encoding)
410+
values[i] = value.decode(self.encoding)

pandas/tests/frame/test_to_csv.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,7 @@ def test_to_csv_withcommas(self):
743743
def test_to_csv_bytes(self):
744744
# GH 9712
745745
times = date_range("2013-10-27 23:00", "2013-10-28 00:00", freq="H")
746-
df = DataFrame(
747-
{b"foo": [b"bar", b"baz"], b"times": times}, index=[b"A", b"B"]
748-
)
746+
df = DataFrame({b"foo": [b"bar", b"baz"], b"times": times}, index=[b"A", b"B"])
749747
df.loc[b"C"] = np.nan
750748
df.index.name = b"idx"
751749

@@ -769,14 +767,20 @@ def test_to_csv_bytes(self):
769767
df_expected = DataFrame({non_unicode_decoded: [non_unicode_decoded, "foo"]})
770768
df_expected.index.name = "idx"
771769

772-
with tm.ensure_clean("__tmp_to_csv_bytes__.csv") as path:
773-
df.to_csv(path, bytes_encoding="gb18030", header=True)
770+
with tm.ensure_clean(
771+
"__tmp_to_csv_bytes__.csv",
772+
return_filelike=True,
773+
mode="w+",
774+
encoding="gb18030",
775+
) as path:
776+
df.to_csv(path, header=True)
777+
path.seek(0)
774778
df_output = self.read_csv(path)
775779
tm.assert_frame_equal(df_output, df_expected)
776780

777781
# decoding error, when transcoding fails
778782
with pytest.raises(UnicodeDecodeError):
779-
df.to_csv(bytes_encoding="utf-8")
783+
df.to_csv(encoding="utf-8")
780784

781785
# mixing of bytes and non-bytes
782786
df = DataFrame({"foo": [b"bar", "baz"]})

0 commit comments

Comments
 (0)