Skip to content

BUG: Remove incorrect check on value label length #60156

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
Oct 31, 2024
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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ I/O
- Bug in :meth:`DataFrame.to_dict` raises unnecessary ``UserWarning`` when columns are not unique and ``orient='tight'``. (:issue:`58281`)
- Bug in :meth:`DataFrame.to_excel` when writing empty :class:`DataFrame` with :class:`MultiIndex` on both axes (:issue:`57696`)
- Bug in :meth:`DataFrame.to_stata` when writing :class:`DataFrame` and ``byteorder=`big```. (:issue:`58969`)
- Bug in :meth:`DataFrame.to_stata` when writing more than 32,000 value labels. (:issue:`60107`)
- Bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
- Bug in :meth:`HDFStore.get` was failing to save data of dtype datetime64[s] correctly (:issue:`59004`)
- Bug in :meth:`read_csv` causing segmentation fault when ``encoding_errors`` is not a string. (:issue:`59059`)
Expand Down
6 changes: 0 additions & 6 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,12 +691,6 @@ def _prepare_value_labels(self) -> None:
self.txt.append(category)
self.n += 1

if self.text_len > 32000:
raise ValueError(
"Stata value labels for a single variable must "
"have a combined length less than 32,000 characters."
)

# Ensure int32
self.off = np.array(offsets, dtype=np.int32)
self.val = np.array(values, dtype=np.int32)
Expand Down
28 changes: 12 additions & 16 deletions pandas/tests/io/test_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from datetime import datetime
import gzip
import io
import itertools
import os
import string
import struct
import tarfile
import zipfile
Expand Down Expand Up @@ -1163,28 +1165,13 @@ def test_categorical_writing(self, version, temp_file):

def test_categorical_warnings_and_errors(self, temp_file):
# Warning for non-string labels
# Error for labels too long
original = DataFrame.from_records(
[["a" * 10000], ["b" * 10000], ["c" * 10000], ["d" * 10000]],
columns=["Too_long"],
)

original = original.astype("category")
path = temp_file
msg = (
"Stata value labels for a single variable must have "
r"a combined length less than 32,000 characters\."
)
with pytest.raises(ValueError, match=msg):
original.to_stata(path)

original = DataFrame.from_records(
[["a"], ["b"], ["c"], ["d"], [1]], columns=["Too_long"]
).astype("category")

msg = "data file created has not lost information due to duplicate labels"
with tm.assert_produces_warning(ValueLabelTypeMismatch, match=msg):
original.to_stata(path)
original.to_stata(temp_file)
# should get a warning for mixed content

@pytest.mark.parametrize("version", [114, 117, 118, 119, None])
Expand Down Expand Up @@ -2592,3 +2579,12 @@ def test_empty_frame(temp_file):
df3 = read_stata(path, columns=["a"])
assert "b" not in df3
tm.assert_series_equal(df3.dtypes, dtypes.loc[["a"]])


@pytest.mark.parametrize("version", [114, 117, 118, 119, None])
def test_many_strl(temp_file, version):
n = 65534
df = DataFrame(np.arange(n), columns=["col"])
lbls = ["".join(v) for v in itertools.product(*([string.ascii_letters] * 3))]
value_labels = {"col": {i: lbls[i] for i in range(n)}}
df.to_stata(temp_file, value_labels=value_labels, version=version)
Loading