|
| 1 | +# SPDX-FileCopyrightText: 2022 Contributors to the Power Grid Model IO project <dynamic.grid.calculation@alliander.com> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MPL-2.0 |
| 4 | +""" |
| 5 | +CSV Directory Store |
| 6 | +""" |
| 7 | + |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any, Dict, List |
| 10 | + |
| 11 | +import pandas as pd |
| 12 | + |
| 13 | +from power_grid_model_io.data_stores.base_data_store import BaseDataStore |
| 14 | +from power_grid_model_io.data_types import TabularData |
| 15 | + |
| 16 | + |
| 17 | +class CsvDirStore(BaseDataStore[TabularData]): |
| 18 | + """ |
| 19 | + CSV Directory Store |
| 20 | +
|
| 21 | + The first row of each .csv file is expected to contain the column names, unless specified differently by an |
| 22 | + extension of this class. |
| 23 | + """ |
| 24 | + |
| 25 | + __slots__ = ("_dir_path", "_csv_kwargs", "_header_rows") |
| 26 | + |
| 27 | + def __init__(self, dir_path: Path, **csv_kwargs): |
| 28 | + super().__init__() |
| 29 | + self._dir_path = dir_path |
| 30 | + self._csv_kwargs: Dict[str, Any] = csv_kwargs |
| 31 | + self._header_rows: List[int] = [0] |
| 32 | + |
| 33 | + def load(self) -> TabularData: |
| 34 | + """ |
| 35 | + Load all CSV files in a directory as tabular data. |
| 36 | + """ |
| 37 | + data: Dict[str, pd.DataFrame] = {} |
| 38 | + for path in self._dir_path.glob("*.csv"): |
| 39 | + data[path.stem] = pd.read_csv(filepath_or_buffer=path, header=self._header_rows, **self._csv_kwargs) |
| 40 | + |
| 41 | + return TabularData(**data) |
| 42 | + |
| 43 | + def save(self, data: TabularData) -> None: |
| 44 | + """ |
| 45 | + Store each table in data as a separate CSV file |
| 46 | + """ |
| 47 | + for table_name, table_data in data.items(): |
| 48 | + table_data.to_csv(path_or_buf=self._dir_path / f"{table_name}.csv", **self._csv_kwargs) |
0 commit comments