|
| 1 | +import msgpack |
| 2 | +import numpy as np |
| 3 | +import pandas as pd |
| 4 | +import zstandard as zstd |
| 5 | + |
| 6 | + |
| 7 | +def encode_dataframe(df, ts_unit="s", zstd_level=10): |
| 8 | + # 1) timestamps |
| 9 | + ts = df.index.view("int64") |
| 10 | + if ts_unit != "ns": |
| 11 | + factor = {"s": 1_000_000_000, "ms": 1_000_000, "us": 1_000, "ns": 1}[ts_unit] |
| 12 | + ts = (ts // factor).astype("int64") |
| 13 | + ts_blob = ts.tobytes() |
| 14 | + |
| 15 | + # 2) split fixed vs. object columns |
| 16 | + obj_cols = df.select_dtypes(include=["object"]).columns.tolist() |
| 17 | + num_cols = [c for c in df.columns if c not in obj_cols] |
| 18 | + |
| 19 | + # 2a) fixed‐dtype blob |
| 20 | + if num_cols: |
| 21 | + rec = df[num_cols].to_records(index=False) |
| 22 | + num_blob = rec.tobytes() |
| 23 | + num_descr = rec.dtype.descr |
| 24 | + else: |
| 25 | + num_blob = b"" |
| 26 | + num_descr = [] |
| 27 | + |
| 28 | + # 2b) object‐dtype data (simple Python lists) |
| 29 | + obj_data = {c: df[c].tolist() for c in obj_cols} |
| 30 | + |
| 31 | + # 3) pack into ExtTypes + one metadata map |
| 32 | + p = msgpack.Packer(use_bin_type=True, strict_types=True) |
| 33 | + parts = [ |
| 34 | + p.pack(msgpack.ExtType(0, ts_blob)), |
| 35 | + p.pack(msgpack.ExtType(1, num_blob)), |
| 36 | + # Ext code 2 carries the already‐msgpacked object data blob: |
| 37 | + p.pack(msgpack.ExtType(2, msgpack.packb(obj_data, use_bin_type=True))), |
| 38 | + ] |
| 39 | + |
| 40 | + # build metadata — now include the original columns order |
| 41 | + meta = { |
| 42 | + "ts_unit": ts_unit, |
| 43 | + "num_descr": [list(x) for x in num_descr], |
| 44 | + "num_cols": num_cols, |
| 45 | + "obj_cols": obj_cols, |
| 46 | + "orig_cols": df.columns.tolist(), |
| 47 | + "index_name": df.index.name, |
| 48 | + } |
| 49 | + |
| 50 | + parts.append(p.pack(meta)) |
| 51 | + raw = b"".join(parts) |
| 52 | + return zstd.ZstdCompressor(level=zstd_level).compress(raw) |
| 53 | + |
| 54 | + |
| 55 | +def decode_payload(blob): |
| 56 | + # 1) decompress |
| 57 | + raw = zstd.ZstdDecompressor().decompress(blob) |
| 58 | + |
| 59 | + # 2) ext_hook to pull out our three ExtTypes |
| 60 | + def ext_hook(code, data): |
| 61 | + if code == 0: |
| 62 | + # timestamps |
| 63 | + return np.frombuffer(data, dtype="int64") |
| 64 | + if code == 1: |
| 65 | + # numeric blob |
| 66 | + return data |
| 67 | + if code == 2: |
| 68 | + # object blob |
| 69 | + return data |
| 70 | + return msgpack.ExtType(code, data) |
| 71 | + |
| 72 | + # 3) unpack in sequence |
| 73 | + unpacker = msgpack.Unpacker(ext_hook=ext_hook, raw=False) |
| 74 | + unpacker.feed(raw) |
| 75 | + ts_arr = next(unpacker) |
| 76 | + num_blob = next(unpacker) |
| 77 | + obj_blob = next(unpacker) |
| 78 | + meta = next(unpacker) |
| 79 | + |
| 80 | + # 4) rebuild timestamps |
| 81 | + factor = {"s": 1_000_000_000, "ms": 1_000_000, "us": 1_000, "ns": 1}[ |
| 82 | + meta["ts_unit"] |
| 83 | + ] |
| 84 | + idx = pd.to_datetime(ts_arr * factor) |
| 85 | + idx.name = meta["index_name"] |
| 86 | + |
| 87 | + # 5) rebuild fixed‐dtype DataFrame |
| 88 | + num_cols = meta["num_cols"] |
| 89 | + if num_cols: |
| 90 | + dtype_descr = [tuple(x) for x in meta["num_descr"]] |
| 91 | + rec = np.frombuffer(num_blob, dtype=np.dtype(dtype_descr)) |
| 92 | + df_num = pd.DataFrame(rec, columns=num_cols) |
| 93 | + else: |
| 94 | + df_num = pd.DataFrame(index=idx) |
| 95 | + |
| 96 | + # 6) rebuild object‐dtype DataFrame |
| 97 | + obj_cols = meta["obj_cols"] |
| 98 | + if obj_cols: |
| 99 | + obj_data = msgpack.unpackb(obj_blob, raw=False) |
| 100 | + df_obj = pd.DataFrame(obj_data) |
| 101 | + else: |
| 102 | + df_obj = pd.DataFrame() |
| 103 | + |
| 104 | + # 7) combine, restore index, and **reorder**: |
| 105 | + df = pd.concat([df_num, df_obj], axis=1) |
| 106 | + df.index = idx |
| 107 | + |
| 108 | + # ← HERE: reorder exactly as original |
| 109 | + df = df[meta["orig_cols"]] |
| 110 | + |
| 111 | + return df |
0 commit comments