Set the DataFrame index (row labels) using an existing column. If ``sort=False``, this function operates exactly like ``pandas.set_index`` and sets the index on the DataFrame. If ``sort=True`` (default), this function also sorts the DataFrame by the new index. This can have
(
self,
other,
drop=True,
sorted=False,
npartitions: int | None = None,
divisions=None,
sort: bool = True,
shuffle_method=None,
upsample: float = 1.0,
partition_size: float = 128e6,
append: bool = False,
**options,
)
| 3334 | return new_collection(Eval(self, _expr=expr, expr_kwargs=kwargs)) |
| 3335 | |
| 3336 | def set_index( |
| 3337 | self, |
| 3338 | other, |
| 3339 | drop=True, |
| 3340 | sorted=False, |
| 3341 | npartitions: int | None = None, |
| 3342 | divisions=None, |
| 3343 | sort: bool = True, |
| 3344 | shuffle_method=None, |
| 3345 | upsample: float = 1.0, |
| 3346 | partition_size: float = 128e6, |
| 3347 | append: bool = False, |
| 3348 | **options, |
| 3349 | ): |
| 3350 | """Set the DataFrame index (row labels) using an existing column. |
| 3351 | |
| 3352 | If ``sort=False``, this function operates exactly like ``pandas.set_index`` |
| 3353 | and sets the index on the DataFrame. If ``sort=True`` (default), |
| 3354 | this function also sorts the DataFrame by the new index. This can have a |
| 3355 | significant impact on performance, because joins, groupbys, lookups, etc. |
| 3356 | are all much faster on that column. However, this performance increase |
| 3357 | comes with a cost, sorting a parallel dataset requires expensive shuffles. |
| 3358 | Often we ``set_index`` once directly after data ingest and filtering and |
| 3359 | then perform many cheap computations off of the sorted dataset. |
| 3360 | |
| 3361 | With ``sort=True``, this function is much more expensive. Under normal |
| 3362 | operation this function does an initial pass over the index column to |
| 3363 | compute approximate quantiles to serve as future divisions. It then passes |
| 3364 | over the data a second time, splitting up each input partition into several |
| 3365 | pieces and sharing those pieces to all of the output partitions now in |
| 3366 | sorted order. |
| 3367 | |
| 3368 | In some cases we can alleviate those costs, for example if your dataset is |
| 3369 | sorted already then we can avoid making many small pieces or if you know |
| 3370 | good values to split the new index column then we can avoid the initial |
| 3371 | pass over the data. For example if your new index is a datetime index and |
| 3372 | your data is already sorted by day then this entire operation can be done |
| 3373 | for free. You can control these options with the following parameters. |
| 3374 | |
| 3375 | Parameters |
| 3376 | ---------- |
| 3377 | other: string or Dask Series |
| 3378 | Column to use as index. |
| 3379 | drop: boolean, default True |
| 3380 | Delete column to be used as the new index. |
| 3381 | sorted: bool, optional |
| 3382 | If the index column is already sorted in increasing order. |
| 3383 | Defaults to False |
| 3384 | npartitions: int, None, or 'auto' |
| 3385 | The ideal number of output partitions. If None, use the same as |
| 3386 | the input. If 'auto' then decide by memory use. |
| 3387 | Only used when ``divisions`` is not given. If ``divisions`` is given, |
| 3388 | the number of output partitions will be ``len(divisions) - 1``. |
| 3389 | divisions: list, optional |
| 3390 | The "dividing lines" used to split the new index into partitions. |
| 3391 | For ``divisions=[0, 10, 50, 100]``, there would be three output partitions, |
| 3392 | where the new index contained [0, 10), [10, 50), and [50, 100), respectively. |
| 3393 | See https://docs.dask.org/en/latest/dataframe-design.html#partitions. |