Test that eval works with N-dimensional data where N > 2.
()
| 58 | |
| 59 | |
| 60 | def test_eval_ndimensional() -> None: |
| 61 | """Test that eval works with N-dimensional data where N > 2.""" |
| 62 | # Create a 3D dataset - this previously failed with pd.eval |
| 63 | rng = np.random.default_rng(42) |
| 64 | ds = Dataset( |
| 65 | { |
| 66 | "x": (["time", "lat", "lon"], rng.random((3, 4, 5))), |
| 67 | "y": (["time", "lat", "lon"], rng.random((3, 4, 5))), |
| 68 | } |
| 69 | ) |
| 70 | |
| 71 | # Basic arithmetic |
| 72 | actual = ds.eval("x + y") |
| 73 | expect = ds["x"] + ds["y"] |
| 74 | assert_identical(expect, actual) |
| 75 | |
| 76 | # Assignment |
| 77 | actual = ds.eval("z = x + y") |
| 78 | assert "z" in actual.data_vars |
| 79 | assert_equal(ds["x"] + ds["y"], actual["z"]) |
| 80 | |
| 81 | # Complex expression |
| 82 | actual = ds.eval("x * 2 + y ** 2") |
| 83 | expect = ds["x"] * 2 + ds["y"] ** 2 |
| 84 | assert_identical(expect, actual) |
| 85 | |
| 86 | # Comparison |
| 87 | actual = ds.eval("x > y") |
| 88 | expect = ds["x"] > ds["y"] |
| 89 | assert_identical(expect, actual) |
| 90 | |
| 91 | # Use bitwise operators for element-wise boolean operations |
| 92 | actual = ds.eval("(x > 0.5) & (y < 0.5)") |
| 93 | expect = (ds["x"] > 0.5) & (ds["y"] < 0.5) |
| 94 | assert_identical(expect, actual) |
| 95 | |
| 96 | |
| 97 | def test_eval_chained_comparisons() -> None: |
nothing calls this directly
no test coverage detected
searching dependent graphs…