Skip to content

BUG: Integer values at the top end of the supported range incorrectly… #59310

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 3 commits into from
Jul 25, 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 @@ -583,6 +583,7 @@ I/O
- Bug in :meth:`read_excel` raising ``ValueError`` when passing array of boolean values when ``dtype="boolean"``. (:issue:`58159`)
- Bug in :meth:`read_json` not validating the ``typ`` argument to not be exactly ``"frame"`` or ``"series"`` (:issue:`59124`)
- Bug in :meth:`read_stata` raising ``KeyError`` when input file is stored in big-endian format and contains strL data. (:issue:`58638`)
- Bug in :meth:`read_stata` where extreme value integers were incorrectly interpreted as missing for format versions 111 and prior (:issue:`58130`)

Period
^^^^^^
Expand Down
37 changes: 31 additions & 6 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,19 @@ def __init__(self) -> None:
np.float64(struct.unpack("<d", float64_max)[0]),
),
}
self.OLD_VALID_RANGE = {
"b": (-128, 126),
"h": (-32768, 32766),
"l": (-2147483648, 2147483646),
"f": (
np.float32(struct.unpack("<f", float32_min)[0]),
np.float32(struct.unpack("<f", float32_max)[0]),
),
"d": (
np.float64(struct.unpack("<d", float64_min)[0]),
np.float64(struct.unpack("<d", float64_max)[0]),
),
}

self.OLD_TYPE_MAPPING = {
98: 251, # byte
Expand All @@ -994,7 +1007,7 @@ def __init__(self) -> None:

# These missing values are the generic '.' in Stata, and are used
# to replace nans
self.MISSING_VALUES = {
self.MISSING_VALUES: dict[str, int | np.float32 | np.float64] = {
"b": 101,
"h": 32741,
"l": 2147483621,
Expand Down Expand Up @@ -1808,11 +1821,18 @@ def _do_convert_missing(self, data: DataFrame, convert_missing: bool) -> DataFra
replacements = {}
for i in range(len(data.columns)):
fmt = self._typlist[i]
if fmt not in self.VALID_RANGE:
continue
if self._format_version <= 111:
if fmt not in self.OLD_VALID_RANGE:
continue

fmt = cast(str, fmt) # only strs in VALID_RANGE
nmin, nmax = self.VALID_RANGE[fmt]
fmt = cast(str, fmt) # only strs in OLD_VALID_RANGE
nmin, nmax = self.OLD_VALID_RANGE[fmt]
else:
if fmt not in self.VALID_RANGE:
continue

fmt = cast(str, fmt) # only strs in VALID_RANGE
nmin, nmax = self.VALID_RANGE[fmt]
series = data.iloc[:, i]

# appreciably faster to do this with ndarray instead of Series
Expand All @@ -1827,7 +1847,12 @@ def _do_convert_missing(self, data: DataFrame, convert_missing: bool) -> DataFra
umissing, umissing_loc = np.unique(series[missing], return_inverse=True)
replacement = Series(series, dtype=object)
for j, um in enumerate(umissing):
missing_value = StataMissingValue(um)
if self._format_version <= 111:
missing_value = StataMissingValue(
float(self.MISSING_VALUES[fmt])
)
else:
missing_value = StataMissingValue(um)

loc = missing_loc[umissing_loc == j]
replacement.iloc[loc] = missing_value
Expand Down
Binary file added pandas/tests/io/data/stata/stata1_108.dta
Binary file not shown.
Binary file added pandas/tests/io/data/stata/stata1_110.dta
Binary file not shown.
Binary file added pandas/tests/io/data/stata/stata1_111.dta
Binary file not shown.
Binary file added pandas/tests/io/data/stata/stata1_113.dta
Binary file not shown.
Binary file added pandas/tests/io/data/stata/stata1_115.dta
Binary file not shown.
Binary file added pandas/tests/io/data/stata/stata1_118.dta
Binary file not shown.
Binary file added pandas/tests/io/data/stata/stata1_119.dta
Binary file not shown.
Binary file added pandas/tests/io/data/stata/stata8_108.dta
Binary file not shown.
Binary file added pandas/tests/io/data/stata/stata8_110.dta
Binary file not shown.
Binary file added pandas/tests/io/data/stata/stata8_111.dta
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
83 changes: 80 additions & 3 deletions pandas/tests/io/test_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ def test_read_index_col_none(self, version, temp_file):
expected["a"] = expected["a"].astype(np.int32)
tm.assert_frame_equal(read_df, expected, check_index_type=True)

@pytest.mark.parametrize("file", ["stata1_114", "stata1_117"])
def test_read_dta1(self, file, datapath):
file = datapath("io", "data", "stata", f"{file}.dta")
# Note this test starts at format version 108 as the missing code for double
# was different prior to this (see GH 58149) and would therefore fail
@pytest.mark.parametrize("version", [108, 110, 111, 113, 114, 115, 117, 118, 119])
def test_read_dta1(self, version, datapath):
file = datapath("io", "data", "stata", f"stata1_{version}.dta")
parsed = self.read_dta(file)

# Pandas uses np.nan as missing value.
Expand All @@ -136,6 +138,18 @@ def test_read_dta1(self, file, datapath):
# the casting doesn't fail so need to match stata here
expected["float_miss"] = expected["float_miss"].astype(np.float32)

# Column names too long for older Stata formats
if version <= 108:
expected = expected.rename(
columns={
"float_miss": "f_miss",
"double_miss": "d_miss",
"byte_miss": "b_miss",
"int_miss": "i_miss",
"long_miss": "l_miss",
}
)

tm.assert_frame_equal(parsed, expected)

def test_read_dta2(self, datapath):
Expand Down Expand Up @@ -920,6 +934,23 @@ def test_missing_value_conversion(self, file, datapath):
)
tm.assert_frame_equal(parsed, expected)

# Note this test starts at format version 108 as the missing code for double
# was different prior to this (see GH 58149) and would therefore fail
@pytest.mark.parametrize("file", ["stata8_108", "stata8_110", "stata8_111"])
def test_missing_value_conversion_compat(self, file, datapath):
columns = ["int8_", "int16_", "int32_", "float32_", "float64_"]
smv = StataMissingValue(101)
keys = sorted(smv.MISSING_VALUES.keys())
data = []
row = [StataMissingValue(keys[j * 27]) for j in range(5)]
data.append(row)
expected = DataFrame(data, columns=columns)

parsed = read_stata(
datapath("io", "data", "stata", f"{file}.dta"), convert_missing=True
)
tm.assert_frame_equal(parsed, expected)

def test_big_dates(self, datapath, temp_file):
yr = [1960, 2000, 9999, 100, 2262, 1677]
mo = [1, 1, 12, 1, 4, 9]
Expand Down Expand Up @@ -2035,6 +2066,52 @@ def test_read_write_ea_dtypes(self, dtype_backend, temp_file, tmp_path):

tm.assert_frame_equal(written_and_read_again.set_index("index"), expected)

@pytest.mark.parametrize("version", [113, 114, 115, 117, 118, 119])
def test_read_data_int_validranges(self, version, datapath):
expected = DataFrame(
{
"byte": np.array([-127, 100], dtype=np.int8),
"int": np.array([-32767, 32740], dtype=np.int16),
"long": np.array([-2147483647, 2147483620], dtype=np.int32),
}
)

parsed = read_stata(
datapath("io", "data", "stata", f"stata_int_validranges_{version}.dta")
)
tm.assert_frame_equal(parsed, expected)

@pytest.mark.parametrize("version", [104, 105, 108, 110, 111])
def test_read_data_int_validranges_compat(self, version, datapath):
expected = DataFrame(
{
"byte": np.array([-128, 126], dtype=np.int8),
"int": np.array([-32768, 32766], dtype=np.int16),
"long": np.array([-2147483648, 2147483646], dtype=np.int32),
}
)

parsed = read_stata(
datapath("io", "data", "stata", f"stata_int_validranges_{version}.dta")
)
tm.assert_frame_equal(parsed, expected)

# The byte type was not supported prior to the 104 format
@pytest.mark.parametrize("version", [102, 103])
def test_read_data_int_validranges_compat_nobyte(self, version, datapath):
expected = DataFrame(
{
"byte": np.array([-128, 126], dtype=np.int16),
"int": np.array([-32768, 32766], dtype=np.int16),
"long": np.array([-2147483648, 2147483646], dtype=np.int32),
}
)

parsed = read_stata(
datapath("io", "data", "stata", f"stata_int_validranges_{version}.dta")
)
tm.assert_frame_equal(parsed, expected)


@pytest.mark.parametrize("version", [105, 108, 110, 111, 113, 114])
def test_backward_compat(version, datapath):
Expand Down