Skip to content

Commit

Permalink
PERF: use new to_records() argument in to_stata()
Browse files Browse the repository at this point in the history
  • Loading branch information
qwhelan committed Jan 31, 2019
1 parent da5f5eb commit f8ebd68
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 deletions.
12 changes: 2 additions & 10 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ Other Enhancements
-
-

.. _whatsnew_0250.performance:

Performance Improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- Significant speedup in `SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0 (:issue:`24985`)



.. _whatsnew_0250.api_breaking:

Backwards incompatible API changes
Expand Down Expand Up @@ -69,8 +61,8 @@ Removal of prior version deprecations/changes
Performance Improvements
~~~~~~~~~~~~~~~~~~~~~~~~

-
-
- Significant speedup in `SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0 (:issue:`24985`)
- `DataFrame.to_stata()` is now faster when outputting data with any string or non-native endian columns (:issue:`25045`)
-


Expand Down
20 changes: 5 additions & 15 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2385,32 +2385,22 @@ def _prepare_data(self):
data = self._convert_strls(data)

# 3. Convert bad string data to '' and pad to correct length
dtypes = []
data_cols = []
has_strings = False
dtypes = {}
native_byteorder = self._byteorder == _set_endianness(sys.byteorder)
for i, col in enumerate(data):
typ = typlist[i]
if typ <= self._max_string_length:
has_strings = True
data[col] = data[col].fillna('').apply(_pad_bytes, args=(typ,))
stype = 'S{type}'.format(type=typ)
dtypes.append(('c' + str(i), stype))
string = data[col].str.encode(self._encoding)
data_cols.append(string.values.astype(stype))
dtypes[col] = stype
data[col] = data[col].str.encode(self._encoding).astype(stype)
else:
values = data[col].values
dtype = data[col].dtype
if not native_byteorder:
dtype = dtype.newbyteorder(self._byteorder)
dtypes.append(('c' + str(i), dtype))
data_cols.append(values)
dtypes = np.dtype(dtypes)
dtypes[col] = dtype

if has_strings or not native_byteorder:
self.data = np.fromiter(zip(*data_cols), dtype=dtypes)
else:
self.data = data.to_records(index=False)
self.data = data.to_records(index=False, column_dtypes=dtypes)

def _write_data(self):
data = self.data
Expand Down

0 comments on commit f8ebd68

Please sign in to comment.