def from_excel(filename, convert=True): """ Load a pandapower network from an excel file INPUT: **filename** (string) - The absolute or relative path to the input file. OUTPUT: **convert** (bool) - use the convert format function to **net** (dict) - The pandapower format network EXAMPLE: >>> net1 = pp.from_excel(os.path.join("C:", "example_folder", "example1.xlsx")) #absolute path >>> net2 = pp.from_excel("example2.xlsx") #relative path """ if not os.path.isfile(filename): raise UserWarning("File %s does not exist!" % filename) xls = pd.ExcelFile(filename).parse(sheetname=None) try: net = from_dict_of_dfs(xls) restore_all_dtypes(net, xls["dtypes"]) except: net = _from_excel_old(xls) if convert: convert_format(net) return net
def from_sql(con): cursor = con.cursor() cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") dodfs = dict() for t, in cursor.fetchall(): table = pd.read_sql_query("SELECT * FROM %s" % t, con, index_col="index") table.index.name = None dodfs[t] = table net = from_dict_of_dfs(dodfs) restore_all_dtypes(net, dodfs["dtypes"]) return net
def test_restore_all_dtypes(): net = create_test_network() pp.runpp(net) net['res_test'] = pd.DataFrame(columns=['test'], data=[1, 2, 3]) net['test'] = pd.DataFrame(columns=['test'], data=[1, 2, 3]) net.line['test'] = 123 net.res_line['test'] = 123 net.bus['test'] = 123 net.res_bus['test'] = 123 net.res_load['test'] = 123 dtdf = collect_all_dtypes_df(net) restore_all_dtypes(net, dtdf)
def from_json(filename, convert=True): """ 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. INPUT: **filename** (string or file) - The absolute or relative path to the input file or file-like object OUTPUT: **convert** (bool) - use the convert format function to **net** (dict) - The pandapower format network EXAMPLE: >>> net = pp.from_json("example.json") """ if hasattr(filename, 'read'): data = json.load(filename) elif not os.path.isfile(filename): raise UserWarning("File %s does not exist!!" % filename) else: with open(filename) as data_file: data = json.load(data_file) try: pd_dicts = dicts_to_pandas(data) net = from_dict_of_dfs(pd_dicts) restore_all_dtypes(net, pd_dicts["dtypes"]) if convert: convert_format(net) return net except UserWarning: # Can be deleted in the future, maybe now return from_json_dict(data, convert=convert)