def test_to_latex_float_format_no_fixed_width(self): # GH 21625 df = DataFrame({'x': [0.19999]}) expected = r"""\begin{tabular}{lr} \toprule {} & x \\ \midrule 0 & 0.200 \\ \bottomrule \end{tabular} """ assert df.to_latex(float_format='%.3f') == expected # GH 22270 df = DataFrame({'x': [100.0]}) expected = r"""\begin{tabular}{lr} \toprule {} & x \\ \midrule 0 & 100 \\ \bottomrule \end{tabular} """ assert df.to_latex(float_format='%.0f') == expected
def test_to_latex(self, frame): # it works! frame.to_latex() df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']}) withindex_result = df.to_latex() withindex_expected = r"""\begin{tabular}{lrl} \toprule {} & a & b \\ \midrule 0 & 1 & b1 \\ 1 & 2 & b2 \\ \bottomrule \end{tabular} """ assert withindex_result == withindex_expected withoutindex_result = df.to_latex(index=False) withoutindex_expected = r"""\begin{tabular}{rl} \toprule a & b \\ \midrule 1 & b1 \\ 2 & b2 \\ \bottomrule \end{tabular} """ assert withoutindex_result == withoutindex_expected
def test_to_latex_longtable(self, frame): frame.to_latex(longtable=True) df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']}) withindex_result = df.to_latex(longtable=True) withindex_expected = r"""\begin{longtable}{lrl} \toprule {} & a & b \\ \midrule \endhead \midrule \multicolumn{3}{r}{{Continued on next page}} \\ \midrule \endfoot \bottomrule \endlastfoot 0 & 1 & b1 \\ 1 & 2 & b2 \\ \end{longtable} """ open("expected.txt", "w").write(withindex_result) assert withindex_result == withindex_expected withoutindex_result = df.to_latex(index=False, longtable=True) withoutindex_expected = r"""\begin{longtable}{rl} \toprule a & b \\ \midrule \endhead \midrule \multicolumn{2}{r}{{Continued on next page}} \\ \midrule \endfoot \bottomrule \endlastfoot 1 & b1 \\ 2 & b2 \\ \end{longtable} """ assert withoutindex_result == withoutindex_expected df = DataFrame({'a': [1, 2]}) with1column_result = df.to_latex(index=False, longtable=True) assert "\multicolumn{1}" in with1column_result df = DataFrame({'a': [1, 2], 'b': [3, 4], 'c': [5, 6]}) with3columns_result = df.to_latex(index=False, longtable=True) assert "\multicolumn{3}" in with3columns_result
def test_to_latex_filename(self, frame): with tm.ensure_clean('test.tex') as path: frame.to_latex(path) with open(path, 'r') as f: assert frame.to_latex() == f.read() # test with utf-8 and encoding option (GH 7061) df = DataFrame([[u'au\xdfgangen']]) with tm.ensure_clean('test.tex') as path: df.to_latex(path, encoding='utf-8') with codecs.open(path, 'r', encoding='utf-8') as f: assert df.to_latex() == f.read() # test with utf-8 without encoding option if compat.PY3: # python3: pandas default encoding is utf-8 with tm.ensure_clean('test.tex') as path: df.to_latex(path) with codecs.open(path, 'r', encoding='utf-8') as f: assert df.to_latex() == f.read() else: # python2 default encoding is ascii, so an error should be raised with tm.ensure_clean('test.tex') as path: with pytest.raises(UnicodeEncodeError): df.to_latex(path)
def test_to_latex_with_formatters(self): df = DataFrame({'int': [1, 2, 3], 'float': [1.0, 2.0, 3.0], 'object': [(1, 2), True, False], 'datetime64': [datetime(2016, 1, 1), datetime(2016, 2, 5), datetime(2016, 3, 3)]}) formatters = {'int': lambda x: '0x%x' % x, 'float': lambda x: '[% 4.1f]' % x, 'object': lambda x: '-%s-' % str(x), 'datetime64': lambda x: x.strftime('%Y-%m'), '__index__': lambda x: 'index: %s' % x} result = df.to_latex(formatters=dict(formatters)) expected = r"""\begin{tabular}{llrrl} \toprule {} & datetime64 & float & int & object \\ \midrule index: 0 & 2016-01 & [ 1.0] & 0x1 & -(1, 2)- \\ index: 1 & 2016-02 & [ 2.0] & 0x2 & -True- \\ index: 2 & 2016-03 & [ 3.0] & 0x3 & -False- \\ \bottomrule \end{tabular} """ assert result == expected
def print_reduction(): ton = 9 f = open(DATAP + '/articledict.json', 'r', encoding="UTF8") langdict = load(f) df = DataFrame(columns=['#seed', '#art', '#rec_art', '#rec_nonseed', 'reduced %'], index=['Formal languages', 'Computer file formats', 'Installation software']) for c in ROOTS: articles = [a for a in langdict if c + "Depth" in langdict[a]] seed_articles = [sa for sa in articles if is_seed(sa, langdict)] recovered_articles = [ra for ra in articles if check_sl(ra, langdict)] nonseed_articles = [nsa for nsa in articles if not is_seed(nsa, langdict) and check_sl(nsa, langdict)] percentage = 100 * (len(recovered_articles) / len(articles)) FL_overlap = [fl for fl in recovered_articles if "Category:Formal_LanguagesDepth" in langdict[fl]] print(len(FL_overlap)) CFF_overlap = [cff for cff in recovered_articles if "Category:Computer_file_formatsDepth" in langdict[cff]] print(len(CFF_overlap)) IS_overlap = [i for i in recovered_articles if "Category:Installation_softwareDepth" in langdict[i]] print(len(IS_overlap)) df.loc[c.replace("Category:", "").replace("_", " ")] = Series( {'#art': len(articles), '#seed': len(seed_articles), '#rec_art': len(recovered_articles), '#rec_nonseed': len(nonseed_articles), 'reduced %': percentage}) print("----") print(df) print(df.to_latex()) nogo = [l for l in langdict if is_seed(l, langdict) and not check_sl(l, langdict)] print(nogo) print(langdict["Augmented_Backus–Naur_Form"])
def test_to_latex_specified_header(self): # GH 7124 df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']}) withindex_result = df.to_latex(header=['AA', 'BB']) withindex_expected = r"""\begin{tabular}{lrl} \toprule {} & AA & BB \\ \midrule 0 & 1 & b1 \\ 1 & 2 & b2 \\ \bottomrule \end{tabular} """ assert withindex_result == withindex_expected withoutindex_result = df.to_latex(header=['AA', 'BB'], index=False) withoutindex_expected = r"""\begin{tabular}{rl} \toprule AA & BB \\ \midrule 1 & b1 \\ 2 & b2 \\ \bottomrule \end{tabular} """ assert withoutindex_result == withoutindex_expected withoutescape_result = df.to_latex(header=['$A$', '$B$'], escape=False) withoutescape_expected = r"""\begin{tabular}{lrl} \toprule {} & $A$ & $B$ \\ \midrule 0 & 1 & b1 \\ 1 & 2 & b2 \\ \bottomrule \end{tabular} """ assert withoutescape_result == withoutescape_expected with pytest.raises(ValueError): df.to_latex(header=['A'])
def test_to_latex_longtable(self, frame): frame.to_latex(longtable=True) df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']}) withindex_result = df.to_latex(longtable=True) withindex_expected = r"""\begin{longtable}{lrl} \toprule {} & a & b \\ \midrule \endhead \midrule \multicolumn{3}{r}{{Continued on next page}} \\ \midrule \endfoot \bottomrule \endlastfoot 0 & 1 & b1 \\ 1 & 2 & b2 \\ \end{longtable} """ assert withindex_result == withindex_expected withoutindex_result = df.to_latex(index=False, longtable=True) withoutindex_expected = r"""\begin{longtable}{rl} \toprule a & b \\ \midrule \endhead \midrule \multicolumn{3}{r}{{Continued on next page}} \\ \midrule \endfoot \bottomrule \endlastfoot 1 & b1 \\ 2 & b2 \\ \end{longtable} """ assert withoutindex_result == withoutindex_expected
def computational_cost(): n_values = [50 * 2 ** m for m in range(6)] solvers = [s.__name__ for s in [gaussian_elimination, gaussian_elimination_pivots, housetriang_solve]] table = DataFrame(columns=solvers, index=n_values) for solver in [gaussian_elimination, gaussian_elimination_pivots, housetriang_solve]: times = timer(solver, n_values) table[solver.__name__] = Series(times) with open("table_file.tex", 'w') as out: out.write(table.to_latex()) return n_values, table
def print_pca_components(pca, name): from plotting import plot_pca_components components = DataFrame(pca.components_, columns=utils.get_latex_par_names_from_list(par.columns)) filename = folder + 'clustering_%s_%s'%(dataset, name) plot_pca_components(filename=filename, components=components[::-1]) components['$\\gamma$'] = pca.explained_variance_ratio_ filename = folder + 'clustering_%s_%s.tex'%(dataset, name) tex = utils.prettify_table(components.to_latex(float_format=lambda x: str(round(x,2))), label='table:clustering_%s_%s'%(dataset, name), caption='XXX') with open(filename, 'w') as f: f.write(tex)
def prepare_for_print(df: pd.DataFrame, precisions: dict): do_percent = True if do_percent: df['ece'] *= 100 df['ds_ece'] *= 100 for entry, prec in precisions.items(): df[entry] = df[entry].round(prec) print(df.to_latex()) return df
def test_to_latex_empty_longtable(self): df = DataFrame() result = df.to_latex(longtable=True) expected = _dedent(r""" \begin{longtable}{l} \toprule Empty DataFrame Columns: Index([], dtype='object') Index: Index([], dtype='object') \\ \end{longtable} """) assert result == expected
def print_df(df: DataFrame, formats=None): """ Prints the dataframe on the output for the specified list of formats :param df: :param formats: one of ['tab','latex'] """ if formats is None: formats = ['tab'] if 'tab' in formats: print(df) if 'latex' in formats: print(df.to_latex(float_format=lambda x: '%.2f' % x))
def test_to_latex_multiindex_small_tabular(self): df = DataFrame({("x", "y"): ["a"]}).T result = df.to_latex() expected = _dedent(r""" \begin{tabular}{lll} \toprule & & 0 \\ \midrule x & y & a \\ \bottomrule \end{tabular} """) assert result == expected
def test_to_latex_empty(self): df = DataFrame() result = df.to_latex() expected = r"""\begin{tabular}{l} \toprule Empty DataFrame Columns: Index([], dtype='object') Index: Index([], dtype='object') \\ \bottomrule \end{tabular} """ assert result == expected result = df.to_latex(longtable=True) expected = r"""\begin{longtable}{l} \toprule Empty DataFrame Columns: Index([], dtype='object') Index: Index([], dtype='object') \\ \end{longtable} """ assert result == expected
def test_to_latex_no_header_without_index(self): # GH 7124 df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) result = df.to_latex(index=False, header=False) expected = _dedent(r""" \begin{tabular}{rl} \toprule 1 & b1 \\ 2 & b2 \\ \bottomrule \end{tabular} """) assert result == expected
def test_to_latex_empty_tabular(self): df = DataFrame() result = df.to_latex() expected = _dedent(r""" \begin{tabular}{l} \toprule Empty DataFrame Columns: Index([], dtype='object') Index: Index([], dtype='object') \\ \bottomrule \end{tabular} """) assert result == expected
def create_latex_from_table(filename: str, table: pd.DataFrame, create_doc: bool): if create_doc: latex = """\\documentclass[12pt]{article}\n\\usepackage{booktabs}\n\\usepackage{geometry}\n\\usepackage{extsizes}\n\\geometry{a4paper, landscape, lmargin=4cm, rmargin=2cm, tmargin=1cm, bmargin=1cm}\n\\begin{document}\n""" + table.to_latex( ) mode = "w" else: latex = table.to_latex() + "\\end{document}" mode = "a" with open(filename + ".tex", mode) as f: f.write(latex) f.write("\n")
def test_to_latex_float_format_no_fixed_width_integer(self): # GH 22270 df = DataFrame({"x": [100.0]}) result = df.to_latex(float_format="%.0f") expected = _dedent(r""" \begin{tabular}{lr} \toprule {} & x \\ \midrule 0 & 100 \\ \bottomrule \end{tabular} """) assert result == expected
def test_to_latex_multiindex_column_tabular(self): df = DataFrame({("x", "y"): ["a"]}) result = df.to_latex() expected = _dedent(r""" \begin{tabular}{ll} \toprule {} & x \\ {} & y \\ \midrule 0 & a \\ \bottomrule \end{tabular} """) assert result == expected
def test_to_latex_tabular_with_index(self): df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) result = df.to_latex() expected = _dedent(r""" \begin{tabular}{lrl} \toprule {} & a & b \\ \midrule 0 & 1 & b1 \\ 1 & 2 & b2 \\ \bottomrule \end{tabular} """) assert result == expected
def to_latex_is_rapid_learning_real(df: DataFrame): # put the |c|...|c| for each column column_format = '|' num_columns = len(df.columns) + 1 # +1 because the "index" df is the rows for _ in range(num_columns): column_format = column_format + 'c|' latex = df.to_latex(column_format=column_format) # replace \toprule, \midrule, \bottomrule with \hline (looks better plus don't need to import \usepackage{booktabs} rules = ['\\toprule', '\\midrule', '\\bottomrule'] latex = latex.replace(rules[0], '\\hline') latex = latex.replace(rules[1], '\\hline') latex = latex.replace(rules[2], '\\hline') latex = latex.replace('+-', ' $\\pm$ ') return latex
def test_to_latex_float_format_no_fixed_width_3decimals(self): # GH 21625 df = DataFrame({"x": [0.19999]}) result = df.to_latex(float_format="%.3f") expected = _dedent(r""" \begin{tabular}{lr} \toprule {} & x \\ \midrule 0 & 0.200 \\ \bottomrule \end{tabular} """) assert result == expected
def test_to_latex_column_format(self): df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) result = df.to_latex(column_format="lcr") expected = _dedent(r""" \begin{tabular}{lcr} \toprule {} & a & b \\ \midrule 0 & 1 & b1 \\ 1 & 2 & b2 \\ \bottomrule \end{tabular} """) assert result == expected
def test_to_latex_no_header(self): # GH 7124 df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']}) withindex_result = df.to_latex(header=False) withindex_expected = r"""\begin{tabular}{lrl} \toprule 0 & 1 & b1 \\ 1 & 2 & b2 \\ \bottomrule \end{tabular} """ assert withindex_result == withindex_expected withoutindex_result = df.to_latex(index=False, header=False) withoutindex_expected = r"""\begin{tabular}{rl} \toprule 1 & b1 \\ 2 & b2 \\ \bottomrule \end{tabular} """ assert withoutindex_result == withoutindex_expected
def test_to_latex_decimal(self): # GH 12031 df = DataFrame({"a": [1.0, 2.1], "b": ["b1", "b2"]}) result = df.to_latex(decimal=",") expected = _dedent(r""" \begin{tabular}{lrl} \toprule {} & a & b \\ \midrule 0 & 1,0 & b1 \\ 1 & 2,1 & b2 \\ \bottomrule \end{tabular} """) assert result == expected
def test_to_latex_specified_header_special_chars_without_escape(self): # GH 7124 df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) result = df.to_latex(header=["$A$", "$B$"], escape=False) expected = _dedent(r""" \begin{tabular}{lrl} \toprule {} & $A$ & $B$ \\ \midrule 0 & 1 & b1 \\ 1 & 2 & b2 \\ \bottomrule \end{tabular} """) assert result == expected
def test_to_latex_special_escape(self): df = DataFrame([r"a\b\c", r"^a^b^c", r"~a~b~c"]) escaped_result = df.to_latex() escaped_expected = r"""\begin{tabular}{ll} \toprule {} & 0 \\ \midrule 0 & a\textbackslash b\textbackslash c \\ 1 & \textasciicircum a\textasciicircum b\textasciicircum c \\ 2 & \textasciitilde a\textasciitilde b\textasciitilde c \\ \bottomrule \end{tabular} """ assert escaped_result == escaped_expected
def test_to_latex_no_bold_rows(self): # GH 16707 df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) result = df.to_latex(bold_rows=False) expected = _dedent(r""" \begin{tabular}{lrl} \toprule {} & a & b \\ \midrule 0 & 1 & b1 \\ 1 & 2 & b2 \\ \bottomrule \end{tabular} """) assert result == expected
def test_to_latex_special_escape(self): df = DataFrame([r"a\b\c", r"^a^b^c", r"~a~b~c"]) result = df.to_latex() expected = _dedent(r""" \begin{tabular}{ll} \toprule {} & 0 \\ \midrule 0 & a\textbackslash b\textbackslash c \\ 1 & \textasciicircum a\textasciicircum b\textasciicircum c \\ 2 & \textasciitilde a\textasciitilde b\textasciitilde c \\ \bottomrule \end{tabular} """) assert result == expected
def test_to_latex_specified_header_with_index(self): # GH 7124 df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) result = df.to_latex(header=["AA", "BB"]) expected = _dedent(r""" \begin{tabular}{lrl} \toprule {} & AA & BB \\ \midrule 0 & 1 & b1 \\ 1 & 2 & b2 \\ \bottomrule \end{tabular} """) assert result == expected
def test_to_latex_non_string_index(self): # GH 19981 df = DataFrame([[1, 2, 3]] * 2).set_index([0, 1]) result = df.to_latex() expected = _dedent(r""" \begin{tabular}{llr} \toprule & & 2 \\ 0 & 1 & \\ \midrule 1 & 2 & 3 \\ & 2 & 3 \\ \bottomrule \end{tabular} """) assert result == expected
def save_tables(*, df: pd.DataFrame, translator: str, file: Path) -> None: """Saves to disk the tabular data of the n most relevant features of a classifier. Takes a DataFrame containing the n most relevant features and their weights. Parameters: df: pd.DataFrame - Contains two series: 'Weights' and 'Features' translator: str - Name of the translator file: Path - Feature set used to train the classifier Returns: None """ latex = df.to_latex(float_format=lambda x: "%.4f" % x) with open(TABLES_FOLDER / f"{file.stem}_{translator}.tex", "w") as f: f.write(latex)
def test_to_latex_multindex_header(self): # GH 16718 df = DataFrame({"a": [0], "b": [1], "c": [2], "d": [3]}) df = df.set_index(["a", "b"]) observed = df.to_latex(header=["r1", "r2"]) expected = _dedent(r""" \begin{tabular}{llrr} \toprule & & r1 & r2 \\ a & b & & \\ \midrule 0 & 1 & 2 & 3 \\ \bottomrule \end{tabular} """) assert observed == expected
def test_to_latex_midrule_location(self): # GH 18326 df = DataFrame({"a": [1, 2]}) df.index.name = "foo" result = df.to_latex(index_names=False) expected = _dedent(r""" \begin{tabular}{lr} \toprule {} & a \\ \midrule 0 & 1 \\ 1 & 2 \\ \bottomrule \end{tabular} """) assert result == expected
def test_to_latex_multiindex_empty_name(self): # GH 18669 mi = pd.MultiIndex.from_product([[1, 2]], names=[""]) df = DataFrame(-1, index=mi, columns=range(4)) observed = df.to_latex() expected = _dedent(r""" \begin{tabular}{lrrrr} \toprule & 0 & 1 & 2 & 3 \\ {} & & & & \\ \midrule 1 & -1 & -1 & -1 & -1 \\ 2 & -1 & -1 & -1 & -1 \\ \bottomrule \end{tabular} """) assert observed == expected
def test_to_latex_format(self, frame): # GH Bug #9402 frame.to_latex(column_format='ccc') df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']}) withindex_result = df.to_latex(column_format='ccc') withindex_expected = r"""\begin{tabular}{ccc} \toprule {} & a & b \\ \midrule 0 & 1 & b1 \\ 1 & 2 & b2 \\ \bottomrule \end{tabular} """ assert withindex_result == withindex_expected
def test_to_latex_format(self, float_frame): # GH Bug #9402 float_frame.to_latex(column_format="ccc") df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) withindex_result = df.to_latex(column_format="ccc") withindex_expected = r"""\begin{tabular}{ccc} \toprule {} & a & b \\ \midrule 0 & 1 & b1 \\ 1 & 2 & b2 \\ \bottomrule \end{tabular} """ assert withindex_result == withindex_expected
def test_to_latex_decimal(self, frame): # GH 12031 frame.to_latex() df = DataFrame({'a': [1.0, 2.1], 'b': ['b1', 'b2']}) withindex_result = df.to_latex(decimal=',') withindex_expected = r"""\begin{tabular}{lrl} \toprule {} & a & b \\ \midrule 0 & 1,0 & b1 \\ 1 & 2,1 & b2 \\ \bottomrule \end{tabular} """ assert withindex_result == withindex_expected
def test_to_latex_position(self): the_position = "h" df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) result = df.to_latex(position=the_position) expected = _dedent(r""" \begin{table}[h] \centering \begin{tabular}{lrl} \toprule {} & a & b \\ \midrule 0 & 1 & b1 \\ 1 & 2 & b2 \\ \bottomrule \end{tabular} \end{table} """) assert result == expected
def test_to_latex_escape_special_chars(self): special_characters = ['&', '%', '$', '#', '_', '{', '}', '~', '^', '\\'] df = DataFrame(data=special_characters) observed = df.to_latex() expected = r"""\begin{tabular}{ll} \toprule {} & 0 \\ \midrule 0 & \& \\ 1 & \% \\ 2 & \$ \\ 3 & \# \\ 4 & \_ \\ 5 & \{ \\ 6 & \} \\ 7 & \textasciitilde \\ 8 & \textasciicircum \\ 9 & \textbackslash \\ \bottomrule \end{tabular} """ assert observed == expected
def test_to_latex_filename(self, frame): with tm.ensure_clean('test.tex') as path: frame.to_latex(path) with open(path, 'r') as f: assert frame.to_latex() == f.read() # test with utf-8 and encoding option (GH 7061) df = DataFrame([['au\xdfgangen']]) with tm.ensure_clean('test.tex') as path: df.to_latex(path, encoding='utf-8') with codecs.open(path, 'r', encoding='utf-8') as f: assert df.to_latex() == f.read() # test with utf-8 without encoding option with tm.ensure_clean('test.tex') as path: df.to_latex(path) with codecs.open(path, 'r', encoding='utf-8') as f: assert df.to_latex() == f.read()
from data import DATAP, INDICATORS from json import load from pandas import DataFrame, Series f = open(DATAP + '/olangdict.json', 'r', encoding="UTF8") ld = load(f) inds = INDICATORS df = DataFrame(columns=['Articles', 'Ex', 'Positive', 'Solo'], index=inds) for ind in inds: sls = [l for l in ld if ld[l][ind] == 1] sls_only = [l for l in ld if ld[l][ind] == 1 and not any(ld[l][p] == 1 for p in inds if p is not ind)] print(ind + ": " + str(len(sls_only)) + "/" + str(len(sls))) df.loc[ind] = Series({'Articles': len(ld.keys()), 'Ex': 0, 'Positive': len(sls), 'Solo': len(sls_only)}) print(df.to_latex()) print(len(ld.keys())) sls = len([l for l in ld if "DbpediaInfoboxTemplate" in ld[l]]) print(sls) sls = len([l for l in ld if '(' in l]) print(sls)
def test_to_latex_multiindex(self): df = DataFrame({('x', 'y'): ['a']}) result = df.to_latex() expected = r"""\begin{tabular}{ll} \toprule {} & x \\ {} & y \\ \midrule 0 & a \\ \bottomrule \end{tabular} """ assert result == expected result = df.T.to_latex() expected = r"""\begin{tabular}{lll} \toprule & & 0 \\ \midrule x & y & a \\ \bottomrule \end{tabular} """ assert result == expected df = DataFrame.from_dict({ ('c1', 0): pd.Series(dict((x, x) for x in range(4))), ('c1', 1): pd.Series(dict((x, x + 4) for x in range(4))), ('c2', 0): pd.Series(dict((x, x) for x in range(4))), ('c2', 1): pd.Series(dict((x, x + 4) for x in range(4))), ('c3', 0): pd.Series(dict((x, x) for x in range(4))), }).T result = df.to_latex() expected = r"""\begin{tabular}{llrrrr} \toprule & & 0 & 1 & 2 & 3 \\ \midrule c1 & 0 & 0 & 1 & 2 & 3 \\ & 1 & 4 & 5 & 6 & 7 \\ c2 & 0 & 0 & 1 & 2 & 3 \\ & 1 & 4 & 5 & 6 & 7 \\ c3 & 0 & 0 & 1 & 2 & 3 \\ \bottomrule \end{tabular} """ assert result == expected # GH 14184 df = df.T df.columns.names = ['a', 'b'] result = df.to_latex() expected = r"""\begin{tabular}{lrrrrr} \toprule a & \multicolumn{2}{l}{c1} & \multicolumn{2}{l}{c2} & c3 \\ b & 0 & 1 & 0 & 1 & 0 \\ \midrule 0 & 0 & 4 & 0 & 4 & 0 \\ 1 & 1 & 5 & 1 & 5 & 1 \\ 2 & 2 & 6 & 2 & 6 & 2 \\ 3 & 3 & 7 & 3 & 7 & 3 \\ \bottomrule \end{tabular} """ assert result == expected # GH 10660 df = pd.DataFrame({'a': [0, 0, 1, 1], 'b': list('abab'), 'c': [1, 2, 3, 4]}) result = df.set_index(['a', 'b']).to_latex() expected = r"""\begin{tabular}{llr} \toprule & & c \\ a & b & \\ \midrule 0 & a & 1 \\ & b & 2 \\ 1 & a & 3 \\ & b & 4 \\ \bottomrule \end{tabular} """ assert result == expected result = df.groupby('a').describe().to_latex() expected = r"""\begin{tabular}{lrrrrrrrr} \toprule {} & \multicolumn{8}{l}{c} \\ {} & count & mean & std & min & 25\% & 50\% & 75\% & max \\ a & & & & & & & & \\ \midrule 0 & 2.0 & 1.5 & 0.707107 & 1.0 & 1.25 & 1.5 & 1.75 & 2.0 \\ 1 & 2.0 & 3.5 & 0.707107 & 3.0 & 3.25 & 3.5 & 3.75 & 4.0 \\ \bottomrule \end{tabular} """ assert result == expected
p.xlim([-100, 100]) p.legend() p.ylim([-0.1, 1.1]) p.grid() p.draw() p.savefig(paper1_figures_path("total_profile_corrected_velocity_rotsub_hi_fit.pdf")) p.savefig(paper1_figures_path("total_profile_corrected_velocity_rotsub_hi_fit.png")) # raw_input("Next plot?") p.clf() # Save parameter table hi_param_df = DataFrame({"Params": parvals, "Errors": [np.sqrt(cov[i, i]) for i in range(cov.shape[0])]}, index=parnames) hi_param_df.to_latex(paper1_tables_path("hi_gaussian_totalprof_fits.tex")) hi_param_df.to_csv(fourteenB_HI_data_path("tables/hi_gaussian_totalprof_fits.csv", no_check=True)) # Lorentzian model g_HI_init_lor = models.Lorentz1D(amplitude=1., x_0=0., fwhm=5.) fit_g_lor = fitting.LevMarLSQFitter() g_HI_lor = fit_g_lor(g_HI_init_lor, vels, norm_intens) cov = fit_g_lor.fit_info['param_cov'] parnames = [n for n in g_HI_lor.param_names] parvals = [v for (n, v) in zip(g_HI_lor.param_names, g_HI_lor.parameters) if n in parnames]
# Disregard outside this velocity range vmax = -100 * u.km / u.s vmin = -300 * u.km / u.s spec_axis = cube.spectral_axis.to(u.km / u.s) good_chans = np.logical_and(spec_axis <= vmax, spec_axis >= vmin) # Same channel width in both chan_width = \ np.abs(cube.spectral_axis[1] - cube.spectral_axis[0]).to(u.km / u.s) total_line_flux = np.nansum(total_spectrum[good_chans]) * chan_width total_line_flux_arecibo = \ np.nansum(total_spectrum_arecibo[good_chans]) * chan_width # Distance is 0.84 Mpc mass_conversion = 2.36e5 * (gal.distance.to(u.Mpc).value ** 2) * \ (u.solMass / (u.Jy * u.km / u.s)) # These estimates are low by a factor of ~0.85. Not entirely sure why, but # the estimate from the moment 0 is correct (comparing to Putman+09) total_mass = total_line_flux * mass_conversion total_mass_arecibo = total_line_flux_arecibo * mass_conversion print("VLA HI Total Mass: {}".format(total_mass)) print("Arecibo HI Total Mass: {}".format(total_mass_arecibo)) df = DataFrame({"VLA Mass": [total_mass.value], "Arecibo Mass": [total_mass_arecibo.value]}) df.to_latex(paper1_tables_path("hi_masses.tex")) df.to_csv(fourteenB_HI_data_path("tables/hi_masses.csv", no_check=True))