Debian Codegraph @

tests/test_df.py

raw
1 import pandas as pd
2 import pytest
3
4 fastparquet = pytest.importorskip("fastparquet")
5 import fsspec
6 import ujson
7
8 from kerchunk.df import refs_to_dataframe
9
10
11 @pytest.mark.parametrize("url", [True, False])
12 def test_1(m, url):
13 refs = {
14 "a/0": ["memory://url1.file"],
15 "a/1": ["memory://url1.file", 10, 100],
16 "a/2": ["memory://url2.file"],
17 "a/3": ["memory://url3.file"],
18 "a/4": ["memory://url4.file"],
19 "a/5": ["memory://url5.file"],
20 "a/6": b"data",
21 "a/.zarray": b"""{"shape": [7], "chunks":[1], "filters": [], "compressor": null}""",
22 ".zgroup": b'{"zarr_format": 2}',
23 }
24 u = "memory://myrefs.json"
25 if url:
26 with fsspec.open(u, "wt") as f:
27 ujson.dump(
28 {
29 k: (v.decode() if isinstance(v, bytes) else v)
30 for k, v in refs.items()
31 },
32 f,
33 )
34 refs = u
35
36 refs_to_dataframe(refs, "memory://outdir", record_size=4)
37 with fsspec.open("memory:///outdir/.zmetadata") as f:
38 meta = ujson.load(f)
39 assert list(meta) == ["metadata", "record_size"]
40 assert meta["record_size"] == 4
41
42 df0 = pd.read_parquet("memory:///outdir/a/refs.0.parq")
43
44 # no raw
45 assert df0.to_dict() == {
46 "offset": {0: 0, 1: 10, 2: 0, 3: 0},
47 "path": {
48 0: "memory://url1.file",
49 1: "memory://url1.file",
50 2: "memory://url2.file",
51 3: "memory://url3.file",
52 },
53 "size": {0: 0, 1: 100, 2: 0, 3: 0},
54 "raw": {0: None, 1: None, 2: None, 3: None},
55 }
56
57 # with raw column
58 df1 = pd.read_parquet("memory:///outdir/a/refs.1.parq")
59 assert df1.to_dict() == {
60 "offset": {0: 0, 1: 0, 2: 0, 3: 0},
61 "path": {0: "memory://url4.file", 1: "memory://url5.file", 2: None, 3: None},
62 "raw": {0: None, 1: None, 2: b"data", 3: None},
63 "size": {0: 0, 1: 0, 2: 0, 3: 0},
64 }