Converting TFS tables to Pandas DataFrames

The package pandas is an optional module, that has an inbuilt function to convert TFS tables (called mtable in MAD-NG) to a pandas DataFrame or a TfsDataFrame if you have tfs-pandas installed. In the example below, we generate an mtable by doing a survey and twiss on the Proton Synchrotron lattice, and then convert these to a DataFrame (or TfsDataFrame).

 1    mad["srv", "mflw"] = mad.survey(sequence=mad.ps)
 2    mad["mtbl", "mflw"] = mad.twiss(sequence=mad.ps, method=6, nslice=3, chrom=True)
 3    df = mad.mtbl.to_df()
 4    print(df)
 5    try:
 6        import tfs
 7    except ImportError:
 8        print("tfs-pandas not installed, so the header is stored in attrs instead of headers")
 9        print(df.attrs)
10
11    print(mad.srv.to_df())

In this script, we create the variables srv and mtbl which are mtables created by survey and twiss respectively. Then first, we convert the mtbl to a DataFrame and print it, before checking if you have tfs-pandas installed to check if we need to print out the header of the TFS table, which is stored in the attrs attribute of the DataFrame, but is automatically printed when using tfs-pandas. Then we convert the srv to a DataFrame and print it.

Note: If your object is not an mtable then this function will raise a TypeError, but it is available to call on all object types in MAD-NG.