Skip to content

Commit d2c1530

Browse files
committed
Lazy loading CSV files (or tables in general)
Signed-off-by: Bram Stoeller <bram.stoeller@alliander.com>
1 parent 574cf6f commit d2c1530

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/power_grid_model_io/data_stores/csv_dir_store.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88
from pathlib import Path
9-
from typing import Any, Dict, List
9+
from typing import Any, Callable, Dict, List
1010

1111
import pandas as pd
1212

@@ -26,17 +26,24 @@ class CsvDirStore(BaseDataStore[TabularData]):
2626

2727
def __init__(self, dir_path: Path, **csv_kwargs):
2828
super().__init__()
29-
self._dir_path = dir_path
29+
self._dir_path = Path(dir_path)
3030
self._csv_kwargs: Dict[str, Any] = csv_kwargs
3131
self._header_rows: List[int] = [0]
3232

3333
def load(self) -> TabularData:
3434
"""
35-
Load all CSV files in a directory as tabular data.
35+
Create a lazy loader for all CSV files in a directory and store them in a TabularData instance.
3636
"""
37-
data: Dict[str, pd.DataFrame] = {}
37+
38+
def lazy_csv_loader(csv_path: Path) -> Callable[[], pd.DataFrame]:
39+
def csv_loader():
40+
return pd.read_csv(filepath_or_buffer=csv_path, header=self._header_rows, **self._csv_kwargs)
41+
42+
return csv_loader
43+
44+
data: Dict[str, Callable[[], pd.DataFrame]] = {}
3845
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)
46+
data[path.stem] = lazy_csv_loader(path)
4047

4148
return TabularData(**data)
4249

0 commit comments

Comments
 (0)