Skip to content

MNT: migrate from codecs.open to open #62073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions pandas/tests/io/formats/test_to_latex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import codecs
from datetime import datetime
from textwrap import dedent

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

def test_to_latex_to_file_utf8_without_encoding(self):
# test with utf-8 without encoding option
df = DataFrame([["au\xdfgangen"]])
with tm.ensure_clean("test.tex") as path:
df.to_latex(path)
with codecs.open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
assert df.to_latex() == f.read()

def test_to_latex_tabular_with_index(self):
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/io/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,19 +513,18 @@ def test_is_fsspec_url_chained():
assert not icom.is_fsspec_url("filecache::://pandas/test.csv")


@pytest.mark.parametrize("encoding", [None, "utf-8"])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could also keep testing this but CPython warns about using open(..., encoding=None): EncodingWarning: 'encoding' argument not specified. In this case we are explicitly specifying None but I don't think CPython can tell the difference.

@pytest.mark.parametrize("format", ["csv", "json"])
def test_codecs_encoding(encoding, format):
def test_codecs_encoding(format):
# GH39247
expected = pd.DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=pd.Index(list("ABCD")),
index=pd.Index([f"i-{i}" for i in range(30)]),
)
with tm.ensure_clean() as path:
with codecs.open(path, mode="w", encoding=encoding) as handle:
with open(path, mode="w", encoding="utf-8") as handle:
getattr(expected, f"to_{format}")(handle)
with codecs.open(path, mode="r", encoding=encoding) as handle:
with open(path, encoding="utf-8") as handle:
if format == "csv":
df = pd.read_csv(handle, index_col=0)
else:
Expand Down
3 changes: 1 addition & 2 deletions pandas/util/_print_versions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import codecs
import json
import locale
import os
Expand Down Expand Up @@ -143,7 +142,7 @@ def show_versions(as_json: str | bool = False) -> None:
sys.stdout.writelines(json.dumps(j, indent=2))
else:
assert isinstance(as_json, str) # needed for mypy
with codecs.open(as_json, "wb", encoding="utf8") as f:
with open(as_json, "w", encoding="utf-8") as f:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it from "wb" to "w" because I think the "b" here is a bug (bytes and utf-8???)

json.dump(j, f, indent=2)

else:
Expand Down
Loading