|
7 | 7 |
|
8 | 8 | import numpy as np
|
9 | 9 |
|
10 |
| -from pandas.types.common import _ensure_platform_int, is_list_like |
| 10 | +from pandas.types.common import (_ensure_platform_int, |
| 11 | + is_list_like, is_bool_dtype, |
| 12 | + needs_i8_conversion) |
11 | 13 | from pandas.types.cast import _maybe_promote
|
12 | 14 | from pandas.types.missing import notnull
|
13 | 15 | import pandas.types.concat as _concat
|
|
25 | 27 |
|
26 | 28 | import pandas.core.algorithms as algos
|
27 | 29 | import pandas.algos as _algos
|
| 30 | +import pandas._reshape as _reshape |
28 | 31 |
|
29 | 32 | from pandas.core.index import MultiIndex, _get_na_value
|
30 | 33 |
|
@@ -193,14 +196,32 @@ def get_new_values(self):
|
193 | 196 | new_values.fill(fill_value)
|
194 | 197 |
|
195 | 198 | new_mask = np.zeros(result_shape, dtype=bool)
|
196 |
| - |
197 |
| - # is there a simpler / faster way of doing this? |
198 |
| - for i in range(values.shape[1]): |
199 |
| - chunk = new_values[:, i * width:(i + 1) * width] |
200 |
| - mask_chunk = new_mask[:, i * width:(i + 1) * width] |
201 |
| - |
202 |
| - chunk.flat[self.mask] = self.sorted_values[:, i] |
203 |
| - mask_chunk.flat[self.mask] = True |
| 199 | + new_mask = new_mask.view('u1') |
| 200 | + |
| 201 | + name = np.dtype(dtype).name |
| 202 | + sorted_values = self.sorted_values |
| 203 | + if needs_i8_conversion(values): |
| 204 | + sorted_values = sorted_values.view('i8') |
| 205 | + new_values = new_values.view('i8') |
| 206 | + name = 'int64' |
| 207 | + elif is_bool_dtype(values): |
| 208 | + name = 'object' |
| 209 | + sorted_values = sorted_values.astype('object') |
| 210 | + new_values = new_values.astype('object') |
| 211 | + else: |
| 212 | + sorted_values = sorted_values.astype(name, copy=False) |
| 213 | + |
| 214 | + f = getattr(_reshape, "reshape_{}".format(name)) |
| 215 | + f(sorted_values, |
| 216 | + self.mask.view('u1'), |
| 217 | + stride, |
| 218 | + length, |
| 219 | + width, |
| 220 | + new_values, |
| 221 | + new_mask) |
| 222 | + |
| 223 | + if needs_i8_conversion(values): |
| 224 | + new_values = new_values.view(values.dtype) |
204 | 225 |
|
205 | 226 | return new_values, new_mask
|
206 | 227 |
|
|
0 commit comments