Skip to content

Commit 2d1e59d

Browse files
authored
BUG: Unable to open Stata 118 or 119 format files saved in big-endian… (#58640)
* BUG: Unable to open Stata 118 or 119 format files saved in big-endian format that contain strL data * Rename test functions to make their purpose clearer
1 parent a787f45 commit 2d1e59d

14 files changed

+43
-12
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ I/O
546546
- Bug in :meth:`DataFrame.to_excel` when writing empty :class:`DataFrame` with :class:`MultiIndex` on both axes (:issue:`57696`)
547547
- Bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
548548
- Bug in :meth:`read_csv` raising ``TypeError`` when ``index_col`` is specified and ``na_values`` is a dict containing the key ``None``. (:issue:`57547`)
549+
- Bug in :meth:`read_stata` raising ``KeyError`` when input file is stored in big-endian format and contains strL data. (:issue:`58638`)
549550

550551
Period
551552
^^^^^^

pandas/io/stata.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,14 +1600,13 @@ def _read_strls(self) -> None:
16001600
v_o = self._read_uint64()
16011601
else:
16021602
buf = self._path_or_buf.read(12)
1603-
# Only tested on little endian file on little endian machine.
1603+
# Only tested on little endian machine.
16041604
v_size = 2 if self._format_version == 118 else 3
16051605
if self._byteorder == "<":
16061606
buf = buf[0:v_size] + buf[4 : (12 - v_size)]
16071607
else:
1608-
# This path may not be correct, impossible to test
1609-
buf = buf[0:v_size] + buf[(4 + v_size) :]
1610-
v_o = struct.unpack("Q", buf)[0]
1608+
buf = buf[4 - v_size : 4] + buf[(4 + v_size) :]
1609+
v_o = struct.unpack(f"{self._byteorder}Q", buf)[0]
16111610
typ = self._read_uint8()
16121611
length = self._read_uint32()
16131612
va = self._path_or_buf.read(length)
2.56 KB
Binary file not shown.
2.57 KB
Binary file not shown.
1.25 KB
Binary file not shown.
2.56 KB
Binary file not shown.
2.57 KB
Binary file not shown.
5.44 KB
Binary file not shown.
5.43 KB
Binary file not shown.
5.44 KB
Binary file not shown.

0 commit comments

Comments
 (0)