(temp_hdfstore)
| 651 | |
| 652 | |
| 653 | def test_frame_select_complex(temp_hdfstore): |
| 654 | # select via complex criteria |
| 655 | |
| 656 | df = DataFrame( |
| 657 | np.random.default_rng(2).standard_normal((10, 4)), |
| 658 | columns=Index(list("ABCD")), |
| 659 | index=date_range("2000-01-01", periods=10, freq="B", unit="ns"), |
| 660 | ) |
| 661 | df["string"] = "foo" |
| 662 | df.loc[df.index[0:4], "string"] = "bar" |
| 663 | |
| 664 | temp_hdfstore.put("df", df, format="table", data_columns=["string"]) |
| 665 | |
| 666 | # empty |
| 667 | result = temp_hdfstore.select("df", 'index>df.index[3] & string="bar"') |
| 668 | expected = df.loc[(df.index > df.index[3]) & (df.string == "bar")] |
| 669 | tm.assert_frame_equal(result, expected) |
| 670 | |
| 671 | result = temp_hdfstore.select("df", 'index>df.index[3] & string="foo"') |
| 672 | expected = df.loc[(df.index > df.index[3]) & (df.string == "foo")] |
| 673 | tm.assert_frame_equal(result, expected) |
| 674 | |
| 675 | # or |
| 676 | result = temp_hdfstore.select("df", 'index>df.index[3] | string="bar"') |
| 677 | expected = df.loc[(df.index > df.index[3]) | (df.string == "bar")] |
| 678 | tm.assert_frame_equal(result, expected) |
| 679 | |
| 680 | result = temp_hdfstore.select( |
| 681 | "df", '(index>df.index[3] & index<=df.index[6]) | string="bar"' |
| 682 | ) |
| 683 | expected = df.loc[ |
| 684 | ((df.index > df.index[3]) & (df.index <= df.index[6])) | (df.string == "bar") |
| 685 | ] |
| 686 | tm.assert_frame_equal(result, expected) |
| 687 | |
| 688 | # invert |
| 689 | result = temp_hdfstore.select("df", 'string!="bar"') |
| 690 | expected = df.loc[df.string != "bar"] |
| 691 | tm.assert_frame_equal(result, expected) |
| 692 | |
| 693 | # invert not implemented in numexpr :( |
| 694 | msg = "cannot use an invert condition when passing to numexpr" |
| 695 | with pytest.raises(NotImplementedError, match=msg): |
| 696 | temp_hdfstore.select("df", '~(string="bar")') |
| 697 | |
| 698 | # invert ok for filters |
| 699 | result = temp_hdfstore.select("df", "~(columns=['A','B'])") |
| 700 | expected = df.loc[:, df.columns.difference(["A", "B"])] |
| 701 | tm.assert_frame_equal(result, expected) |
| 702 | |
| 703 | # in |
| 704 | result = temp_hdfstore.select("df", "index>df.index[3] & columns in ['A','B']") |
| 705 | expected = df.loc[df.index > df.index[3]].reindex(columns=["A", "B"]) |
| 706 | tm.assert_frame_equal(result, expected) |
| 707 | |
| 708 | |
| 709 | def test_frame_select_complex2(tmp_path): |
nothing calls this directly
no test coverage detected