|
20 | 20 | Sequence,
|
21 | 21 | Tuple,
|
22 | 22 | Union,
|
23 |
| - cast, |
24 | 23 | )
|
25 | 24 |
|
26 | 25 | from pandas._typing import (
|
27 | 26 | AggFuncType,
|
28 |
| - AggFuncTypeBase, |
29 |
| - AggFuncTypeDict, |
30 |
| - Axis, |
31 | 27 | FrameOrSeries,
|
32 |
| - FrameOrSeriesUnion, |
33 | 28 | )
|
34 | 29 |
|
35 | 30 | from pandas.core.dtypes.common import (
|
36 | 31 | is_dict_like,
|
37 | 32 | is_list_like,
|
38 | 33 | )
|
39 |
| -from pandas.core.dtypes.generic import ( |
40 |
| - ABCDataFrame, |
41 |
| - ABCSeries, |
42 |
| -) |
| 34 | +from pandas.core.dtypes.generic import ABCSeries |
43 | 35 |
|
44 |
| -from pandas.core.algorithms import safe_sort |
45 | 36 | from pandas.core.base import SpecificationError
|
46 | 37 | import pandas.core.common as com
|
47 | 38 | from pandas.core.indexes.api import Index
|
@@ -405,134 +396,3 @@ def validate_func_kwargs(
|
405 | 396 | no_arg_message = "Must provide 'func' or named aggregation **kwargs."
|
406 | 397 | raise TypeError(no_arg_message)
|
407 | 398 | return columns, func
|
408 |
| - |
409 |
| - |
410 |
| -def transform( |
411 |
| - obj: FrameOrSeries, func: AggFuncType, axis: Axis, *args, **kwargs |
412 |
| -) -> FrameOrSeriesUnion: |
413 |
| - """ |
414 |
| - Transform a DataFrame or Series |
415 |
| -
|
416 |
| - Parameters |
417 |
| - ---------- |
418 |
| - obj : DataFrame or Series |
419 |
| - Object to compute the transform on. |
420 |
| - func : string, function, list, or dictionary |
421 |
| - Function(s) to compute the transform with. |
422 |
| - axis : {0 or 'index', 1 or 'columns'} |
423 |
| - Axis along which the function is applied: |
424 |
| -
|
425 |
| - * 0 or 'index': apply function to each column. |
426 |
| - * 1 or 'columns': apply function to each row. |
427 |
| -
|
428 |
| - Returns |
429 |
| - ------- |
430 |
| - DataFrame or Series |
431 |
| - Result of applying ``func`` along the given axis of the |
432 |
| - Series or DataFrame. |
433 |
| -
|
434 |
| - Raises |
435 |
| - ------ |
436 |
| - ValueError |
437 |
| - If the transform function fails or does not transform. |
438 |
| - """ |
439 |
| - is_series = obj.ndim == 1 |
440 |
| - |
441 |
| - if obj._get_axis_number(axis) == 1: |
442 |
| - assert not is_series |
443 |
| - return transform(obj.T, func, 0, *args, **kwargs).T |
444 |
| - |
445 |
| - if is_list_like(func) and not is_dict_like(func): |
446 |
| - func = cast(List[AggFuncTypeBase], func) |
447 |
| - # Convert func equivalent dict |
448 |
| - if is_series: |
449 |
| - func = {com.get_callable_name(v) or v: v for v in func} |
450 |
| - else: |
451 |
| - func = {col: func for col in obj} |
452 |
| - |
453 |
| - if is_dict_like(func): |
454 |
| - func = cast(AggFuncTypeDict, func) |
455 |
| - return transform_dict_like(obj, func, *args, **kwargs) |
456 |
| - |
457 |
| - # func is either str or callable |
458 |
| - func = cast(AggFuncTypeBase, func) |
459 |
| - try: |
460 |
| - result = transform_str_or_callable(obj, func, *args, **kwargs) |
461 |
| - except Exception: |
462 |
| - raise ValueError("Transform function failed") |
463 |
| - |
464 |
| - # Functions that transform may return empty Series/DataFrame |
465 |
| - # when the dtype is not appropriate |
466 |
| - if isinstance(result, (ABCSeries, ABCDataFrame)) and result.empty and not obj.empty: |
467 |
| - raise ValueError("Transform function failed") |
468 |
| - if not isinstance(result, (ABCSeries, ABCDataFrame)) or not result.index.equals( |
469 |
| - obj.index |
470 |
| - ): |
471 |
| - raise ValueError("Function did not transform") |
472 |
| - |
473 |
| - return result |
474 |
| - |
475 |
| - |
476 |
| -def transform_dict_like( |
477 |
| - obj: FrameOrSeries, |
478 |
| - func: AggFuncTypeDict, |
479 |
| - *args, |
480 |
| - **kwargs, |
481 |
| -): |
482 |
| - """ |
483 |
| - Compute transform in the case of a dict-like func |
484 |
| - """ |
485 |
| - from pandas.core.reshape.concat import concat |
486 |
| - |
487 |
| - if len(func) == 0: |
488 |
| - raise ValueError("No transform functions were provided") |
489 |
| - |
490 |
| - if obj.ndim != 1: |
491 |
| - # Check for missing columns on a frame |
492 |
| - cols = set(func.keys()) - set(obj.columns) |
493 |
| - if len(cols) > 0: |
494 |
| - cols_sorted = list(safe_sort(list(cols))) |
495 |
| - raise SpecificationError(f"Column(s) {cols_sorted} do not exist") |
496 |
| - |
497 |
| - # Can't use func.values(); wouldn't work for a Series |
498 |
| - if any(is_dict_like(v) for _, v in func.items()): |
499 |
| - # GH 15931 - deprecation of renaming keys |
500 |
| - raise SpecificationError("nested renamer is not supported") |
501 |
| - |
502 |
| - results: Dict[Hashable, FrameOrSeriesUnion] = {} |
503 |
| - for name, how in func.items(): |
504 |
| - colg = obj._gotitem(name, ndim=1) |
505 |
| - try: |
506 |
| - results[name] = transform(colg, how, 0, *args, **kwargs) |
507 |
| - except Exception as err: |
508 |
| - if str(err) in { |
509 |
| - "Function did not transform", |
510 |
| - "No transform functions were provided", |
511 |
| - }: |
512 |
| - raise err |
513 |
| - |
514 |
| - # combine results |
515 |
| - if not results: |
516 |
| - raise ValueError("Transform function failed") |
517 |
| - return concat(results, axis=1) |
518 |
| - |
519 |
| - |
520 |
| -def transform_str_or_callable( |
521 |
| - obj: FrameOrSeries, func: AggFuncTypeBase, *args, **kwargs |
522 |
| -) -> FrameOrSeriesUnion: |
523 |
| - """ |
524 |
| - Compute transform in the case of a string or callable func |
525 |
| - """ |
526 |
| - if isinstance(func, str): |
527 |
| - return obj._try_aggregate_string_function(func, *args, **kwargs) |
528 |
| - |
529 |
| - if not args and not kwargs: |
530 |
| - f = obj._get_cython_func(func) |
531 |
| - if f: |
532 |
| - return getattr(obj, f)() |
533 |
| - |
534 |
| - # Two possible ways to use a UDF - apply or call directly |
535 |
| - try: |
536 |
| - return obj.apply(func, args=args, **kwargs) |
537 |
| - except Exception: |
538 |
| - return func(obj, *args, **kwargs) |
0 commit comments