pyerrors.input.pandas

 1import warnings
 2import gzip
 3import pandas as pd
 4from ..obs import Obs
 5from ..correlators import Corr
 6from .json import create_json_string, import_json_string
 7
 8
 9def dump_df(df, fname, gz=True):
10    """Exports a pandas DataFrame containing Obs valued columns to a (gzipped) csv file.
11
12    Before making use of pandas to_csv functionality Obs objects are serialized via the standardized
13    json format of pyerrors.
14
15    Parameters
16    ----------
17    df : pandas.DataFrame
18        Dataframe to be dumped to a file.
19    fname : str
20        Filename of the output file.
21    gz : bool
22        If True, the output is a gzipped csv file. If False, the output is a csv file.
23    """
24
25    out = df.copy()
26    for column in out:
27        if isinstance(out[column][0], (Obs, Corr)):
28            out[column] = out[column].transform(lambda x: create_json_string(x, indent=0))
29
30    if not fname.endswith('.csv'):
31        fname += '.csv'
32
33    if gz is True:
34        if not fname.endswith('.gz'):
35            fname += '.gz'
36        out.to_csv(fname, index=False, compression='gzip')
37    else:
38        out.to_csv(fname, index=False)
39
40
41def load_df(fname, auto_gamma=False, gz=True):
42    """Imports a pandas DataFrame from a csv.(gz) file in which Obs objects are serialized as json strings.
43
44    Parameters
45    ----------
46    fname : str
47        Filename of the input file.
48    auto_gamma : bool
49        If True applies the gamma_method to all imported Obs objects with the default parameters for
50        the error analysis. Default False.
51    gz : bool
52        If True, assumes that data is gzipped. If False, assumes JSON file.
53    """
54
55    if not fname.endswith('.csv') and not fname.endswith('.gz'):
56        fname += '.csv'
57
58    if gz is True:
59        if not fname.endswith('.gz'):
60            fname += '.gz'
61        with gzip.open(fname) as f:
62            re_import = pd.read_csv(f)
63    else:
64        if fname.endswith('.gz'):
65            warnings.warn("Trying to read from %s without unzipping!" % fname, UserWarning)
66        re_import = pd.read_csv(fname)
67
68    for column in re_import.select_dtypes(include="object"):
69        if isinstance(re_import[column][0], str):
70            if re_import[column][0][:20] == '{"program":"pyerrors':
71                re_import[column] = re_import[column].transform(lambda x: import_json_string(x, verbose=False))
72                if auto_gamma is True:
73                    re_import[column].apply(lambda x: x.gamma_method())
74
75    return re_import
def dump_df(df, fname, gz=True)
10def dump_df(df, fname, gz=True):
11    """Exports a pandas DataFrame containing Obs valued columns to a (gzipped) csv file.
12
13    Before making use of pandas to_csv functionality Obs objects are serialized via the standardized
14    json format of pyerrors.
15
16    Parameters
17    ----------
18    df : pandas.DataFrame
19        Dataframe to be dumped to a file.
20    fname : str
21        Filename of the output file.
22    gz : bool
23        If True, the output is a gzipped csv file. If False, the output is a csv file.
24    """
25
26    out = df.copy()
27    for column in out:
28        if isinstance(out[column][0], (Obs, Corr)):
29            out[column] = out[column].transform(lambda x: create_json_string(x, indent=0))
30
31    if not fname.endswith('.csv'):
32        fname += '.csv'
33
34    if gz is True:
35        if not fname.endswith('.gz'):
36            fname += '.gz'
37        out.to_csv(fname, index=False, compression='gzip')
38    else:
39        out.to_csv(fname, index=False)

Exports a pandas DataFrame containing Obs valued columns to a (gzipped) csv file.

Before making use of pandas to_csv functionality Obs objects are serialized via the standardized json format of pyerrors.

Parameters
  • df (pandas.DataFrame): Dataframe to be dumped to a file.
  • fname (str): Filename of the output file.
  • gz (bool): If True, the output is a gzipped csv file. If False, the output is a csv file.
def load_df(fname, auto_gamma=False, gz=True)
42def load_df(fname, auto_gamma=False, gz=True):
43    """Imports a pandas DataFrame from a csv.(gz) file in which Obs objects are serialized as json strings.
44
45    Parameters
46    ----------
47    fname : str
48        Filename of the input file.
49    auto_gamma : bool
50        If True applies the gamma_method to all imported Obs objects with the default parameters for
51        the error analysis. Default False.
52    gz : bool
53        If True, assumes that data is gzipped. If False, assumes JSON file.
54    """
55
56    if not fname.endswith('.csv') and not fname.endswith('.gz'):
57        fname += '.csv'
58
59    if gz is True:
60        if not fname.endswith('.gz'):
61            fname += '.gz'
62        with gzip.open(fname) as f:
63            re_import = pd.read_csv(f)
64    else:
65        if fname.endswith('.gz'):
66            warnings.warn("Trying to read from %s without unzipping!" % fname, UserWarning)
67        re_import = pd.read_csv(fname)
68
69    for column in re_import.select_dtypes(include="object"):
70        if isinstance(re_import[column][0], str):
71            if re_import[column][0][:20] == '{"program":"pyerrors':
72                re_import[column] = re_import[column].transform(lambda x: import_json_string(x, verbose=False))
73                if auto_gamma is True:
74                    re_import[column].apply(lambda x: x.gamma_method())
75
76    return re_import

Imports a pandas DataFrame from a csv.(gz) file in which Obs objects are serialized as json strings.

Parameters
  • fname (str): Filename of the input file.
  • auto_gamma (bool): If True applies the gamma_method to all imported Obs objects with the default parameters for the error analysis. Default False.
  • gz (bool): If True, assumes that data is gzipped. If False, assumes JSON file.