load_data¶
- pyhelpers.store.load_data(path_to_file, err_warning=True, prt_kwargs=None, **kwargs)[source]¶
Load data from a file.
- Parameters:
path_to_file (str | os.PathLike) – Pathname of the file; supported formats include Pickle, CSV, Microsoft Excel spreadsheet, JSON, Joblib and Feather.
err_warning (bool) – Whether to show a warning message if an unknown error occurs; defaults to
True
.prt_kwargs (dict | None) – [Optional] Additional parameters for the function
pyhelpers.store.ldr._check_loading_path()
; defaults toNone
.kwargs – [Optional] Additional parameters for one of the following functions:
load_pickle()
,load_csv()
,load_multiple_spreadsheets()
,load_json()
,load_joblib()
orload_feather()
.
- Returns:
Data retrieved from the specified path
path_to_file
.- Return type:
Any
Note
Example data can be referred to in the function
save_data()
.
Examples:
>>> from pyhelpers.store import load_data >>> from pyhelpers.dirs import cd >>> data_dir = cd("tests\data") >>> dat_pathname = cd(data_dir, "dat.pickle") >>> pickle_dat = load_data(path_to_file=dat_pathname, verbose=True) Loading "tests\data\dat.pickle" ... Done. >>> pickle_dat Longitude Latitude City London -0.127647 51.507322 Birmingham -1.902691 52.479699 Manchester -2.245115 53.479489 Leeds -1.543794 53.797418 >>> dat_pathname = cd(data_dir, "dat.csv") >>> csv_dat = load_data(path_to_file=dat_pathname, index=0, verbose=True) Loading "tests\data\dat.csv" ... Done. >>> csv_dat Longitude Latitude City London -0.127647 51.507322 Birmingham -1.902691 52.479699 Manchester -2.245115 53.479489 Leeds -1.543794 53.797418 >>> dat_pathname = cd(data_dir, "dat.json") >>> json_dat = load_data(path_to_file=dat_pathname, verbose=True) Loading "tests\data\dat.json" ... Done. >>> json_dat {'London': {'Longitude': -0.1276474, 'Latitude': 51.5073219}, 'Birmingham': {'Longitude': -1.9026911, 'Latitude': 52.4796992}, 'Manchester': {'Longitude': -2.2451148, 'Latitude': 53.4794892}, 'Leeds': {'Longitude': -1.5437941, 'Latitude': 53.7974185}} >>> dat_pathname = cd(data_dir, "dat.feather") >>> feather_dat = load_data(path_to_file=dat_pathname, index=0, verbose=True) Loading "tests\data\dat.feather" ... Done. >>> feather_dat Longitude Latitude City London -0.127647 51.507322 Birmingham -1.902691 52.479699 Manchester -2.245115 53.479489 Leeds -1.543794 53.797418 >>> dat_pathname = cd(data_dir, "dat.joblib") >>> joblib_dat = load_data(path_to_file=dat_pathname, verbose=True) Loading "tests\data\dat.joblib" ... Done. >>> joblib_dat array([[0.5488135 , 0.71518937, 0.60276338, ..., 0.02010755, 0.82894003, 0.00469548], [0.67781654, 0.27000797, 0.73519402, ..., 0.25435648, 0.05802916, 0.43441663], [0.31179588, 0.69634349, 0.37775184, ..., 0.86219152, 0.97291949, 0.96083466], ..., [0.89111234, 0.26867428, 0.84028499, ..., 0.5736796 , 0.73729114, 0.22519844], [0.26969792, 0.73882539, 0.80714479, ..., 0.94836806, 0.88130699, 0.1419334 ], [0.88498232, 0.19701397, 0.56861333, ..., 0.75842952, 0.02378743, 0.81357508]])