Save and Load Networks
Advantage |
Disadvantage |
Example: saving
case9241pegase
|
|
pickle |
Allows storing of objects
|
- large filesize
- Stored objects might become
incompatible when loading
with different versions
|
- Savetime: 1.2s
- Loadtime: 0.65s
- Filesize: 18.4 MB
|
Excel |
Human readable
|
- Long time to save and load
- Needs libraries that are not part of
standard python distribution
|
- Savetime: 23.9s
- Loadtime: 10.9s
- Filesize: 4.9 MB
|
SQL |
- Savetime: 1.32s
- Loadtime: 0.6s
- Filesize: 5.1 MB
|
||
json |
can be interpreted in
other languages
|
potential insecurity with additional
translation in json notation
|
-Savetime: 0.19s
-Loadtime: 0.79s
- Filesize: 5.3 MB
|
pickle
- pandapower.to_pickle(net, filename)
Saves a pandapower Network with the pickle library.
- Parameters:
net (pandapowerNet) – The pandapower format network
filename (str) – The absolute or relative path to the output file or an writable file-like objects
- Example:
>>> from pandapower import to_pickle >>> to_pickle(net, os.path.join("C:", "example_folder", "example1.p")) # absolute path >>> to_pickle(net, "example2.p") # relative path
- Return type:
None
- pandapower.from_pickle(filename, convert=True, drop_invalid_geodata=False, ignore_version_conflicts=False)
Load a pandapower format Network from pickle file
- Parameters:
filename (str or file) – The absolute or relative path to the input file or file-like object
convert (bool) – If True, converts the format of the net loaded from pickle from the older version of pandapower to the newer version format, default True
drop_invalid_geodata (bool) – If set to True, drop geodata entries with invalid coordinates instead of raising an error, default True
ignore_version_conflicts (bool) – If set to True, ignore version conflicts between the net being loaded and the pandapower version. This can lead to errors when loading nets saved in older formats. Use with caution! default False
- Returns:
The pandapower network
- Return type:
pandapowerNet
- Example:
>>> from pandapower import from_pickle >>> net1 = from_pickle(os.path.join("C:", "example_folder", "example1.p")) #absolute path >>> net2 = from_pickle("example2.p") #relative path
Excel
Using excel is highly discouraged as to_excel uses pandas to_excel which is a lossy conversion. Loading from excel may result in a different network.
- pandapower.to_excel(net, filename, include_empty_tables=False, include_results=True)
Saves a pandapower Network to an Excel file.
- Parameters:
net (pandapowerNet) – The pandapower format network
filename (str) – The absolute or relative path to the output file
include_empty_tables (bool) – empty element tables are saved as Excel sheet, default False
include_results (bool) – results are included in the Excel sheet, default True
- Example:
>>> from pandapower import to_excel >>> to_excel(net, os.path.join("C:", "example_folder", "example1.xlsx")) # absolute path >>> to_excel(net, "example2.xlsx") # relative path
- pandapower.from_excel(filename, convert=True, add_basic_std_types=True, drop_invalid_geodata=False, ignore_version_conflicts=False)
Load a pandapower network from an Excel file
- Parameters:
filename (str) – The absolute or relative path to the input file.
convert (bool) – If True, converts the format of the net loaded from Excel from the older version of pandapower to the newer version format, default True
add_basic_std_types (bool) – If True, Adds missing standard-types from pandapower standard type library, default True. the older version of pandapower to the newer version format, default True
drop_invalid_geodata (bool) – If set to True, drop geodata entries with invalid coordinates instead of raising an error, default True
ignore_version_conflicts (bool) – If set to True, ignore version conflicts between the net being loaded and the pandapower version. This can lead to errors when loading nets saved in older formats. Use with caution! default False
- Returns:
The pandapower network
- Return type:
pandapowerNet
- Example:
>>> from pandapower import from_excel >>> net1 = from_excel(os.path.join("C:", "example_folder", "example1.xlsx")) >>> net2 = from_excel("example2.xlsx") #relative path
Json
- pandapower.to_json(net: pandapowerNet, filename: None = None, encryption_key: str | None = None, indent: int | str | None = 2, sort_keys: bool = False) str
- pandapower.to_json(net: pandapowerNet, filename: str | TextIO, encryption_key: str | None = None, indent: int | str | None = 2, sort_keys: bool = False) None
Saves a pandapower Network in JSON format. The index columns of all pandas DataFrames will be saved in ascending order. net elements which name begins with “_” (internal elements) will not be saved. Std types will also not be saved.
- Parameters:
net (pandapowerNet) – The pandapower format network
filename (str or file) – The absolute or relative path to the output file or a file-like object, if ‘None’ the function returns a json string, default None
encryption_key (str or None) – If given, the pandapower network is stored as an encrypted json string, default None
indent (int or str or None) – indentation to use for the json. String or amount of spaces to use, defaut 2
sort_keys (bool) – sort dictionaries by key, default False
- Example:
>>> from pandapower.file_io import to_json >>> to_json(net, "example.json")
- pandapower.from_json(filename_or_str, convert=True, encryption_key=None, elements_to_deserialize=None, keep_serialized_elements=True, add_basic_std_types=False, replace_elements=None, empty_dict_like_object=None, ignore_unknown_objects=False, drop_invalid_geodata=False, omit_tables=None, omit_modules=None, ignore_version_conflicts=False, skip_checks=False)
Load a pandapower network from a JSON file. The index of the returned network is not necessarily in the same order as the original network. Index columns of all pandas DataFrames are sorted in ascending order.
- Parameters:
filename_or_str (str or file) – The absolute or relative path to the input file or file-like object
convert (bool) – If True, converts the format of the net loaded from json from the older version of pandapower to the newer version format, default True
encryption_key (str or None) – If given, key to decrypt an encrypted pandapower network, default None
elements_to_deserialize (list or None) – Deserialize only certain pandapower elements. If None all elements are deserialized, default None
keep_serialized_elements (bool) – Keep serialized elements if given. Default: Serialized elements are kept, default True
add_basic_std_types (bool) – Add missing standard-types from pandapower standard type library, default False
replace_elements (dict or None) – Keys are replaced by values found in json string. Both key and value are supposed to be strings, default None
empty_dict_like_object (dict or pandapowerNet or None) – If None, the output of pandapower.create_empty_network() is used as an empty element to be filled by the data of the json string. Give another dict-like object to start filling that alternative object with the json data, default None
ignore_unknown_objects (bool) – If set to True, ignore any objects that cannot be deserialized instead of raising an error, default False
drop_invalid_geodata (bool) – If set to True, drop geodata entries with invalid coordinates instead of raising an error, default False
omit_tables (list or None) – List of tables to omit during deserialization, default None
omit_modules (list or None) – List of modules to omit during deserialization, default None
ignore_version_conflicts (bool) – If set to True, ignore version conflicts between the net being loaded and the pandapower version. This can lead to errors when loading nets saved in older formats. Use with caution! default False
skip_checks (bool) – If set to True, no checks will be performed. .. warning:: Only perform on trusted data sources / networks!
- Returns:
The pandapower network
- Return type:
pandapowerNet
- Example:
>>> from pandapower.file_io import from_json >>> net = from_json("example.json")
SQL
- pandapower.to_sqlite(net, filename, include_results=False)
Saves pandapowerNet an SQLite format
- Parameters:
net (grid model) – pandapowerNet
filename (path to a text file where the data will be stored) – str
include_results (whether result tables should be included) – bool
- pandapower.from_sqlite(filename)
Loads a grid model from SQLite format
- Parameters:
filename (path to the text file where the data are stored)
- Returns:
net – pandapowerNet
- Return type:
the grid model
PostgreSQL
- pandapower.to_postgresql(net, dsn, schema, include_results=False, grid_id=None, grid_id_column='grid_id', grid_catalogue_name='grid_catalogue', index_name=None)
Uploads a pandapowerNet to a PostgreSQL database. The database must exist, the element tables are created if they do not exist. JSON serialization (e.g. for controller objects) is not implemented yet.
- Parameters:
net (pandapowerNet) – the grid model to be uploaded to the database
dsn (str) – data source name according to pep-249
schema (str) – name of the database schema (e.g. ‘postgres’)
include_results (bool) – specify whether the power flow results are included when the grid is uploaded
grid_id (int | None) – unique grid_id that will be used to identify the data for the grid model, default None. If None, it will be set automatically by PostgreSQL
grid_id_column (str) – name of the column for “grid_id” in the PosgreSQL tables, default=”grid_id”.
grid_catalogue_name (str) – name of the catalogue table that includes all grid_id values and the timestamp when the grid data were added
index_name – name of the custom column to be used inplace of index in the element tables if it is not the standard DataFrame index
- Returns:
either the user-specified grid_id or the automatically generated grid_id of the grid model
- Return type:
int
- pandapower.from_postgresql(grid_id, dsn, schema, grid_id_column='grid_id', grid_catalogue_name='grid_catalogue', empty_dict_like_object=None, grid_tables=None)
Downloads an existing pandapowerNet from a PostgreSQL database.
- Parameters:
grid_id (int) – unique grid_id that will be used to identify the data for the grid model
dsn (str) – data source name according to pep-249
schema (str) – name of the database schema (e.g. ‘postgres’)
grid_id_column (str) – name of the column for “grid_id” in the PosgreSQL tables, default=”grid_id”.
grid_catalogue_name (str) – name of the catalogue table that includes all grid_id values and the timestamp when the grid data were added
empty_dict_like_object (dict | None) – If None, the output of pandapower.create_empty_network() is used as an empty element to be filled by the grid data. Give another dict-like object to start filling that alternative object with the data.
grid_tables
- Returns:
the loaded pandapower network
- pandapower.delete_postgresql_net(grid_id, dsn, schema, grid_id_column='grid_id', grid_catalogue_name='grid_catalogue')
Removes a grid model from the PostgreSQL database.
- Parameters:
grid_id (int) – unique grid_id that will be used to identify the data for the grid model
dsn (str) – data source name according to pep-249
schema (str) – name of the database schema (e.g. ‘postgres’)
grid_id_column (str) – name of the column for “grid_id” in the PosgreSQL tables, default=”grid_id”.
grid_catalogue_name (str) – name of the catalogue table that includes all grid_id values and the timestamp when the grid data were added
- Return type:
None
Examples
>>> delete_postgresql_net(0, "postgresql://user:password@host:port/database", "test_schema", "grid_id", "grid_catalogue")