Skip to content

Commit

Permalink
CSV data store (beta)
Browse files Browse the repository at this point in the history
Signed-off-by: Bram Stoeller <[email protected]>
  • Loading branch information
bramstoeller committed Mar 1, 2023
1 parent 2648460 commit 0351485
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/power_grid_model_io/data_stores/csv_dir_store.py
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)

0 comments on commit 0351485

Please sign in to comment.