Reset the index of a Series or DataFrame
| 2393 | |
| 2394 | |
| 2395 | class ResetIndex(Elemwise): |
| 2396 | """Reset the index of a Series or DataFrame""" |
| 2397 | |
| 2398 | _parameters = ["frame", "drop", "name"] |
| 2399 | _defaults = {"drop": False, "name": no_default} |
| 2400 | _keyword_only = ["drop", "name"] |
| 2401 | operation = M.reset_index |
| 2402 | _filter_passthrough = True |
| 2403 | _preserves_partitioning_information = True |
| 2404 | |
| 2405 | @functools.cached_property |
| 2406 | def _kwargs(self) -> dict: |
| 2407 | kwargs = {"drop": self.drop} |
| 2408 | if self.operand("name") is not no_default: |
| 2409 | kwargs.update({"name": self.operand("name")}) |
| 2410 | return kwargs |
| 2411 | |
| 2412 | def _divisions(self): |
| 2413 | return (None,) * (self.frame.npartitions + 1) |
| 2414 | |
| 2415 | def _simplify_up(self, parent, dependents): |
| 2416 | if isinstance(parent, Filter) and self._filter_passthrough_available( |
| 2417 | parent, dependents |
| 2418 | ): |
| 2419 | parents = [ |
| 2420 | p().columns |
| 2421 | for p in dependents[self._name] |
| 2422 | if p() is not None and not isinstance(p(), Filter) |
| 2423 | ] |
| 2424 | predicate = None |
| 2425 | if not set(flatten(parents, list)).issubset(set(self.frame.columns)): |
| 2426 | # one of the filters is the Index |
| 2427 | name = self.operand("name") or self.frame._meta.index.name |
| 2428 | if name is no_default and self.frame._meta.index.name is None: |
| 2429 | name = "index" |
| 2430 | elif self.frame._meta.index.name is not None: |
| 2431 | name = self.frame._meta.index.name |
| 2432 | # replace the projection of the former index with the actual index |
| 2433 | subs = Projection(self, name) |
| 2434 | predicate = parent.predicate.substitute(subs, Index(self.frame)) |
| 2435 | elif self.frame.ndim == 1 and not self.operand("drop"): |
| 2436 | name = self.frame._meta.name |
| 2437 | # Avoid Projection since we are already a Series |
| 2438 | subs = Projection(self, name) |
| 2439 | predicate = parent.predicate.substitute(subs, self.frame) |
| 2440 | return self._filter_simplification(parent, predicate) |
| 2441 | |
| 2442 | if isinstance(parent, Projection): |
| 2443 | if self.frame.ndim == 1 and not self.drop: |
| 2444 | if isinstance(parent.operand("columns"), list): |
| 2445 | # Don't bother, dimensionality changes are tricky here and |
| 2446 | # potential improvement is tiny |
| 2447 | return |
| 2448 | col = parent.operand("columns") |
| 2449 | if col in (self.name, "index", self.frame._meta.index.name): |
| 2450 | return |
| 2451 | if all( |
| 2452 | isinstance(d(), Projection) and d().operand("columns") == col |