Skip to content

Commit 85df94f

Browse files
committed
PERF: use new to_records() argument in to_stata()
1 parent 4cbee17 commit 85df94f

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

Diff for: pandas/io/stata.py

+5-12
Original file line numberDiff line numberDiff line change
@@ -2385,8 +2385,7 @@ def _prepare_data(self):
23852385
data = self._convert_strls(data)
23862386

23872387
# 3. Convert bad string data to '' and pad to correct length
2388-
dtypes = []
2389-
data_cols = []
2388+
dtypes = {}
23902389
has_strings = False
23912390
native_byteorder = self._byteorder == _set_endianness(sys.byteorder)
23922391
for i, col in enumerate(data):
@@ -2395,22 +2394,16 @@ def _prepare_data(self):
23952394
has_strings = True
23962395
data[col] = data[col].fillna('').apply(_pad_bytes, args=(typ,))
23972396
stype = 'S{type}'.format(type=typ)
2398-
dtypes.append(('c' + str(i), stype))
2399-
string = data[col].str.encode(self._encoding)
2400-
data_cols.append(string.values.astype(stype))
2397+
dtypes[col] = stype
2398+
data[col] = data[col].str.encode(self._encoding).astype(stype)
24012399
else:
24022400
values = data[col].values
24032401
dtype = data[col].dtype
24042402
if not native_byteorder:
24052403
dtype = dtype.newbyteorder(self._byteorder)
2406-
dtypes.append(('c' + str(i), dtype))
2407-
data_cols.append(values)
2408-
dtypes = np.dtype(dtypes)
2404+
dtypes[col] = dtype
24092405

2410-
if has_strings or not native_byteorder:
2411-
self.data = np.fromiter(zip(*data_cols), dtype=dtypes)
2412-
else:
2413-
self.data = data.to_records(index=False)
2406+
self.data = data.to_records(index=False, column_dtypes=dtypes)
24142407

24152408
def _write_data(self):
24162409
data = self.data

0 commit comments

Comments
 (0)