|
38 | 38 | from pandas.core.base import DataError, PandasObject, SelectionMixin, ShallowMixin |
39 | 39 | import pandas.core.common as com |
40 | 40 | from pandas.core.construction import extract_array |
41 | | -from pandas.core.indexes.api import Index, MultiIndex, ensure_index |
| 41 | +from pandas.core.indexes.api import Index, MultiIndex |
42 | 42 | from pandas.core.util.numba_ import NUMBA_FUNC_CACHE, maybe_use_numba |
43 | 43 | from pandas.core.window.common import ( |
44 | 44 | WindowGroupByMixin, |
@@ -402,36 +402,27 @@ def _wrap_results(self, results, blocks, obj, exclude=None) -> FrameOrSeries: |
402 | 402 | return result |
403 | 403 | final.append(result) |
404 | 404 |
|
405 | | - # if we have an 'on' column |
406 | | - # we want to put it back into the results |
407 | | - # in the same location |
408 | | - columns = self._selected_obj.columns |
409 | | - if self.on is not None and not self._on.equals(obj.index): |
410 | | - |
411 | | - name = self._on.name |
412 | | - final.append(Series(self._on, index=obj.index, name=name)) |
413 | | - |
414 | | - if self._selection is not None: |
415 | | - |
416 | | - selection = ensure_index(self._selection) |
417 | | - |
418 | | - # need to reorder to include original location of |
419 | | - # the on column (if its not already there) |
420 | | - if name not in selection: |
421 | | - columns = self.obj.columns |
422 | | - indexer = columns.get_indexer(selection.tolist() + [name]) |
423 | | - columns = columns.take(sorted(indexer)) |
424 | | - |
425 | | - # exclude nuisance columns so that they are not reindexed |
426 | | - if exclude is not None and exclude: |
427 | | - columns = [c for c in columns if c not in exclude] |
| 405 | + exclude = exclude or [] |
| 406 | + columns = [c for c in self._selected_obj.columns if c not in exclude] |
| 407 | + if not columns and not len(final) and exclude: |
| 408 | + raise DataError("No numeric types to aggregate") |
| 409 | + elif not len(final): |
| 410 | + return obj.astype("float64") |
428 | 411 |
|
429 | | - if not columns: |
430 | | - raise DataError("No numeric types to aggregate") |
| 412 | + df = concat(final, axis=1).reindex(columns=columns, copy=False) |
431 | 413 |
|
432 | | - if not len(final): |
433 | | - return obj.astype("float64") |
434 | | - return concat(final, axis=1).reindex(columns=columns, copy=False) |
| 414 | + # if we have an 'on' column we want to put it back into |
| 415 | + # the results in the same location |
| 416 | + if self.on is not None and not self._on.equals(obj.index): |
| 417 | + name = self._on.name |
| 418 | + extra_col = Series(self._on, index=obj.index, name=name) |
| 419 | + if name not in df.columns and name not in df.index.names: |
| 420 | + new_loc = len(df.columns) |
| 421 | + df.insert(new_loc, name, extra_col) |
| 422 | + elif name in df.columns: |
| 423 | + # TODO: sure we want to overwrite results? |
| 424 | + df[name] = extra_col |
| 425 | + return df |
435 | 426 |
|
436 | 427 | def _center_window(self, result, window) -> np.ndarray: |
437 | 428 | """ |
@@ -2277,6 +2268,7 @@ def _get_window_indexer(self, window: int) -> GroupbyRollingIndexer: |
2277 | 2268 | if isinstance(self.window, BaseIndexer): |
2278 | 2269 | rolling_indexer = type(self.window) |
2279 | 2270 | indexer_kwargs = self.window.__dict__ |
| 2271 | + assert isinstance(indexer_kwargs, dict) |
2280 | 2272 | # We'll be using the index of each group later |
2281 | 2273 | indexer_kwargs.pop("index_array", None) |
2282 | 2274 | elif self.is_freq_type: |
|
0 commit comments