-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bram Stoeller <[email protected]>
- Loading branch information
1 parent
2648460
commit 0351485
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# SPDX-FileCopyrightText: 2022 Contributors to the Power Grid Model IO project <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
""" | ||
CSV Directory Store | ||
""" | ||
|
||
from pathlib import Path | ||
from typing import Any, Dict, List | ||
|
||
import pandas as pd | ||
|
||
from power_grid_model_io.data_stores.base_data_store import BaseDataStore | ||
from power_grid_model_io.data_types import TabularData | ||
|
||
|
||
class CsvDirStore(BaseDataStore[TabularData]): | ||
""" | ||
CSV Directory Store | ||
The first row of each .csv file is expected to contain the column names, unless specified differently by an | ||
extension of this class. | ||
""" | ||
|
||
__slots__ = ("_dir_path", "_csv_kwargs", "_header_rows") | ||
|
||
def __init__(self, dir_path: Path, **csv_kwargs): | ||
super().__init__() | ||
self._dir_path = dir_path | ||
self._csv_kwargs: Dict[str, Any] = csv_kwargs | ||
self._header_rows: List[int] = [0] | ||
|
||
def load(self) -> TabularData: | ||
""" | ||
Load all CSV files in a directory as tabular data. | ||
""" | ||
data: Dict[str, pd.DataFrame] = {} | ||
for path in self._dir_path.glob("*.csv"): | ||
data[path.stem] = pd.read_csv(filepath_or_buffer=path, header=self._header_rows, **self._csv_kwargs) | ||
|
||
return TabularData(**data) | ||
|
||
def save(self, data: TabularData) -> None: | ||
""" | ||
Store each table in data as a separate CSV file | ||
""" | ||
for table_name, table_data in data.items(): | ||
table_data.to_csv(path_or_buf=self._dir_path / f"{table_name}.csv", **self._csv_kwargs) |