Skip to content

Commit 98d8065

Browse files
authored
REF: insert self.on column _after_ concat (#35746)
1 parent 8cbf46b commit 98d8065

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

pandas/core/window/rolling.py

+21-29
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from pandas.core.base import DataError, PandasObject, SelectionMixin, ShallowMixin
3939
import pandas.core.common as com
4040
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
4242
from pandas.core.util.numba_ import NUMBA_FUNC_CACHE, maybe_use_numba
4343
from pandas.core.window.common import (
4444
WindowGroupByMixin,
@@ -402,36 +402,27 @@ def _wrap_results(self, results, blocks, obj, exclude=None) -> FrameOrSeries:
402402
return result
403403
final.append(result)
404404

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")
428411

429-
if not columns:
430-
raise DataError("No numeric types to aggregate")
412+
df = concat(final, axis=1).reindex(columns=columns, copy=False)
431413

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
435426

436427
def _center_window(self, result, window) -> np.ndarray:
437428
"""
@@ -2277,6 +2268,7 @@ def _get_window_indexer(self, window: int) -> GroupbyRollingIndexer:
22772268
if isinstance(self.window, BaseIndexer):
22782269
rolling_indexer = type(self.window)
22792270
indexer_kwargs = self.window.__dict__
2271+
assert isinstance(indexer_kwargs, dict)
22802272
# We'll be using the index of each group later
22812273
indexer_kwargs.pop("index_array", None)
22822274
elif self.is_freq_type:

0 commit comments

Comments
 (0)