Last active
May 9, 2016 12:56
-
-
Save tschm/0b76a8bdbce6d52a01706a957e8f5bc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def formatter(str="{:0.2f}"): | |
return lambda x: str.format(x) | |
def to_latex(frame, filename, na_rep="", float_format=lambda number: "{:0.2f}".format(number)): | |
with open(filename, "w") as file: | |
frame.to_latex(buf=file, na_rep=na_rep, float_format=float_format, escape=False) | |
class TestLatex(TestCase): | |
def testFormatter(self): | |
f = formatter("{:0.3f}") | |
self.assertEqual(f(0.12345), "0.123") | |
self.assertEqual(f(0.1), "0.100") | |
self.assertEqual(f(0.12390), "0.124") | |
def testToLatex(self): | |
file = tempfile.NamedTemporaryFile() | |
frame = pd.DataFrame(data=[[2.0, 3.0], [1.0, 4.0]]) | |
to_latex(frame=frame, filename=file.name) | |
with open(file.name, "r") as file: | |
lines = [line for line in file.readlines()] | |
self.assertListEqual(lines, ['\\begin{tabular}{lrr}\n', '\\toprule\n', '{} & 0 & 1 \\\\\n', '\\midrule\n', '0 & 2.00 & 3.00 \\\\\n', '1 & 1.00 & 4.00 \\\\\n', '\\bottomrule\n', '\\end{tabular}\n']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment