Skip to content
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

PERF: use new to_records() argument in to_stata() #25045

Merged
merged 1 commit into from
Feb 1, 2019
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
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