Skip to content

BUG: StataWriterUTF8 removing some valid characters in column names (#47276) #47297

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 1 commit into from
Jun 10, 2022
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ I/O
- :meth:`to_html` now excludes the ``border`` attribute from ``<table>`` elements when ``border`` keyword is set to ``False``.
- Bug in :func:`read_sas` returned ``None`` rather than an empty DataFrame for SAS7BDAT files with zero rows (:issue:`18198`)
- Bug in :class:`StataWriter` where value labels were always written with default encoding (:issue:`46750`)
- Bug in :class:`StataWriterUTF8` where some valid characters were removed from variable names (:issue:`47276`)

Period
^^^^^^
Expand Down
16 changes: 10 additions & 6 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3646,12 +3646,16 @@ def _validate_variable_name(self, name: str) -> str:
# High code points appear to be acceptable
for c in name:
if (
ord(c) < 128
and (c < "A" or c > "Z")
and (c < "a" or c > "z")
and (c < "0" or c > "9")
and c != "_"
) or 128 <= ord(c) < 256:
(
ord(c) < 128
and (c < "A" or c > "Z")
and (c < "a" or c > "z")
and (c < "0" or c > "9")
and c != "_"
)
or 128 <= ord(c) < 192
or c in {"×", "÷"}
):
name = name.replace(c, "_")

return name
4 changes: 2 additions & 2 deletions pandas/tests/io/test_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1786,11 +1786,11 @@ def test_utf8_writer(self, version):
[2.0, 2, "ᴮ", ""],
[3.0, 3, "ᴰ", None],
],
columns=["a", "β", "ĉ", "strls"],
columns=["Å", "β", "ĉ", "strls"],
)
data["ᴐᴬᵀ"] = cat
variable_labels = {
"a": "apple",
"Å": "apple",
"β": "ᵈᵉᵊ",
"ĉ": "ᴎტჄႲႳႴႶႺ",
"strls": "Long Strings",
Expand Down