Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Add special case to Table.add_rows to increase performance #608

Merged
merged 5 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Marsmaennchen221 marked this conversation as resolved.
Show resolved Hide resolved
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