Skip to content

Commit

Permalink
Merge branch 'dependabot/pip/polars-1.0.0' into dependabot/pip/numpy-…
Browse files Browse the repository at this point in the history
…2.1.1

# Conflicts:
#	pyproject.toml
  • Loading branch information
lars-reimann committed Sep 17, 2024
2 parents f4dcccd + f4acdfd commit 20a4eb9
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/safeds/data/tabular/containers/_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __init__(self, name: str, data: Sequence[T_co] | None = None) -> None:
if data is None:
data = []

self._series: pl.Series = pl.Series(name, data)
self._series: pl.Series = pl.Series(name, data, strict=False)

def __contains__(self, item: Any) -> bool:
return self._series.__contains__(item)
Expand All @@ -84,7 +84,7 @@ def __eq__(self, other: object) -> bool:
return NotImplemented
if self is other:
return True
return self._series.equals(other._series)
return self.name == other.name and self._series.equals(other._series)

@overload
def __getitem__(self, index: int) -> T_co: ...
Expand Down
20 changes: 7 additions & 13 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def from_json_file(path: str | Path) -> Table:

try:
return Table._from_polars_data_frame(pl.read_json(path))
except pl.PolarsPanicError:
except (pl.exceptions.PanicException, pl.exceptions.ComputeError):
# Can happen if the JSON file is empty (https://github.com/pola-rs/polars/issues/10234)
return Table()

Expand Down Expand Up @@ -322,7 +322,7 @@ def __init__(self, data: Mapping[str, Sequence[Any]] | None = None) -> None:
)

# Implementation
self._lazy_frame: pl.LazyFrame = pl.LazyFrame(data)
self._lazy_frame: pl.LazyFrame = pl.LazyFrame(data, strict=False)
self.__data_frame_cache: pl.DataFrame | None = None # Scramble the name to prevent access from outside

def __eq__(self, other: object) -> bool:
Expand Down Expand Up @@ -425,10 +425,10 @@ def schema(self) -> Schema:
import polars as pl

try:
return _PolarsSchema(self._lazy_frame.schema)
except (pl.NoDataError, pl.PolarsPanicError):
return _PolarsSchema(self._lazy_frame.collect_schema())
except (pl.exceptions.NoDataError, pl.exceptions.PanicException):
# Can happen for some operations on empty tables (e.g. https://github.com/pola-rs/polars/issues/16202)
return _PolarsSchema({})
return _PolarsSchema(pl.Schema({}))

# ------------------------------------------------------------------------------------------------------------------
# Column operations
Expand Down Expand Up @@ -698,7 +698,7 @@ def remove_columns(
_check_columns_exist(self, names)

return Table._from_polars_lazy_frame(
self._lazy_frame.drop(names),
self._lazy_frame.drop(names, strict=not ignore_unknown_names),
)

def remove_columns_except(
Expand Down Expand Up @@ -1919,8 +1919,6 @@ def to_dict(self) -> dict[str, list[Any]]:
def to_json_file(
self,
path: str | Path,
*,
orientation: Literal["column", "row"] = "column",
) -> None:
"""
Write the table to a JSON file.
Expand All @@ -1934,10 +1932,6 @@ def to_json_file(
----------
path:
The path to the JSON file. If the file extension is omitted, it is assumed to be ".json".
orientation:
The orientation of the JSON file. If "column", the JSON file will be structured as a list of columns. If
"row", the JSON file will be structured as a list of rows. Row orientation is more human-readable, but
slower and less memory-efficient.
Raises
------
Expand All @@ -1954,7 +1948,7 @@ def to_json_file(
path.parent.mkdir(parents=True, exist_ok=True)

# Write JSON to file
self._data_frame.write_json(path, row_oriented=(orientation == "row"))
self._data_frame.write_json(path)

def to_parquet_file(self, path: str | Path) -> None:
"""
Expand Down
6 changes: 3 additions & 3 deletions src/safeds/data/tabular/typing/_polars_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class _PolarsSchema(Schema):
# Dunder methods
# ------------------------------------------------------------------------------------------------------------------

def __init__(self, schema: dict[str, pl.DataType]):
self._schema: dict[str, pl.DataType] = schema
def __init__(self, schema: pl.Schema):
self._schema: pl.Schema = schema

def __eq__(self, other: object) -> bool:
if not isinstance(other, _PolarsSchema):
Expand Down Expand Up @@ -66,7 +66,7 @@ def __str__(self) -> str:

@property
def column_names(self) -> list[str]:
return list(self._schema.keys())
return list(self._schema.names())

# ------------------------------------------------------------------------------------------------------------------
# Getters
Expand Down
2 changes: 1 addition & 1 deletion tests/safeds/data/tabular/containers/_cell/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def test_should_be_deterministic() -> None:
cell: Cell[Any] = _LazyCell(pl.col("a"))
assert hash(cell) == 7139977585477665635
assert hash(cell) == 977452292332124345


@pytest.mark.parametrize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_should_get_the_item_at_index(index: int, expected: Any) -> None:
],
)
def test_should_raise_if_index_is_out_of_bounds(index: int) -> None:
column = Column("a", [0, "1"])
column = Column("a", ["a", "b"])

with pytest.raises(IndexOutOfBoundsError):
column.get_value(index)
4 changes: 2 additions & 2 deletions tests/safeds/data/tabular/containers/_column/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"+-----+\n| a |\n| --- |\n| i64 |\n+=====+\n| 0 |\n+-----+",
),
(
Column("a", [0, "1"]),
"+------+\n| a |\n| --- |\n| str |\n+======+\n| null |\n| 1 |\n+------+",
Column("a", ["a", "b"]),
"+-----+\n| a |\n| --- |\n| str |\n+=====+\n| a |\n| b |\n+-----+",
),
],
ids=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[
(Column("a", []), 0),
(Column("a", [0]), 1),
(Column("a", [0, "1"]), 2),
(Column("a", ["a", "b"]), 2),
],
ids=[
"empty",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[
Column("a", []),
Column("a", [0]),
Column("a", [0, "1"]),
Column("a", ["a", "b"]),
],
ids=[
"empty",
Expand Down
4 changes: 2 additions & 2 deletions tests/safeds/data/tabular/containers/_column/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"+-----+\n| a |\n| --- |\n| i64 |\n+=====+\n| 0 |\n+-----+",
),
(
Column("a", [0, "1"]),
"+------+\n| a |\n| --- |\n| str |\n+======+\n| null |\n| 1 |\n+------+",
Column("a", ["a", "b"]),
"+-----+\n| a |\n| --- |\n| str |\n+=====+\n| a |\n| b |\n+-----+",
),
],
ids=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def test_should_be_deterministic() -> None:
cell = _LazyStringCell(pl.col("a"))
assert hash(cell) == 7139977585477665635
assert hash(cell) == 977452292332124345


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/safeds/data/tabular/containers/_table/test_sizeof.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[
Table(),
Table({"col1": [0]}),
Table({"col1": [0, "1"], "col2": ["a", "b"]}),
Table({"col1": [0, 1], "col2": ["a", "b"]}),
],
ids=[
"empty table",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def test_should_be_deterministic() -> None:
cell = _LazyTemporalCell(pl.col("a"))
assert hash(cell) == 7139977585477665635
assert hash(cell) == 977452292332124345


@pytest.mark.parametrize(
Expand Down

0 comments on commit 20a4eb9

Please sign in to comment.