Skip to content

Commit 120ec7f

Browse files
authored
fix: Raise on invalid shape dataframe arithmetic (#17322)
1 parent ccdfbda commit 120ec7f

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

crates/polars-core/src/frame/arithmetic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl DataFrame {
145145
let df = if col_len < self.width() { self } else { other };
146146

147147
for i in col_len..max_len {
148-
let s = &df.get_columns()[i];
148+
let s = &df.get_columns().get(i).ok_or_else(|| polars_err!(InvalidOperation: "cannot do arithmetic on DataFrames with shapes: {:?} and {:?}", self.shape(), other.shape()))?;
149149
let name = s.name();
150150
let dtype = s.dtype();
151151

py-polars/tests/unit/operations/arithmetic/test_arithmetic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,3 +760,8 @@ def test_date_datetime_sub() -> None:
760760
"foo": [timedelta(days=-4)],
761761
"bar": [timedelta(days=4)],
762762
}
763+
764+
765+
def test_raise_invalid_shape() -> None:
766+
with pytest.raises(pl.exceptions.InvalidOperationError):
767+
pl.DataFrame([[1, 2], [3, 4]]) * pl.DataFrame([1, 2, 3])

0 commit comments

Comments
 (0)