Skip to content

Commit

Permalink
feat: add Column.to_table (#705)
Browse files Browse the repository at this point in the history
Closes #695

### Summary of Changes

Add method `Column.to_table` to get a `Table` from a `Column`.

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
lars-reimann and megalinter-bot committed May 3, 2024
1 parent f8c27bc commit 36e4a7a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/safeds/data/tabular/containers/_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import pandas as pd

from safeds.data.tabular.containers import Table


T = TypeVar("T")
R = TypeVar("R")
Expand Down Expand Up @@ -1015,8 +1017,22 @@ def plot_histogram(self) -> Image:
# Conversion
# ------------------------------------------------------------------------------------------------------------------

def to_table(self) -> Table:
"""
Create a table that contains only this column.
Returns
-------
table:
The table with this column.
"""
# Must be imported here to avoid circular imports
from safeds.data.tabular.containers import Table

return Table.from_columns([self])

def to_html(self) -> str:
r"""
"""
Return an HTML representation of the column.
Returns
Expand All @@ -1040,7 +1056,7 @@ def to_html(self) -> str:
# ------------------------------------------------------------------------------------------------------------------

def _repr_html_(self) -> str:
r"""
"""
Return an HTML representation of the column.
Returns
Expand Down
17 changes: 17 additions & 0 deletions tests/safeds/data/tabular/containers/_column/test_to_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
from safeds.data.tabular.containers import Column, Table


@pytest.mark.parametrize(
argnames=("column", "expected"),
argvalues=[
(Column("A", []), Table({"A": []})),
(Column("A", [1, 2, 3]), Table({"A": [1, 2, 3]})),
],
ids=[
"empty",
"non-empty",
],
)
def test_should_return_a_table_with_this_column(column: Column, expected: Table) -> None:
assert column.to_table() == expected

0 comments on commit 36e4a7a

Please sign in to comment.