|
1 | 1 | import numpy as np
|
2 | 2 | import pytest
|
3 | 3 |
|
| 4 | +import pandas.util._test_decorators as td |
| 5 | + |
4 | 6 | import pandas as pd
|
5 | 7 | from pandas import DataFrame, Index, MultiIndex, Series
|
6 | 8 | import pandas._testing as tm
|
@@ -560,16 +562,123 @@ def strech(row):
|
560 | 562 | assert not isinstance(result, tm.SubclassedDataFrame)
|
561 | 563 | tm.assert_series_equal(result, expected)
|
562 | 564 |
|
563 |
| - def test_subclassed_numeric_reductions(self, all_numeric_reductions): |
| 565 | + def test_subclassed_reductions(self, all_reductions): |
564 | 566 | # GH 25596
|
565 | 567 |
|
566 | 568 | df = tm.SubclassedDataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
|
567 |
| - result = getattr(df, all_numeric_reductions)() |
| 569 | + result = getattr(df, all_reductions)() |
568 | 570 | assert isinstance(result, tm.SubclassedSeries)
|
569 | 571 |
|
570 |
| - def test_subclassed_boolean_reductions(self, all_boolean_reductions): |
571 |
| - # GH 25596 |
| 572 | + def test_subclassed_count(self): |
| 573 | + |
| 574 | + df = tm.SubclassedDataFrame( |
| 575 | + { |
| 576 | + "Person": ["John", "Myla", "Lewis", "John", "Myla"], |
| 577 | + "Age": [24.0, np.nan, 21.0, 33, 26], |
| 578 | + "Single": [False, True, True, True, False], |
| 579 | + } |
| 580 | + ) |
| 581 | + result = df.count() |
| 582 | + assert isinstance(result, tm.SubclassedSeries) |
| 583 | + |
| 584 | + df = tm.SubclassedDataFrame({"A": [1, 0, 3], "B": [0, 5, 6], "C": [7, 8, 0]}) |
| 585 | + result = df.count() |
| 586 | + assert isinstance(result, tm.SubclassedSeries) |
| 587 | + |
| 588 | + df = tm.SubclassedDataFrame( |
| 589 | + [[10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33], [40, 41, 42, 43]], |
| 590 | + index=MultiIndex.from_tuples( |
| 591 | + list(zip(list("AABB"), list("cdcd"))), names=["aaa", "ccc"] |
| 592 | + ), |
| 593 | + columns=MultiIndex.from_tuples( |
| 594 | + list(zip(list("WWXX"), list("yzyz"))), names=["www", "yyy"] |
| 595 | + ), |
| 596 | + ) |
| 597 | + result = df.count(level=1) |
| 598 | + assert isinstance(result, tm.SubclassedDataFrame) |
| 599 | + |
| 600 | + df = tm.SubclassedDataFrame() |
| 601 | + result = df.count() |
| 602 | + assert isinstance(result, tm.SubclassedSeries) |
| 603 | + |
| 604 | + def test_isin(self): |
| 605 | + |
| 606 | + df = tm.SubclassedDataFrame( |
| 607 | + {"num_legs": [2, 4], "num_wings": [2, 0]}, index=["falcon", "dog"] |
| 608 | + ) |
| 609 | + result = df.isin([0, 2]) |
| 610 | + assert isinstance(result, tm.SubclassedDataFrame) |
| 611 | + |
| 612 | + def test_duplicated(self): |
572 | 613 |
|
573 | 614 | df = tm.SubclassedDataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
|
574 |
| - result = getattr(df, all_boolean_reductions)() |
| 615 | + result = df.duplicated() |
| 616 | + assert isinstance(result, tm.SubclassedSeries) |
| 617 | + |
| 618 | + df = tm.SubclassedDataFrame() |
| 619 | + result = df.duplicated() |
| 620 | + assert isinstance(result, tm.SubclassedSeries) |
| 621 | + |
| 622 | + @pytest.mark.parametrize("idx_method", ["idxmax", "idxmin"]) |
| 623 | + def test_idx(self, idx_method): |
| 624 | + |
| 625 | + df = tm.SubclassedDataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}) |
| 626 | + result = getattr(df, idx_method)() |
| 627 | + assert isinstance(result, tm.SubclassedSeries) |
| 628 | + |
| 629 | + def test_dot(self): |
| 630 | + |
| 631 | + df = tm.SubclassedDataFrame([[0, 1, -2, -1], [1, 1, 1, 1]]) |
| 632 | + s = tm.SubclassedSeries([1, 1, 2, 1]) |
| 633 | + result = df.dot(s) |
| 634 | + assert isinstance(result, tm.SubclassedSeries) |
| 635 | + |
| 636 | + df = tm.SubclassedDataFrame([[0, 1, -2, -1], [1, 1, 1, 1]]) |
| 637 | + s = tm.SubclassedDataFrame([1, 1, 2, 1]) |
| 638 | + result = df.dot(s) |
| 639 | + assert isinstance(result, tm.SubclassedDataFrame) |
| 640 | + |
| 641 | + def test_memory_usage(self): |
| 642 | + |
| 643 | + df = tm.SubclassedDataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}) |
| 644 | + result = df.memory_usage() |
| 645 | + assert isinstance(result, tm.SubclassedSeries) |
| 646 | + |
| 647 | + result = df.memory_usage(index=False) |
| 648 | + assert isinstance(result, tm.SubclassedSeries) |
| 649 | + |
| 650 | + @td.skip_if_no_scipy |
| 651 | + def test_corrwith(self): |
| 652 | + index = ["a", "b", "c", "d", "e"] |
| 653 | + columns = ["one", "two", "three", "four"] |
| 654 | + df1 = tm.SubclassedDataFrame( |
| 655 | + np.random.randn(5, 4), index=index, columns=columns |
| 656 | + ) |
| 657 | + df2 = tm.SubclassedDataFrame( |
| 658 | + np.random.randn(4, 4), index=index[:4], columns=columns |
| 659 | + ) |
| 660 | + correls = df1.corrwith(df2, axis=1, drop=True, method="kendall") |
| 661 | + |
| 662 | + assert isinstance(correls, (tm.SubclassedSeries)) |
| 663 | + |
| 664 | + def test_asof(self): |
| 665 | + |
| 666 | + N = 3 |
| 667 | + rng = pd.date_range("1/1/1990", periods=N, freq="53s") |
| 668 | + df = tm.SubclassedDataFrame( |
| 669 | + { |
| 670 | + "A": [np.nan, np.nan, np.nan], |
| 671 | + "B": [np.nan, np.nan, np.nan], |
| 672 | + "C": [np.nan, np.nan, np.nan], |
| 673 | + }, |
| 674 | + index=rng, |
| 675 | + ) |
| 676 | + |
| 677 | + result = df.asof(rng[-2:]) |
| 678 | + assert isinstance(result, tm.SubclassedDataFrame) |
| 679 | + |
| 680 | + result = df.asof(rng[-2]) |
| 681 | + assert isinstance(result, tm.SubclassedSeries) |
| 682 | + |
| 683 | + result = df.asof("1989-12-31") |
575 | 684 | assert isinstance(result, tm.SubclassedSeries)
|
0 commit comments