Skip to content

Commit fd158d6

Browse files
[backport 2.3.x] MNT: migrate from codecs.open to open (#62073) (#62325)
Co-authored-by: Nathan Goldbaum <nathan.goldbaum@gmail.com>
1 parent 1d189f2 commit fd158d6

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

pandas/tests/io/formats/test_to_latex.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import codecs
21
from datetime import datetime
32
from textwrap import dedent
43

@@ -42,15 +41,15 @@ def test_to_latex_to_file_utf8_with_encoding(self):
4241
df = DataFrame([["au\xdfgangen"]])
4342
with tm.ensure_clean("test.tex") as path:
4443
df.to_latex(path, encoding="utf-8")
45-
with codecs.open(path, "r", encoding="utf-8") as f:
44+
with open(path, encoding="utf-8") as f:
4645
assert df.to_latex() == f.read()
4746

4847
def test_to_latex_to_file_utf8_without_encoding(self):
4948
# test with utf-8 without encoding option
5049
df = DataFrame([["au\xdfgangen"]])
5150
with tm.ensure_clean("test.tex") as path:
5251
df.to_latex(path)
53-
with codecs.open(path, "r", encoding="utf-8") as f:
52+
with open(path, encoding="utf-8") as f:
5453
assert df.to_latex() == f.read()
5554

5655
def test_to_latex_tabular_with_index(self):

pandas/tests/io/test_common.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,19 +513,18 @@ def test_is_fsspec_url():
513513
assert icom.is_fsspec_url("RFC-3986+compliant.spec://something")
514514

515515

516-
@pytest.mark.parametrize("encoding", [None, "utf-8"])
517516
@pytest.mark.parametrize("format", ["csv", "json"])
518-
def test_codecs_encoding(encoding, format):
517+
def test_codecs_encoding(format):
519518
# GH39247
520519
expected = pd.DataFrame(
521520
1.1 * np.arange(120).reshape((30, 4)),
522521
columns=pd.Index(list("ABCD")),
523522
index=pd.Index([f"i-{i}" for i in range(30)]),
524523
)
525524
with tm.ensure_clean() as path:
526-
with codecs.open(path, mode="w", encoding=encoding) as handle:
525+
with open(path, mode="w", encoding="utf-8") as handle:
527526
getattr(expected, f"to_{format}")(handle)
528-
with codecs.open(path, mode="r", encoding=encoding) as handle:
527+
with open(path, encoding="utf-8") as handle:
529528
if format == "csv":
530529
df = pd.read_csv(handle, index_col=0)
531530
else:

pandas/util/_print_versions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import codecs
43
import json
54
import locale
65
import os
@@ -139,7 +138,7 @@ def show_versions(as_json: str | bool = False) -> None:
139138
sys.stdout.writelines(json.dumps(j, indent=2))
140139
else:
141140
assert isinstance(as_json, str) # needed for mypy
142-
with codecs.open(as_json, "wb", encoding="utf8") as f:
141+
with open(as_json, "w", encoding="utf-8") as f:
143142
json.dump(j, f, indent=2)
144143

145144
else:

0 commit comments

Comments
 (0)