|
21 | 21 | is_scalar,
|
22 | 22 | )
|
23 | 23 | from pandas.core.dtypes.dtypes import register_extension_dtype
|
24 |
| -from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries |
| 24 | +from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries |
25 | 25 | from pandas.core.dtypes.missing import isna, notna
|
26 | 26 |
|
27 | 27 | from pandas.core import nanops, ops
|
@@ -592,25 +592,29 @@ def _values_for_argsort(self) -> np.ndarray:
|
592 | 592 |
|
593 | 593 | @classmethod
|
594 | 594 | def _create_comparison_method(cls, op):
|
595 |
| - def cmp_method(self, other): |
| 595 | + op_name = op.__name__ |
596 | 596 |
|
597 |
| - op_name = op.__name__ |
598 |
| - mask = None |
| 597 | + def cmp_method(self, other): |
599 | 598 |
|
600 |
| - if isinstance(other, (ABCSeries, ABCIndexClass)): |
| 599 | + if isinstance(other, (ABCDataFrame, ABCSeries, ABCIndexClass)): |
601 | 600 | # Rely on pandas to unbox and dispatch to us.
|
602 | 601 | return NotImplemented
|
603 | 602 |
|
| 603 | + other = lib.item_from_zerodim(other) |
| 604 | + mask = None |
| 605 | + |
604 | 606 | if isinstance(other, IntegerArray):
|
605 | 607 | other, mask = other._data, other._mask
|
606 | 608 |
|
607 | 609 | elif is_list_like(other):
|
608 | 610 | other = np.asarray(other)
|
609 |
| - if other.ndim > 0 and len(self) != len(other): |
| 611 | + if other.ndim > 1: |
| 612 | + raise NotImplementedError( |
| 613 | + "can only perform ops with 1-d structures" |
| 614 | + ) |
| 615 | + if len(self) != len(other): |
610 | 616 | raise ValueError("Lengths must match to compare")
|
611 | 617 |
|
612 |
| - other = lib.item_from_zerodim(other) |
613 |
| - |
614 | 618 | # numpy will show a DeprecationWarning on invalid elementwise
|
615 | 619 | # comparisons, this will raise in the future
|
616 | 620 | with warnings.catch_warnings():
|
@@ -683,31 +687,31 @@ def _maybe_mask_result(self, result, mask, other, op_name):
|
683 | 687 |
|
684 | 688 | @classmethod
|
685 | 689 | def _create_arithmetic_method(cls, op):
|
686 |
| - def integer_arithmetic_method(self, other): |
| 690 | + op_name = op.__name__ |
687 | 691 |
|
688 |
| - op_name = op.__name__ |
689 |
| - mask = None |
| 692 | + def integer_arithmetic_method(self, other): |
690 | 693 |
|
691 |
| - if isinstance(other, (ABCSeries, ABCIndexClass)): |
| 694 | + if isinstance(other, (ABCDataFrame, ABCSeries, ABCIndexClass)): |
692 | 695 | # Rely on pandas to unbox and dispatch to us.
|
693 | 696 | return NotImplemented
|
694 | 697 |
|
695 |
| - if getattr(other, "ndim", 0) > 1: |
696 |
| - raise NotImplementedError("can only perform ops with 1-d structures") |
| 698 | + other = lib.item_from_zerodim(other) |
| 699 | + mask = None |
697 | 700 |
|
698 | 701 | if isinstance(other, IntegerArray):
|
699 | 702 | other, mask = other._data, other._mask
|
700 | 703 |
|
701 |
| - elif getattr(other, "ndim", None) == 0: |
702 |
| - other = other.item() |
703 |
| - |
704 | 704 | elif is_list_like(other):
|
705 | 705 | other = np.asarray(other)
|
706 |
| - if not other.ndim: |
707 |
| - other = other.item() |
708 |
| - elif other.ndim == 1: |
709 |
| - if not (is_float_dtype(other) or is_integer_dtype(other)): |
710 |
| - raise TypeError("can only perform ops with numeric values") |
| 706 | + if other.ndim > 1: |
| 707 | + raise NotImplementedError( |
| 708 | + "can only perform ops with 1-d structures" |
| 709 | + ) |
| 710 | + if len(self) != len(other): |
| 711 | + raise ValueError("Lengths must match") |
| 712 | + if not (is_float_dtype(other) or is_integer_dtype(other)): |
| 713 | + raise TypeError("can only perform ops with numeric values") |
| 714 | + |
711 | 715 | else:
|
712 | 716 | if not (is_float(other) or is_integer(other)):
|
713 | 717 | raise TypeError("can only perform ops with numeric values")
|
|
0 commit comments