Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate to_json(store_index_names) #2129

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions pandapower/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import numpy
import pandas as pd
from packaging.version import Version
from packaging import version
import sys
try:
import xlsxwriter
Expand All @@ -25,6 +24,7 @@
except ImportError:
openpyxl_INSTALLED = False

from pandapower._version import __version__ as pp_version
from pandapower.auxiliary import soft_dependency_error, _preserve_dtypes
from pandapower.auxiliary import pandapowerNet
from pandapower.std_types import basic_std_types
Expand Down Expand Up @@ -101,7 +101,7 @@
writer._save()


def to_json(net, filename=None, encryption_key=None, store_index_names=False):
def to_json(net, filename=None, encryption_key=None, store_index_names=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)
Expand All @@ -116,38 +116,23 @@
**encrytion_key** (string, None) - If given, the pandapower network is stored as an
encrypted json string

**store_index_names** (bool, False) - If True, an additional dict "index_names" is
stored into the json string which includes the index names of the dataframes within the
net.
Since pandapower does usually not use net[elm].index.name, the default is False.


EXAMPLE:

>>> pp.to_json(net, "example.json")

"""
# --- store index names
if store_index_names:
# To ensure correct index names (see https://github.com/e2nIEE/pandapower/issues/1410),
# these are additionally stored to the json file as a dict.
if "index_names" in net.keys():
raise ValueError("To store DataFrame index names, 'index_names' "
"is used and thus should not be a key of net.")
net["index_names"] = {
key: net[key].index.name for key in net.keys() if isinstance(
net[key], pd.DataFrame) and isinstance(net[key].index.name, str) and \
net[key].index.name != ""
}
if store_index_names is not None:
msg = "The input parameter 'store_index_names' of function 'to_json()' is deprecated."
if Version(pp_version) < Version("2.15"):
warn(msg)

Check warning on line 128 in pandapower/file_io.py

View check run for this annotation

Codecov / codecov/patch

pandapower/file_io.py#L126-L128

Added lines #L126 - L128 were not covered by tests
else:
raise DeprecationWarning(msg)

Check warning on line 130 in pandapower/file_io.py

View check run for this annotation

Codecov / codecov/patch

pandapower/file_io.py#L130

Added line #L130 was not covered by tests

json_string = json.dumps(net, cls=io_utils.PPJSONEncoder, indent=2)
if encryption_key is not None:
json_string = io_utils.encrypt_string(json_string, encryption_key)

if store_index_names:
# remove the key "index_names" to not change net
del net["index_names"]

if filename is None:
return json_string

Expand Down
Loading