Skip to content

Commit cc3cb3d

Browse files
lukemanleyphofl
authored andcommitted
PERF: get_dummies (pandas-dev#56089)
* improve perf of get_dummies * whatsnew * mypy
1 parent e4a710f commit cc3cb3d

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ Performance improvements
319319
~~~~~~~~~~~~~~~~~~~~~~~~
320320
- Performance improvement in :func:`.testing.assert_frame_equal` and :func:`.testing.assert_series_equal` (:issue:`55949`, :issue:`55971`)
321321
- Performance improvement in :func:`concat` with ``axis=1`` and objects with unaligned indexes (:issue:`55084`)
322+
- Performance improvement in :func:`get_dummies` (:issue:`56089`)
322323
- Performance improvement in :func:`merge_asof` when ``by`` is not ``None`` (:issue:`55580`, :issue:`55678`)
323324
- Performance improvement in :func:`read_stata` for files with many variables (:issue:`55515`)
324325
- Performance improvement in :func:`to_dict` on converting DataFrame to dictionary (:issue:`50990`)

pandas/core/reshape/encoding.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,15 @@ def get_empty_frame(data) -> DataFrame:
321321
return concat(sparse_series, axis=1, copy=False)
322322

323323
else:
324-
# take on axis=1 + transpose to ensure ndarray layout is column-major
325-
eye_dtype: NpDtype
324+
# ensure ndarray layout is column-major
325+
shape = len(codes), number_of_cols
326+
dummy_dtype: NpDtype
326327
if isinstance(_dtype, np.dtype):
327-
eye_dtype = _dtype
328+
dummy_dtype = _dtype
328329
else:
329-
eye_dtype = np.bool_
330-
dummy_mat = np.eye(number_of_cols, dtype=eye_dtype).take(codes, axis=1).T
330+
dummy_dtype = np.bool_
331+
dummy_mat = np.zeros(shape=shape, dtype=dummy_dtype, order="F")
332+
dummy_mat[np.arange(len(codes)), codes] = 1
331333

332334
if not dummy_na:
333335
# reset NaN GH4446

0 commit comments

Comments
 (0)