Path: blob/main/py-polars/tests/unit/dataframe/test_show.py
7884 views
import pytest12import polars as pl3from polars.config import TableFormatNames456def test_df_show_default(capsys: pytest.CaptureFixture[str]) -> None:7df = pl.DataFrame(8{9"foo": [1, 2, 3, 4, 5, 6, 7],10"bar": ["a", "b", "c", "d", "e", "f", "g"],11}12)1314df.show()15out, _ = capsys.readouterr()16assert (17out18== """shape: (5, 2)19┌─────┬─────┐20│ foo ┆ bar │21│ --- ┆ --- │22│ i64 ┆ str │23╞═════╪═════╡24│ 1 ┆ a │25│ 2 ┆ b │26│ 3 ┆ c │27│ 4 ┆ d │28│ 5 ┆ e │29└─────┴─────┘30"""31)323334def test_df_show_positive_limit(capsys: pytest.CaptureFixture[str]) -> None:35df = pl.DataFrame(36{37"foo": [1, 2, 3, 4, 5, 6, 7],38"bar": ["a", "b", "c", "d", "e", "f", "g"],39}40)4142df.show(3)43out, _ = capsys.readouterr()44assert (45out46== """shape: (3, 2)47┌─────┬─────┐48│ foo ┆ bar │49│ --- ┆ --- │50│ i64 ┆ str │51╞═════╪═════╡52│ 1 ┆ a │53│ 2 ┆ b │54│ 3 ┆ c │55└─────┴─────┘56"""57)585960def test_df_show_negative_limit(capsys: pytest.CaptureFixture[str]) -> None:61df = pl.DataFrame(62{63"foo": [1, 2, 3, 4, 5, 6, 7],64"bar": ["a", "b", "c", "d", "e", "f", "g"],65}66)6768df.show(-5)69out, _ = capsys.readouterr()70assert (71out72== """shape: (2, 2)73┌─────┬─────┐74│ foo ┆ bar │75│ --- ┆ --- │76│ i64 ┆ str │77╞═════╪═════╡78│ 1 ┆ a │79│ 2 ┆ b │80└─────┴─────┘81"""82)838485def test_df_show_no_limit(capsys: pytest.CaptureFixture[str]) -> None:86df = pl.DataFrame(87{88"foo": [1, 2, 3, 4, 5, 6, 7],89"bar": ["a", "b", "c", "d", "e", "f", "g"],90}91)9293df.show(limit=None)94out, _ = capsys.readouterr()95assert (96out97== """shape: (7, 2)98┌─────┬─────┐99│ foo ┆ bar │100│ --- ┆ --- │101│ i64 ┆ str │102╞═════╪═════╡103│ 1 ┆ a │104│ 2 ┆ b │105│ 3 ┆ c │106│ 4 ┆ d │107│ 5 ┆ e │108│ 6 ┆ f │109│ 7 ┆ g │110└─────┴─────┘111"""112)113114115def test_df_show_ascii_tables(capsys: pytest.CaptureFixture[str]) -> None:116df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})117118with pl.Config(ascii_tables=False):119df.show(ascii_tables=True)120out, _ = capsys.readouterr()121assert (122out123== """shape: (3, 2)124+-----+-------+125| abc | xyz |126| --- | --- |127| f64 | bool |128+=============+129| 1.0 | true |130| 2.5 | false |131| 5.0 | true |132+-----+-------+133"""134)135136137@pytest.mark.parametrize(138("ascii_tables", "tbl_formatting"),139[140(True, "ASCII_FULL_CONDENSED"),141(True, "UTF8_FULL_CONDENSED"),142(False, "ASCII_FULL_CONDENSED"),143(False, "UTF8_FULL_CONDENSED"),144],145)146def test_df_show_cannot_set_ascii_tables_and_tbl_formatting(147ascii_tables: bool, tbl_formatting: TableFormatNames148) -> None:149df = pl.DataFrame()150151with pytest.raises(ValueError):152df.show(ascii_tables=ascii_tables, tbl_formatting=tbl_formatting)153154155def test_df_show_decimal_separator(capsys: pytest.CaptureFixture[str]) -> None:156df = pl.DataFrame({"v": [9876.54321, 1010101.0, -123456.78]})157158with pl.Config(decimal_separator="."):159df.show(decimal_separator=",")160out, _ = capsys.readouterr()161assert (162out163== """shape: (3, 1)164┌────────────┐165│ v │166│ --- │167│ f64 │168╞════════════╡169│ 9876,54321 │170│ 1,010101e6 │171│ -123456,78 │172└────────────┘173"""174)175176177def test_df_show_thousands_separator(capsys: pytest.CaptureFixture[str]) -> None:178df = pl.DataFrame({"v": [9876.54321, 1010101.0, -123456.78]})179180with pl.Config(thousands_separator="."):181df.show(thousands_separator=" ")182out, _ = capsys.readouterr()183assert (184out185== """shape: (3, 1)186┌─────────────┐187│ v │188│ --- │189│ f64 │190╞═════════════╡191│ 9 876.54321 │192│ 1.010101e6 │193│ -123 456.78 │194└─────────────┘195"""196)197198199def test_df_show_float_precision(capsys: pytest.CaptureFixture[str]) -> None:200from math import e, pi201202df = pl.DataFrame({"const": ["pi", "e"], "value": [pi, e]})203204with pl.Config(float_precision=8):205df.show(float_precision=15)206out, _ = capsys.readouterr()207assert (208out209== """shape: (2, 2)210┌───────┬───────────────────┐211│ const ┆ value │212│ --- ┆ --- │213│ str ┆ f64 │214╞═══════╪═══════════════════╡215│ pi ┆ 3.141592653589793 │216│ e ┆ 2.718281828459045 │217└───────┴───────────────────┘218"""219)220221df.show(float_precision=3)222out, _ = capsys.readouterr()223assert (224out225== """shape: (2, 2)226┌───────┬───────┐227│ const ┆ value │228│ --- ┆ --- │229│ str ┆ f64 │230╞═══════╪═══════╡231│ pi ┆ 3.142 │232│ e ┆ 2.718 │233└───────┴───────┘234"""235)236237238def test_df_show_fmt_float(capsys: pytest.CaptureFixture[str]) -> None:239df = pl.DataFrame({"num": [1.2304980958725870923, 1e6, 1e-8]})240241with pl.Config(fmt_float="full"):242df.show(fmt_float="mixed")243out, _ = capsys.readouterr()244assert (245out246== """shape: (3, 1)247┌───────────┐248│ num │249│ --- │250│ f64 │251╞═══════════╡252│ 1.230498 │253│ 1e6 │254│ 1.0000e-8 │255└───────────┘256"""257)258259260def test_df_show_fmt_str_lengths(capsys: pytest.CaptureFixture[str]) -> None:261df = pl.DataFrame(262{263"txt": [264"Play it, Sam. Play 'As Time Goes By'.",265"This is the beginning of a beautiful friendship.",266]267}268)269270with pl.Config(fmt_str_lengths=20):271df.show(fmt_str_lengths=10)272out, _ = capsys.readouterr()273assert (274out275== """shape: (2, 1)276┌─────────────┐277│ txt │278│ --- │279│ str │280╞═════════════╡281│ Play it, S… │282│ This is th… │283└─────────────┘284"""285)286287df.show(fmt_str_lengths=50)288out, _ = capsys.readouterr()289assert (290out291== """shape: (2, 1)292┌──────────────────────────────────────────────────┐293│ txt │294│ --- │295│ str │296╞══════════════════════════════════════════════════╡297│ Play it, Sam. Play 'As Time Goes By'. │298│ This is the beginning of a beautiful friendship. │299└──────────────────────────────────────────────────┘300"""301)302303304def test_df_show_fmt_table_cell_list_len(capsys: pytest.CaptureFixture[str]) -> None:305df = pl.DataFrame({"nums": [list(range(10))]})306307with pl.Config(fmt_table_cell_list_len=5):308df.show(fmt_table_cell_list_len=2)309out, _ = capsys.readouterr()310assert (311out312== """shape: (1, 1)313┌───────────┐314│ nums │315│ --- │316│ list[i64] │317╞═══════════╡318│ [0, … 9] │319└───────────┘320"""321)322323df.show(fmt_table_cell_list_len=8)324out, _ = capsys.readouterr()325assert (326out327== """shape: (1, 1)328┌────────────────────────────┐329│ nums │330│ --- │331│ list[i64] │332╞════════════════════════════╡333│ [0, 1, 2, 3, 4, 5, 6, … 9] │334└────────────────────────────┘335"""336)337338339def test_df_show_tbl_cell_alignment(capsys: pytest.CaptureFixture[str]) -> None:340df = pl.DataFrame(341{"column_abc": [1.0, 2.5, 5.0], "column_xyz": [True, False, True]}342)343344with pl.Config(tbl_cell_alignment="LEFT"):345df.show(tbl_cell_alignment="RIGHT")346out, _ = capsys.readouterr()347assert (348out349== """shape: (3, 2)350┌────────────┬────────────┐351│ column_abc ┆ column_xyz │352│ --- ┆ --- │353│ f64 ┆ bool │354╞════════════╪════════════╡355│ 1.0 ┆ true │356│ 2.5 ┆ false │357│ 5.0 ┆ true │358└────────────┴────────────┘359"""360)361362363def test_df_show_tbl_cell_numeric_alignment(capsys: pytest.CaptureFixture[str]) -> None:364from datetime import date365366df = pl.DataFrame(367{368"abc": [11, 2, 333],369"mno": [date(2023, 10, 29), None, date(2001, 7, 5)],370"xyz": [True, False, None],371}372)373374with pl.Config(tbl_cell_numeric_alignment="LEFT"):375df.show(tbl_cell_numeric_alignment="RIGHT")376out, _ = capsys.readouterr()377assert (378out379== """shape: (3, 3)380┌─────┬────────────┬───────┐381│ abc ┆ mno ┆ xyz │382│ --- ┆ --- ┆ --- │383│ i64 ┆ date ┆ bool │384╞═════╪════════════╪═══════╡385│ 11 ┆ 2023-10-29 ┆ true │386│ 2 ┆ null ┆ false │387│ 333 ┆ 2001-07-05 ┆ null │388└─────┴────────────┴───────┘389"""390)391392393def test_df_show_tbl_cols(capsys: pytest.CaptureFixture[str]) -> None:394df = pl.DataFrame({str(i): [i] for i in range(10)})395396with pl.Config(tbl_cols=2):397df.show(tbl_cols=3)398out, _ = capsys.readouterr()399assert (400out401== """shape: (1, 10)402┌─────┬─────┬───┬─────┐403│ 0 ┆ 1 ┆ … ┆ 9 │404│ --- ┆ --- ┆ ┆ --- │405│ i64 ┆ i64 ┆ ┆ i64 │406╞═════╪═════╪═══╪═════╡407│ 0 ┆ 1 ┆ … ┆ 9 │408└─────┴─────┴───┴─────┘409"""410)411412df.show(tbl_cols=7)413out, _ = capsys.readouterr()414assert (415out416== """shape: (1, 10)417┌─────┬─────┬─────┬─────┬───┬─────┬─────┬─────┐418│ 0 ┆ 1 ┆ 2 ┆ 3 ┆ … ┆ 7 ┆ 8 ┆ 9 │419│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- │420│ i64 ┆ i64 ┆ i64 ┆ i64 ┆ ┆ i64 ┆ i64 ┆ i64 │421╞═════╪═════╪═════╪═════╪═══╪═════╪═════╪═════╡422│ 0 ┆ 1 ┆ 2 ┆ 3 ┆ … ┆ 7 ┆ 8 ┆ 9 │423└─────┴─────┴─────┴─────┴───┴─────┴─────┴─────┘424"""425)426427428def test_df_show_tbl_column_data_type_inline(429capsys: pytest.CaptureFixture[str],430) -> None:431df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})432433with pl.Config(tbl_column_data_type_inline=False):434df.show(tbl_column_data_type_inline=True)435out, _ = capsys.readouterr()436assert (437out438== """shape: (3, 2)439┌───────────┬────────────┐440│ abc (f64) ┆ xyz (bool) │441╞═══════════╪════════════╡442│ 1.0 ┆ true │443│ 2.5 ┆ false │444│ 5.0 ┆ true │445└───────────┴────────────┘446"""447)448449450def test_df_show_tbl_dataframe_shape_below(capsys: pytest.CaptureFixture[str]) -> None:451df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})452453with pl.Config(tbl_dataframe_shape_below=False):454df.show(tbl_dataframe_shape_below=True)455out, _ = capsys.readouterr()456assert out == (457"┌─────┬───────┐\n"458"│ abc ┆ xyz │\n"459"│ --- ┆ --- │\n"460"│ f64 ┆ bool │\n"461"╞═════╪═══════╡\n"462"│ 1.0 ┆ true │\n"463"│ 2.5 ┆ false │\n"464"│ 5.0 ┆ true │\n"465"└─────┴───────┘\n"466"shape: (3, 2)\n"467)468469470def test_df_show_tbl_formatting(capsys: pytest.CaptureFixture[str]) -> None:471df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})472473with pl.Config(tbl_formatting="UTF8_FULL"):474df.show(tbl_formatting="ASCII_FULL")475out, _ = capsys.readouterr()476assert (477out478== """shape: (3, 3)479+-----+-----+-----+480| a | b | c |481| --- | --- | --- |482| i64 | i64 | i64 |483+=================+484| 1 | 4 | 7 |485|-----+-----+-----|486| 2 | 5 | 8 |487|-----+-----+-----|488| 3 | 6 | 9 |489+-----+-----+-----+490"""491)492493df.show(tbl_formatting="MARKDOWN")494out, _ = capsys.readouterr()495assert (496out497== """shape: (3, 3)498| a | b | c |499| --- | --- | --- |500| i64 | i64 | i64 |501|-----|-----|-----|502| 1 | 4 | 7 |503| 2 | 5 | 8 |504| 3 | 6 | 9 |505"""506)507508509def test_df_show_tbl_hide_column_data_types(capsys: pytest.CaptureFixture[str]) -> None:510df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})511512with pl.Config(tbl_hide_column_data_types=False):513df.show(tbl_hide_column_data_types=True)514out, _ = capsys.readouterr()515assert (516out517== """shape: (3, 2)518┌─────┬───────┐519│ abc ┆ xyz │520╞═════╪═══════╡521│ 1.0 ┆ true │522│ 2.5 ┆ false │523│ 5.0 ┆ true │524└─────┴───────┘525"""526)527528529def test_df_show_tbl_hide_column_names(capsys: pytest.CaptureFixture[str]) -> None:530df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})531532with pl.Config(tbl_hide_column_names=False):533df.show(tbl_hide_column_names=True)534out, _ = capsys.readouterr()535assert (536out537== """shape: (3, 2)538┌─────┬───────┐539│ f64 ┆ bool │540╞═════╪═══════╡541│ 1.0 ┆ true │542│ 2.5 ┆ false │543│ 5.0 ┆ true │544└─────┴───────┘545"""546)547548549def test_df_show_tbl_hide_dtype_separator(capsys: pytest.CaptureFixture[str]) -> None:550df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})551552with pl.Config(tbl_hide_dtype_separator=False):553df.show(tbl_hide_dtype_separator=True)554out, _ = capsys.readouterr()555assert (556out557== """shape: (3, 2)558┌─────┬───────┐559│ abc ┆ xyz │560│ f64 ┆ bool │561╞═════╪═══════╡562│ 1.0 ┆ true │563│ 2.5 ┆ false │564│ 5.0 ┆ true │565└─────┴───────┘566"""567)568569570def test_df_show_tbl_hide_dataframe_shape(capsys: pytest.CaptureFixture[str]) -> None:571df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})572573with pl.Config(tbl_hide_dataframe_shape=False):574df.show(tbl_hide_dataframe_shape=True)575out, _ = capsys.readouterr()576assert out == (577"┌─────┬───────┐\n"578"│ abc ┆ xyz │\n"579"│ --- ┆ --- │\n"580"│ f64 ┆ bool │\n"581"╞═════╪═══════╡\n"582"│ 1.0 ┆ true │\n"583"│ 2.5 ┆ false │\n"584"│ 5.0 ┆ true │\n"585"└─────┴───────┘\n"586)587588589def test_df_show_tbl_width_chars(capsys: pytest.CaptureFixture[str]) -> None:590df = pl.DataFrame(591{592"id": ["SEQ1", "SEQ2"],593"seq": ["ATGATAAAGGAG", "GCAACGCATATA"],594}595)596597with pl.Config(tbl_width_chars=100):598df.show(tbl_width_chars=12)599out, _ = capsys.readouterr()600assert (601out602== """shape: (2, 2)603┌─────┬─────┐604│ id ┆ seq │605│ --- ┆ --- │606│ str ┆ str │607╞═════╪═════╡608│ SEQ ┆ ATG │609│ 1 ┆ ATA │610│ ┆ AAG │611│ ┆ GAG │612│ SEQ ┆ GCA │613│ 2 ┆ ACG │614│ ┆ CAT │615│ ┆ ATA │616└─────┴─────┘617"""618)619620621def test_df_show_trim_decimal_zeros(capsys: pytest.CaptureFixture[str]) -> None:622from decimal import Decimal as D623624df = pl.DataFrame(625data={"d": [D("1.01000"), D("-5.67890")]},626schema={"d": pl.Decimal(scale=5)},627)628629with pl.Config(trim_decimal_zeros=False):630df.show(trim_decimal_zeros=True)631out, _ = capsys.readouterr()632assert (633out634== """shape: (2, 1)635┌───────────────┐636│ d │637│ --- │638│ decimal[38,5] │639╞═══════════════╡640│ 1.01 │641│ -5.6789 │642└───────────────┘643"""644)645646647