Split-by-hash a DataFrame into `nsplits` groups. Hashing will be performed on the columns or index specified by `on`.
(df, on, nsplits)
| 294 | |
| 295 | |
| 296 | def _split_partition(df, on, nsplits): |
| 297 | """ |
| 298 | Split-by-hash a DataFrame into `nsplits` groups. |
| 299 | |
| 300 | Hashing will be performed on the columns or index specified by `on`. |
| 301 | """ |
| 302 | |
| 303 | if isinstance(on, bytes): |
| 304 | on = pickle.loads(on) |
| 305 | |
| 306 | if isinstance(on, str) or pd.api.types.is_list_like(on): |
| 307 | # If `on` is a column name or list of column names, we |
| 308 | # can hash/split by those columns. |
| 309 | on = [on] if isinstance(on, str) else list(on) |
| 310 | nset = set(on) |
| 311 | if nset.intersection(set(df.columns)) == nset: |
| 312 | o = df[on] |
| 313 | dtypes = {} |
| 314 | for col, dtype in o.dtypes.items(): |
| 315 | if pd.api.types.is_numeric_dtype(dtype): |
| 316 | dtypes[col] = np.float64 |
| 317 | if not dtypes: |
| 318 | ind = hash_object_dispatch(df[on], index=False) |
| 319 | else: |
| 320 | ind = hash_object_dispatch(df[on].astype(dtypes), index=False) |
| 321 | |
| 322 | ind = ind % nsplits |
| 323 | return group_split_dispatch(df, ind, nsplits, ignore_index=False) |
| 324 | |
| 325 | # We are not joining (purely) on columns. Need to |
| 326 | # add a "_partitions" column to perform the split. |
| 327 | from dask.dataframe.dask_expr._collection import FrameBase |
| 328 | |
| 329 | if not isinstance(on, FrameBase): |
| 330 | on = _select_columns_or_index(df, on) |
| 331 | |
| 332 | dtypes = {} |
| 333 | for col, dtype in on.dtypes.items(): |
| 334 | if pd.api.types.is_numeric_dtype(dtype): |
| 335 | dtypes[col] = np.float64 |
| 336 | if not dtypes: |
| 337 | dtypes = None |
| 338 | |
| 339 | partitions = partitioning_index(on, nsplits, cast_dtype=dtypes) |
| 340 | df2 = df.assign(_partitions=partitions) |
| 341 | return shuffle_group( |
| 342 | df2, |
| 343 | ["_partitions"], |
| 344 | 0, |
| 345 | nsplits, |
| 346 | nsplits, |
| 347 | False, |
| 348 | nsplits, |
| 349 | ) |
| 350 | |
| 351 | |
| 352 | def _concat_wrapper(dfs): |
nothing calls this directly
no test coverage detected