|
21 | 21 | npt,
|
22 | 22 | type_t,
|
23 | 23 | )
|
| 24 | +from pandas.compat.numpy import function as nv |
24 | 25 |
|
25 | 26 | from pandas.core.dtypes.common import (
|
26 | 27 | is_bool_dtype,
|
@@ -245,10 +246,8 @@ def coerce_to_array(
|
245 | 246 | if mask_values is not None:
|
246 | 247 | mask = mask | mask_values
|
247 | 248 |
|
248 |
| - if values.ndim != 1: |
249 |
| - raise ValueError("values must be a 1D list-like") |
250 |
| - if mask.ndim != 1: |
251 |
| - raise ValueError("mask must be a 1D list-like") |
| 249 | + if values.shape != mask.shape: |
| 250 | + raise ValueError("values.shape and mask.shape must match") |
252 | 251 |
|
253 | 252 | return values, mask
|
254 | 253 |
|
@@ -447,6 +446,144 @@ def _values_for_argsort(self) -> np.ndarray:
|
447 | 446 | data[self._mask] = -1
|
448 | 447 | return data
|
449 | 448 |
|
| 449 | + def any(self, *, skipna: bool = True, axis: int | None = 0, **kwargs): |
| 450 | + """ |
| 451 | + Return whether any element is True. |
| 452 | +
|
| 453 | + Returns False unless there is at least one element that is True. |
| 454 | + By default, NAs are skipped. If ``skipna=False`` is specified and |
| 455 | + missing values are present, similar :ref:`Kleene logic <boolean.kleene>` |
| 456 | + is used as for logical operations. |
| 457 | +
|
| 458 | + Parameters |
| 459 | + ---------- |
| 460 | + skipna : bool, default True |
| 461 | + Exclude NA values. If the entire array is NA and `skipna` is |
| 462 | + True, then the result will be False, as for an empty array. |
| 463 | + If `skipna` is False, the result will still be True if there is |
| 464 | + at least one element that is True, otherwise NA will be returned |
| 465 | + if there are NA's present. |
| 466 | + axis : int or None, default 0 |
| 467 | + **kwargs : any, default None |
| 468 | + Additional keywords have no effect but might be accepted for |
| 469 | + compatibility with NumPy. |
| 470 | +
|
| 471 | + Returns |
| 472 | + ------- |
| 473 | + bool or :attr:`pandas.NA` |
| 474 | +
|
| 475 | + See Also |
| 476 | + -------- |
| 477 | + numpy.any : Numpy version of this method. |
| 478 | + BooleanArray.all : Return whether all elements are True. |
| 479 | +
|
| 480 | + Examples |
| 481 | + -------- |
| 482 | + The result indicates whether any element is True (and by default |
| 483 | + skips NAs): |
| 484 | +
|
| 485 | + >>> pd.array([True, False, True]).any() |
| 486 | + True |
| 487 | + >>> pd.array([True, False, pd.NA]).any() |
| 488 | + True |
| 489 | + >>> pd.array([False, False, pd.NA]).any() |
| 490 | + False |
| 491 | + >>> pd.array([], dtype="boolean").any() |
| 492 | + False |
| 493 | + >>> pd.array([pd.NA], dtype="boolean").any() |
| 494 | + False |
| 495 | +
|
| 496 | + With ``skipna=False``, the result can be NA if this is logically |
| 497 | + required (whether ``pd.NA`` is True or False influences the result): |
| 498 | +
|
| 499 | + >>> pd.array([True, False, pd.NA]).any(skipna=False) |
| 500 | + True |
| 501 | + >>> pd.array([False, False, pd.NA]).any(skipna=False) |
| 502 | + <NA> |
| 503 | + """ |
| 504 | + kwargs.pop("axis", None) |
| 505 | + nv.validate_any((), kwargs) |
| 506 | + |
| 507 | + values = self._data.copy() |
| 508 | + np.putmask(values, self._mask, False) |
| 509 | + result = values.any(axis=axis) |
| 510 | + |
| 511 | + if skipna: |
| 512 | + return result |
| 513 | + else: |
| 514 | + if result or self.size == 0 or not self._mask.any(): |
| 515 | + return result |
| 516 | + else: |
| 517 | + return self.dtype.na_value |
| 518 | + |
| 519 | + def all(self, *, skipna: bool = True, axis: int | None = 0, **kwargs): |
| 520 | + """ |
| 521 | + Return whether all elements are True. |
| 522 | +
|
| 523 | + Returns True unless there is at least one element that is False. |
| 524 | + By default, NAs are skipped. If ``skipna=False`` is specified and |
| 525 | + missing values are present, similar :ref:`Kleene logic <boolean.kleene>` |
| 526 | + is used as for logical operations. |
| 527 | +
|
| 528 | + Parameters |
| 529 | + ---------- |
| 530 | + skipna : bool, default True |
| 531 | + Exclude NA values. If the entire array is NA and `skipna` is |
| 532 | + True, then the result will be True, as for an empty array. |
| 533 | + If `skipna` is False, the result will still be False if there is |
| 534 | + at least one element that is False, otherwise NA will be returned |
| 535 | + if there are NA's present. |
| 536 | + axis : int or None, default 0 |
| 537 | + **kwargs : any, default None |
| 538 | + Additional keywords have no effect but might be accepted for |
| 539 | + compatibility with NumPy. |
| 540 | +
|
| 541 | + Returns |
| 542 | + ------- |
| 543 | + bool or :attr:`pandas.NA` |
| 544 | +
|
| 545 | + See Also |
| 546 | + -------- |
| 547 | + numpy.all : Numpy version of this method. |
| 548 | + BooleanArray.any : Return whether any element is True. |
| 549 | +
|
| 550 | + Examples |
| 551 | + -------- |
| 552 | + The result indicates whether any element is True (and by default |
| 553 | + skips NAs): |
| 554 | +
|
| 555 | + >>> pd.array([True, True, pd.NA]).all() |
| 556 | + True |
| 557 | + >>> pd.array([True, False, pd.NA]).all() |
| 558 | + False |
| 559 | + >>> pd.array([], dtype="boolean").all() |
| 560 | + True |
| 561 | + >>> pd.array([pd.NA], dtype="boolean").all() |
| 562 | + True |
| 563 | +
|
| 564 | + With ``skipna=False``, the result can be NA if this is logically |
| 565 | + required (whether ``pd.NA`` is True or False influences the result): |
| 566 | +
|
| 567 | + >>> pd.array([True, True, pd.NA]).all(skipna=False) |
| 568 | + <NA> |
| 569 | + >>> pd.array([True, False, pd.NA]).all(skipna=False) |
| 570 | + False |
| 571 | + """ |
| 572 | + kwargs.pop("axis", None) |
| 573 | + nv.validate_all((), kwargs) |
| 574 | + |
| 575 | + values = self._data.copy() |
| 576 | + np.putmask(values, self._mask, True) |
| 577 | + result = values.all(axis=axis) |
| 578 | + |
| 579 | + if skipna: |
| 580 | + return result |
| 581 | + else: |
| 582 | + if not result or self.size == 0 or not self._mask.any(): |
| 583 | + return result |
| 584 | + else: |
| 585 | + return self.dtype.na_value |
| 586 | + |
450 | 587 | def _logical_method(self, other, op):
|
451 | 588 |
|
452 | 589 | assert op.__name__ in {"or_", "ror_", "and_", "rand_", "xor", "rxor"}
|
|
0 commit comments