Skip to content

Commit

Permalink
feat: len for Column and Row
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Mar 21, 2023
1 parent 49fc816 commit d8d4f00
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
51 changes: 27 additions & 24 deletions src/safeds/data/tabular/_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def name(self) -> str:
"""
return self._name

@property
def statistics(self) -> ColumnStatistics:
return ColumnStatistics(self)

@property
def type(self) -> ColumnType:
"""
Expand All @@ -46,12 +50,35 @@ def type(self) -> ColumnType:
"""
return self._type

def __eq__(self, other: object) -> bool:
if not isinstance(other, Column):
return NotImplemented
if self is other:
return True
return self._data.equals(other._data) and self.name == other.name

def __getitem__(self, index: int) -> Any:
return self.get_value(index)

def __hash__(self) -> int:
return hash(self._data)

def __iter__(self) -> Iterator[Any]:
return iter(self._data)

def __len__(self) -> int:
return len(self._data)

def __repr__(self) -> str:
tmp = self._data.to_frame()
tmp.columns = [self.name]
return tmp.__repr__()

def __str__(self) -> str:
tmp = self._data.to_frame()
tmp.columns = [self.name]
return tmp.__str__()

def get_value(self, index: int) -> Any:
"""
Return column value at specified index, starting at 0.
Expand All @@ -76,10 +103,6 @@ def get_value(self, index: int) -> Any:

return self._data[index]

@property
def statistics(self) -> ColumnStatistics:
return ColumnStatistics(self)

def count(self) -> int:
"""
Return the number of elements in the column.
Expand Down Expand Up @@ -226,26 +249,6 @@ def get_unique_values(self) -> list[typing.Any]:
"""
return list(self._data.unique())

def __eq__(self, other: object) -> bool:
if not isinstance(other, Column):
return NotImplemented
if self is other:
return True
return self._data.equals(other._data) and self.name == other.name

def __hash__(self) -> int:
return hash(self._data)

def __str__(self) -> str:
tmp = self._data.to_frame()
tmp.columns = [self.name]
return tmp.__str__()

def __repr__(self) -> str:
tmp = self._data.to_frame()
tmp.columns = [self.name]
return tmp.__repr__()

def _ipython_display_(self) -> DisplayHandle:
"""
Return a display object for the column to be used in Jupyter Notebooks.
Expand Down
14 changes: 14 additions & 0 deletions src/safeds/data/tabular/_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def __getitem__(self, column_name: str) -> Any:
def __iter__(self) -> typing.Iterator[Any]:
return iter(self._data)

def __len__(self) -> int:
return len(self._data)

def get_value(self, column_name: str) -> Any:
"""
Return the value of a specified column.
Expand All @@ -37,6 +40,17 @@ def get_value(self, column_name: str) -> Any:
raise UnknownColumnNameError([column_name])
return self._data[self.schema._get_column_index_by_name(column_name)]

def count(self) -> int:
"""
Return the number of columns in this row.
Returns
-------
count : int
The number of columns.
"""
return len(self._data)

def has_column(self, column_name: str) -> bool:
"""
Return whether the row contains a given column.
Expand Down
2 changes: 1 addition & 1 deletion tests/safeds/data/tabular/_column/test_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_from_columns() -> None:
assert column1._type == column2._type


def negative_test_from_columns() -> None:
def test_from_columns_negative() -> None:
column1 = Column(pd.Series([1, 4]), "A")
column2 = Column(pd.Series(["2", "5"]), "B")

Expand Down

0 comments on commit d8d4f00

Please sign in to comment.