MCPcopy Create free account
hub / github.com/dask/dask / ResetIndex

Class ResetIndex

dask/dataframe/dask_expr/_expr.py:2379–2444  ·  view source on GitHub ↗

Reset the index of a Series or DataFrame

Source from the content-addressed store, hash-verified

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

Callers 3

_leftMethod · 0.90
_lowerMethod · 0.90
_lowerMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected