Skip to content

Commit 3f46c7a

Browse files
committed
FIX: Add endianness missing flag when reading data
Added endianess flat to data type to allow data to be read cross platforms closes #8688
1 parent dcbd007 commit 3f46c7a

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

doc/source/whatsnew/v0.16.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Bug Fixes
9999

100100

101101

102-
102+
- Fixed bug on bug endian platforms which produced incorrect results in ``StataReader`` (:issue:`8688`).
103103

104104
- Bug in ``MultiIndex.has_duplicates`` when having many levels causes an indexer overflow (:issue:`9075`, :issue:`5873`)
105105
- Bug in ``pivot`` and `unstack`` where ``nan`` values would break index alignment (:issue:`7466`)

pandas/io/stata.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ def __init__(self, path_or_buf, encoding='iso-8859-1'):
833833
self._missing_values = False
834834
self._data_read = False
835835
self._value_labels_read = False
836+
self._native_byteorder = _set_endianness(sys.byteorder)
836837
if isinstance(path_or_buf, str):
837838
path_or_buf, encoding = get_filepath_or_buffer(
838839
path_or_buf, encoding=self._default_encoding
@@ -1195,13 +1196,16 @@ def data(self, convert_dates=True, convert_categoricals=True, index=None,
11951196
dtype = [] # Convert struct data types to numpy data type
11961197
for i, typ in enumerate(self.typlist):
11971198
if typ in self.NUMPY_TYPE_MAP:
1198-
dtype.append(('s' + str(i), self.NUMPY_TYPE_MAP[typ]))
1199+
dtype.append(('s' + str(i), self.byteorder + self.NUMPY_TYPE_MAP[typ]))
11991200
else:
12001201
dtype.append(('s' + str(i), 'S' + str(typ)))
12011202
dtype = np.dtype(dtype)
12021203
read_len = count * dtype.itemsize
12031204
self.path_or_buf.seek(self.data_location)
12041205
data = np.frombuffer(self.path_or_buf.read(read_len),dtype=dtype,count=count)
1206+
# if necessary, swap the byte order to native here
1207+
if self.byteorder != self._native_byteorder:
1208+
data = data.byteswap().newbyteorder()
12051209
self._data_read = True
12061210

12071211
if convert_categoricals:

0 commit comments

Comments
 (0)