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

feat: Create table from file: read_csv and read_json #164

Merged
merged 22 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f20dadc
feat: read_csv und read_json
WinPlay02 Nov 11, 2022
413eec6
145: Documentation added
WinPlay02 Nov 11, 2022
c0a7fd7
Merge branch 'main' into 145-create-table-from-file
WinPlay02 Nov 11, 2022
a1f531f
Changed Paths of test_table Data
WinPlay02 Nov 11, 2022
c0f6fa9
Merge branch '145-create-table-from-file' of https://github.com/lars-…
WinPlay02 Nov 11, 2022
cfaae93
Changed Path to other safe-ds
WinPlay02 Nov 11, 2022
d790e63
Fixed Tests + Changed Docstring Format
WinPlay02 Nov 11, 2022
2367bae
Fixed Test-Data
WinPlay02 Nov 11, 2022
dad15ec
Re-Raise Exceptions
WinPlay02 Nov 11, 2022
1d734a8
style: apply automated linter fixes
WinPlay02 Nov 11, 2022
9aa5d94
Type Hinting
WinPlay02 Nov 11, 2022
2ecc452
Merge branch '145-create-table-from-file' of https://github.com/lars-…
WinPlay02 Nov 11, 2022
7f54444
Catch other errors as ValueError
WinPlay02 Nov 11, 2022
038289b
Exception Names are now better
WinPlay02 Nov 11, 2022
d85fa2a
Merge branch 'main' into 145-create-table-from-file
WinPlay02 Nov 11, 2022
0b6fc52
docs: update `returns` sections
lars-reimann Nov 16, 2022
84eed16
refactor: update error messages
lars-reimann Nov 16, 2022
fe0aaaa
refactor: move test resources to dedicated folder
lars-reimann Nov 16, 2022
dbede58
Merge branch 'main' into 145-create-table-from-file
lars-reimann Nov 16, 2022
de082be
fix: issues after merging
lars-reimann Nov 16, 2022
cbde1af
refactor: remove dummy test file
lars-reimann Nov 16, 2022
8b540bb
style: apply automated linter fixes
lars-reimann Nov 16, 2022
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
64 changes: 64 additions & 0 deletions Runtime/safe-ds/safe_ds/data/_table.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,70 @@
from __future__ import annotations

import pandas as pd


class Table:
def __init__(self, data: pd.DataFrame):
self._data: pd.DataFrame = data

@staticmethod
def from_json(path: str) -> Table:
"""
Reads data from a JSON file into a Table

Parameters
----------
path : str
Path to the file as String

Returns
-------
table : Table
The Table read from the file

Raises
------
FileNotFoundError
If the specified file does not exist
ValueError
If the file could not be read
"""

try:
return Table(pd.read_json(path))
except FileNotFoundError as exception:
raise FileNotFoundError(f'File "{path}" does not exist') from exception
except Exception as exception:
raise ValueError(
f'Could not read file from "{path}" as JSON'
) from exception

@staticmethod
def from_csv(path: str) -> Table:
"""
Reads data from a CSV file into a Table.

Parameters
----------
path : str
Path to the file as String

Returns
-------
table : Table
The Table read from the file

Raises
------
FileNotFoundError
If the specified file does not exist
ValueError
If the file could not be read
"""

try:
return Table(pd.read_csv(path))
except FileNotFoundError as exception:
raise FileNotFoundError(f'File "{path}" does not exist') from exception
except Exception as exception:
raise ValueError(f'Could not read file from "{path}" as CSV') from exception
22 changes: 22 additions & 0 deletions Runtime/safe-ds/tests/data/test_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest
from safe_ds.data import Table


def test_read_csv_valid():
table = Table.from_csv("tests/resources/test_table_read_csv.csv")
assert table._data["A"][0] == 1 and table._data["B"][0] == 2


def test_read_csv_invalid():
with pytest.raises(FileNotFoundError):
Table.from_csv("tests/resources/test_table_read_csv_invalid.csv")


def test_read_json_valid():
table = Table.from_json("tests/resources/test_table_read_json.json")
assert table._data["A"][0] == 1 and table._data["B"][0] == 2


def test_read_json_invalid():
with pytest.raises(FileNotFoundError):
Table.from_json("tests/resources/test_table_read_json_invalid.json")
2 changes: 2 additions & 0 deletions Runtime/safe-ds/tests/resources/test_table_read_csv.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A,B
1,2
6 changes: 6 additions & 0 deletions Runtime/safe-ds/tests/resources/test_table_read_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"A": 1,
"B": 2
}
]
2 changes: 0 additions & 2 deletions Runtime/safe-ds/tests/test_dummy.py

This file was deleted.