Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/dataframe/test_show.py
7884 views
1
import pytest
2
3
import polars as pl
4
from polars.config import TableFormatNames
5
6
7
def test_df_show_default(capsys: pytest.CaptureFixture[str]) -> None:
8
df = pl.DataFrame(
9
{
10
"foo": [1, 2, 3, 4, 5, 6, 7],
11
"bar": ["a", "b", "c", "d", "e", "f", "g"],
12
}
13
)
14
15
df.show()
16
out, _ = capsys.readouterr()
17
assert (
18
out
19
== """shape: (5, 2)
20
┌─────┬─────┐
21
│ foo ┆ bar │
22
│ --- ┆ --- │
23
│ i64 ┆ str │
24
╞═════╪═════╡
25
│ 1 ┆ a │
26
│ 2 ┆ b │
27
│ 3 ┆ c │
28
│ 4 ┆ d │
29
│ 5 ┆ e │
30
└─────┴─────┘
31
"""
32
)
33
34
35
def test_df_show_positive_limit(capsys: pytest.CaptureFixture[str]) -> None:
36
df = pl.DataFrame(
37
{
38
"foo": [1, 2, 3, 4, 5, 6, 7],
39
"bar": ["a", "b", "c", "d", "e", "f", "g"],
40
}
41
)
42
43
df.show(3)
44
out, _ = capsys.readouterr()
45
assert (
46
out
47
== """shape: (3, 2)
48
┌─────┬─────┐
49
│ foo ┆ bar │
50
│ --- ┆ --- │
51
│ i64 ┆ str │
52
╞═════╪═════╡
53
│ 1 ┆ a │
54
│ 2 ┆ b │
55
│ 3 ┆ c │
56
└─────┴─────┘
57
"""
58
)
59
60
61
def test_df_show_negative_limit(capsys: pytest.CaptureFixture[str]) -> None:
62
df = pl.DataFrame(
63
{
64
"foo": [1, 2, 3, 4, 5, 6, 7],
65
"bar": ["a", "b", "c", "d", "e", "f", "g"],
66
}
67
)
68
69
df.show(-5)
70
out, _ = capsys.readouterr()
71
assert (
72
out
73
== """shape: (2, 2)
74
┌─────┬─────┐
75
│ foo ┆ bar │
76
│ --- ┆ --- │
77
│ i64 ┆ str │
78
╞═════╪═════╡
79
│ 1 ┆ a │
80
│ 2 ┆ b │
81
└─────┴─────┘
82
"""
83
)
84
85
86
def test_df_show_no_limit(capsys: pytest.CaptureFixture[str]) -> None:
87
df = pl.DataFrame(
88
{
89
"foo": [1, 2, 3, 4, 5, 6, 7],
90
"bar": ["a", "b", "c", "d", "e", "f", "g"],
91
}
92
)
93
94
df.show(limit=None)
95
out, _ = capsys.readouterr()
96
assert (
97
out
98
== """shape: (7, 2)
99
┌─────┬─────┐
100
│ foo ┆ bar │
101
│ --- ┆ --- │
102
│ i64 ┆ str │
103
╞═════╪═════╡
104
│ 1 ┆ a │
105
│ 2 ┆ b │
106
│ 3 ┆ c │
107
│ 4 ┆ d │
108
│ 5 ┆ e │
109
│ 6 ┆ f │
110
│ 7 ┆ g │
111
└─────┴─────┘
112
"""
113
)
114
115
116
def test_df_show_ascii_tables(capsys: pytest.CaptureFixture[str]) -> None:
117
df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})
118
119
with pl.Config(ascii_tables=False):
120
df.show(ascii_tables=True)
121
out, _ = capsys.readouterr()
122
assert (
123
out
124
== """shape: (3, 2)
125
+-----+-------+
126
| abc | xyz |
127
| --- | --- |
128
| f64 | bool |
129
+=============+
130
| 1.0 | true |
131
| 2.5 | false |
132
| 5.0 | true |
133
+-----+-------+
134
"""
135
)
136
137
138
@pytest.mark.parametrize(
139
("ascii_tables", "tbl_formatting"),
140
[
141
(True, "ASCII_FULL_CONDENSED"),
142
(True, "UTF8_FULL_CONDENSED"),
143
(False, "ASCII_FULL_CONDENSED"),
144
(False, "UTF8_FULL_CONDENSED"),
145
],
146
)
147
def test_df_show_cannot_set_ascii_tables_and_tbl_formatting(
148
ascii_tables: bool, tbl_formatting: TableFormatNames
149
) -> None:
150
df = pl.DataFrame()
151
152
with pytest.raises(ValueError):
153
df.show(ascii_tables=ascii_tables, tbl_formatting=tbl_formatting)
154
155
156
def test_df_show_decimal_separator(capsys: pytest.CaptureFixture[str]) -> None:
157
df = pl.DataFrame({"v": [9876.54321, 1010101.0, -123456.78]})
158
159
with pl.Config(decimal_separator="."):
160
df.show(decimal_separator=",")
161
out, _ = capsys.readouterr()
162
assert (
163
out
164
== """shape: (3, 1)
165
┌────────────┐
166
│ v │
167
│ --- │
168
│ f64 │
169
╞════════════╡
170
│ 9876,54321 │
171
│ 1,010101e6 │
172
│ -123456,78 │
173
└────────────┘
174
"""
175
)
176
177
178
def test_df_show_thousands_separator(capsys: pytest.CaptureFixture[str]) -> None:
179
df = pl.DataFrame({"v": [9876.54321, 1010101.0, -123456.78]})
180
181
with pl.Config(thousands_separator="."):
182
df.show(thousands_separator=" ")
183
out, _ = capsys.readouterr()
184
assert (
185
out
186
== """shape: (3, 1)
187
┌─────────────┐
188
│ v │
189
│ --- │
190
│ f64 │
191
╞═════════════╡
192
│ 9 876.54321 │
193
│ 1.010101e6 │
194
│ -123 456.78 │
195
└─────────────┘
196
"""
197
)
198
199
200
def test_df_show_float_precision(capsys: pytest.CaptureFixture[str]) -> None:
201
from math import e, pi
202
203
df = pl.DataFrame({"const": ["pi", "e"], "value": [pi, e]})
204
205
with pl.Config(float_precision=8):
206
df.show(float_precision=15)
207
out, _ = capsys.readouterr()
208
assert (
209
out
210
== """shape: (2, 2)
211
┌───────┬───────────────────┐
212
│ const ┆ value │
213
│ --- ┆ --- │
214
│ str ┆ f64 │
215
╞═══════╪═══════════════════╡
216
│ pi ┆ 3.141592653589793 │
217
│ e ┆ 2.718281828459045 │
218
└───────┴───────────────────┘
219
"""
220
)
221
222
df.show(float_precision=3)
223
out, _ = capsys.readouterr()
224
assert (
225
out
226
== """shape: (2, 2)
227
┌───────┬───────┐
228
│ const ┆ value │
229
│ --- ┆ --- │
230
│ str ┆ f64 │
231
╞═══════╪═══════╡
232
│ pi ┆ 3.142 │
233
│ e ┆ 2.718 │
234
└───────┴───────┘
235
"""
236
)
237
238
239
def test_df_show_fmt_float(capsys: pytest.CaptureFixture[str]) -> None:
240
df = pl.DataFrame({"num": [1.2304980958725870923, 1e6, 1e-8]})
241
242
with pl.Config(fmt_float="full"):
243
df.show(fmt_float="mixed")
244
out, _ = capsys.readouterr()
245
assert (
246
out
247
== """shape: (3, 1)
248
┌───────────┐
249
│ num │
250
│ --- │
251
│ f64 │
252
╞═══════════╡
253
│ 1.230498 │
254
│ 1e6 │
255
│ 1.0000e-8 │
256
└───────────┘
257
"""
258
)
259
260
261
def test_df_show_fmt_str_lengths(capsys: pytest.CaptureFixture[str]) -> None:
262
df = pl.DataFrame(
263
{
264
"txt": [
265
"Play it, Sam. Play 'As Time Goes By'.",
266
"This is the beginning of a beautiful friendship.",
267
]
268
}
269
)
270
271
with pl.Config(fmt_str_lengths=20):
272
df.show(fmt_str_lengths=10)
273
out, _ = capsys.readouterr()
274
assert (
275
out
276
== """shape: (2, 1)
277
┌─────────────┐
278
│ txt │
279
│ --- │
280
│ str │
281
╞═════════════╡
282
│ Play it, S… │
283
│ This is th… │
284
└─────────────┘
285
"""
286
)
287
288
df.show(fmt_str_lengths=50)
289
out, _ = capsys.readouterr()
290
assert (
291
out
292
== """shape: (2, 1)
293
┌──────────────────────────────────────────────────┐
294
│ txt │
295
│ --- │
296
│ str │
297
╞══════════════════════════════════════════════════╡
298
│ Play it, Sam. Play 'As Time Goes By'. │
299
│ This is the beginning of a beautiful friendship. │
300
└──────────────────────────────────────────────────┘
301
"""
302
)
303
304
305
def test_df_show_fmt_table_cell_list_len(capsys: pytest.CaptureFixture[str]) -> None:
306
df = pl.DataFrame({"nums": [list(range(10))]})
307
308
with pl.Config(fmt_table_cell_list_len=5):
309
df.show(fmt_table_cell_list_len=2)
310
out, _ = capsys.readouterr()
311
assert (
312
out
313
== """shape: (1, 1)
314
┌───────────┐
315
│ nums │
316
│ --- │
317
│ list[i64] │
318
╞═══════════╡
319
│ [0, … 9] │
320
└───────────┘
321
"""
322
)
323
324
df.show(fmt_table_cell_list_len=8)
325
out, _ = capsys.readouterr()
326
assert (
327
out
328
== """shape: (1, 1)
329
┌────────────────────────────┐
330
│ nums │
331
│ --- │
332
│ list[i64] │
333
╞════════════════════════════╡
334
│ [0, 1, 2, 3, 4, 5, 6, … 9] │
335
└────────────────────────────┘
336
"""
337
)
338
339
340
def test_df_show_tbl_cell_alignment(capsys: pytest.CaptureFixture[str]) -> None:
341
df = pl.DataFrame(
342
{"column_abc": [1.0, 2.5, 5.0], "column_xyz": [True, False, True]}
343
)
344
345
with pl.Config(tbl_cell_alignment="LEFT"):
346
df.show(tbl_cell_alignment="RIGHT")
347
out, _ = capsys.readouterr()
348
assert (
349
out
350
== """shape: (3, 2)
351
┌────────────┬────────────┐
352
│ column_abc ┆ column_xyz │
353
│ --- ┆ --- │
354
│ f64 ┆ bool │
355
╞════════════╪════════════╡
356
│ 1.0 ┆ true │
357
│ 2.5 ┆ false │
358
│ 5.0 ┆ true │
359
└────────────┴────────────┘
360
"""
361
)
362
363
364
def test_df_show_tbl_cell_numeric_alignment(capsys: pytest.CaptureFixture[str]) -> None:
365
from datetime import date
366
367
df = pl.DataFrame(
368
{
369
"abc": [11, 2, 333],
370
"mno": [date(2023, 10, 29), None, date(2001, 7, 5)],
371
"xyz": [True, False, None],
372
}
373
)
374
375
with pl.Config(tbl_cell_numeric_alignment="LEFT"):
376
df.show(tbl_cell_numeric_alignment="RIGHT")
377
out, _ = capsys.readouterr()
378
assert (
379
out
380
== """shape: (3, 3)
381
┌─────┬────────────┬───────┐
382
│ abc ┆ mno ┆ xyz │
383
│ --- ┆ --- ┆ --- │
384
│ i64 ┆ date ┆ bool │
385
╞═════╪════════════╪═══════╡
386
│ 11 ┆ 2023-10-29 ┆ true │
387
│ 2 ┆ null ┆ false │
388
│ 333 ┆ 2001-07-05 ┆ null │
389
└─────┴────────────┴───────┘
390
"""
391
)
392
393
394
def test_df_show_tbl_cols(capsys: pytest.CaptureFixture[str]) -> None:
395
df = pl.DataFrame({str(i): [i] for i in range(10)})
396
397
with pl.Config(tbl_cols=2):
398
df.show(tbl_cols=3)
399
out, _ = capsys.readouterr()
400
assert (
401
out
402
== """shape: (1, 10)
403
┌─────┬─────┬───┬─────┐
404
│ 0 ┆ 1 ┆ … ┆ 9 │
405
│ --- ┆ --- ┆ ┆ --- │
406
│ i64 ┆ i64 ┆ ┆ i64 │
407
╞═════╪═════╪═══╪═════╡
408
│ 0 ┆ 1 ┆ … ┆ 9 │
409
└─────┴─────┴───┴─────┘
410
"""
411
)
412
413
df.show(tbl_cols=7)
414
out, _ = capsys.readouterr()
415
assert (
416
out
417
== """shape: (1, 10)
418
┌─────┬─────┬─────┬─────┬───┬─────┬─────┬─────┐
419
│ 0 ┆ 1 ┆ 2 ┆ 3 ┆ … ┆ 7 ┆ 8 ┆ 9 │
420
│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- │
421
│ i64 ┆ i64 ┆ i64 ┆ i64 ┆ ┆ i64 ┆ i64 ┆ i64 │
422
╞═════╪═════╪═════╪═════╪═══╪═════╪═════╪═════╡
423
│ 0 ┆ 1 ┆ 2 ┆ 3 ┆ … ┆ 7 ┆ 8 ┆ 9 │
424
└─────┴─────┴─────┴─────┴───┴─────┴─────┴─────┘
425
"""
426
)
427
428
429
def test_df_show_tbl_column_data_type_inline(
430
capsys: pytest.CaptureFixture[str],
431
) -> None:
432
df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})
433
434
with pl.Config(tbl_column_data_type_inline=False):
435
df.show(tbl_column_data_type_inline=True)
436
out, _ = capsys.readouterr()
437
assert (
438
out
439
== """shape: (3, 2)
440
┌───────────┬────────────┐
441
│ abc (f64) ┆ xyz (bool) │
442
╞═══════════╪════════════╡
443
│ 1.0 ┆ true │
444
│ 2.5 ┆ false │
445
│ 5.0 ┆ true │
446
└───────────┴────────────┘
447
"""
448
)
449
450
451
def test_df_show_tbl_dataframe_shape_below(capsys: pytest.CaptureFixture[str]) -> None:
452
df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})
453
454
with pl.Config(tbl_dataframe_shape_below=False):
455
df.show(tbl_dataframe_shape_below=True)
456
out, _ = capsys.readouterr()
457
assert out == (
458
"┌─────┬───────┐\n"
459
"│ abc ┆ xyz │\n"
460
"│ --- ┆ --- │\n"
461
"│ f64 ┆ bool │\n"
462
"╞═════╪═══════╡\n"
463
"│ 1.0 ┆ true │\n"
464
"│ 2.5 ┆ false │\n"
465
"│ 5.0 ┆ true │\n"
466
"└─────┴───────┘\n"
467
"shape: (3, 2)\n"
468
)
469
470
471
def test_df_show_tbl_formatting(capsys: pytest.CaptureFixture[str]) -> None:
472
df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
473
474
with pl.Config(tbl_formatting="UTF8_FULL"):
475
df.show(tbl_formatting="ASCII_FULL")
476
out, _ = capsys.readouterr()
477
assert (
478
out
479
== """shape: (3, 3)
480
+-----+-----+-----+
481
| a | b | c |
482
| --- | --- | --- |
483
| i64 | i64 | i64 |
484
+=================+
485
| 1 | 4 | 7 |
486
|-----+-----+-----|
487
| 2 | 5 | 8 |
488
|-----+-----+-----|
489
| 3 | 6 | 9 |
490
+-----+-----+-----+
491
"""
492
)
493
494
df.show(tbl_formatting="MARKDOWN")
495
out, _ = capsys.readouterr()
496
assert (
497
out
498
== """shape: (3, 3)
499
| a | b | c |
500
| --- | --- | --- |
501
| i64 | i64 | i64 |
502
|-----|-----|-----|
503
| 1 | 4 | 7 |
504
| 2 | 5 | 8 |
505
| 3 | 6 | 9 |
506
"""
507
)
508
509
510
def test_df_show_tbl_hide_column_data_types(capsys: pytest.CaptureFixture[str]) -> None:
511
df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})
512
513
with pl.Config(tbl_hide_column_data_types=False):
514
df.show(tbl_hide_column_data_types=True)
515
out, _ = capsys.readouterr()
516
assert (
517
out
518
== """shape: (3, 2)
519
┌─────┬───────┐
520
│ abc ┆ xyz │
521
╞═════╪═══════╡
522
│ 1.0 ┆ true │
523
│ 2.5 ┆ false │
524
│ 5.0 ┆ true │
525
└─────┴───────┘
526
"""
527
)
528
529
530
def test_df_show_tbl_hide_column_names(capsys: pytest.CaptureFixture[str]) -> None:
531
df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})
532
533
with pl.Config(tbl_hide_column_names=False):
534
df.show(tbl_hide_column_names=True)
535
out, _ = capsys.readouterr()
536
assert (
537
out
538
== """shape: (3, 2)
539
┌─────┬───────┐
540
│ f64 ┆ bool │
541
╞═════╪═══════╡
542
│ 1.0 ┆ true │
543
│ 2.5 ┆ false │
544
│ 5.0 ┆ true │
545
└─────┴───────┘
546
"""
547
)
548
549
550
def test_df_show_tbl_hide_dtype_separator(capsys: pytest.CaptureFixture[str]) -> None:
551
df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})
552
553
with pl.Config(tbl_hide_dtype_separator=False):
554
df.show(tbl_hide_dtype_separator=True)
555
out, _ = capsys.readouterr()
556
assert (
557
out
558
== """shape: (3, 2)
559
┌─────┬───────┐
560
│ abc ┆ xyz │
561
│ f64 ┆ bool │
562
╞═════╪═══════╡
563
│ 1.0 ┆ true │
564
│ 2.5 ┆ false │
565
│ 5.0 ┆ true │
566
└─────┴───────┘
567
"""
568
)
569
570
571
def test_df_show_tbl_hide_dataframe_shape(capsys: pytest.CaptureFixture[str]) -> None:
572
df = pl.DataFrame({"abc": [1.0, 2.5, 5.0], "xyz": [True, False, True]})
573
574
with pl.Config(tbl_hide_dataframe_shape=False):
575
df.show(tbl_hide_dataframe_shape=True)
576
out, _ = capsys.readouterr()
577
assert out == (
578
"┌─────┬───────┐\n"
579
"│ abc ┆ xyz │\n"
580
"│ --- ┆ --- │\n"
581
"│ f64 ┆ bool │\n"
582
"╞═════╪═══════╡\n"
583
"│ 1.0 ┆ true │\n"
584
"│ 2.5 ┆ false │\n"
585
"│ 5.0 ┆ true │\n"
586
"└─────┴───────┘\n"
587
)
588
589
590
def test_df_show_tbl_width_chars(capsys: pytest.CaptureFixture[str]) -> None:
591
df = pl.DataFrame(
592
{
593
"id": ["SEQ1", "SEQ2"],
594
"seq": ["ATGATAAAGGAG", "GCAACGCATATA"],
595
}
596
)
597
598
with pl.Config(tbl_width_chars=100):
599
df.show(tbl_width_chars=12)
600
out, _ = capsys.readouterr()
601
assert (
602
out
603
== """shape: (2, 2)
604
┌─────┬─────┐
605
│ id ┆ seq │
606
│ --- ┆ --- │
607
│ str ┆ str │
608
╞═════╪═════╡
609
│ SEQ ┆ ATG │
610
│ 1 ┆ ATA │
611
│ ┆ AAG │
612
│ ┆ GAG │
613
│ SEQ ┆ GCA │
614
│ 2 ┆ ACG │
615
│ ┆ CAT │
616
│ ┆ ATA │
617
└─────┴─────┘
618
"""
619
)
620
621
622
def test_df_show_trim_decimal_zeros(capsys: pytest.CaptureFixture[str]) -> None:
623
from decimal import Decimal as D
624
625
df = pl.DataFrame(
626
data={"d": [D("1.01000"), D("-5.67890")]},
627
schema={"d": pl.Decimal(scale=5)},
628
)
629
630
with pl.Config(trim_decimal_zeros=False):
631
df.show(trim_decimal_zeros=True)
632
out, _ = capsys.readouterr()
633
assert (
634
out
635
== """shape: (2, 1)
636
┌───────────────┐
637
│ d │
638
│ --- │
639
│ decimal[38,5] │
640
╞═══════════════╡
641
│ 1.01 │
642
│ -5.6789 │
643
└───────────────┘
644
"""
645
)
646
647