Skip to content

Commit

Permalink
perf: Add special case to Table.add_rows to increase performance (#608
Browse files Browse the repository at this point in the history
)

See #606 

### Summary of Changes

perf: Add special case to `Table.add_rows` to increase performance

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
Co-authored-by: WinPlay02 <winplay02_gh@woberlaender.de>
  • Loading branch information
3 people committed Apr 5, 2024
1 parent e555b85 commit ffb8304
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ def add_rows(self, rows: list[Row] | Table) -> Table:
2 5 6
"""
if isinstance(rows, Table):
if rows.number_of_rows == 0:
if rows.number_of_columns == 0:
return self
if self.number_of_columns == 0:
return rows
Expand All @@ -1069,6 +1069,10 @@ def add_rows(self, rows: list[Row] | Table) -> Table:
key={val: ix for ix, val in enumerate(self.column_names)}.__getitem__,
),
)
if rows.number_of_rows == 0:
return self
if self.number_of_rows == 0:
return rows

new_df = pd.concat([self._data, rows._data]).infer_objects()
new_df.columns = self.column_names
Expand Down
22 changes: 20 additions & 2 deletions tests/safeds/data/tabular/containers/_table/test_add_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Table({"col1": ["a", "b", "c"], "col2": [1, 2, 4]}),
[],
Table({"col1": ["a", "b", "c"], "col2": [1, 2, 4]}),
)
),
],
ids=["Rows with string and integer values", "different schema", "empty", "add empty"],
)
Expand Down Expand Up @@ -63,8 +63,26 @@ def test_should_add_rows(table1: Table, rows: list[Row], table2: Table) -> None:
Table({"col1": [5, "7"], "col2": [6, None]}),
Table({"col1": [1, 2, 1, 5, "7"], "col2": [1, 2, 4, 6, None]}),
),
(
Table({"col1": [], "yikes": []}),
Table({"col1": [2], "yikes": [5]}),
Table({"col1": [2], "yikes": [5]}),
),
(
Table({"col1": [2], "yikes": [5]}),
Table({"col1": [], "yikes": []}),
Table({"col1": [2], "yikes": [5]}),
),
],
ids=[
"Rows from table",
"add empty to table",
"add on empty table",
"rowless",
"different schema",
"same schema add to no rows",
"same schema add no rows",
],
ids=["Rows from table", "add empty to table", "add on empty table", "rowless", "different schema"],
)
def test_should_add_rows_from_table(table1: Table, table2: Table, expected: Table) -> None:
table1 = table1.add_rows(table2)
Expand Down

0 comments on commit ffb8304

Please sign in to comment.