|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + text_representation: |
| 5 | + extension: .Rmd |
| 6 | + format_name: rmarkdown |
| 7 | + format_version: '1.2' |
| 8 | + jupytext_version: 1.13.6 |
| 9 | + kernelspec: |
| 10 | + display_name: venv-pins-python |
| 11 | + language: python |
| 12 | + name: venv-pins-python |
| 13 | +--- |
| 14 | + |
| 15 | +# Using custom metadata |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +The `metadata` argument in pins is flexible and can hold any kind of metadata that you can formulate as a `dict` (convertable to JSON). |
| 20 | +In some situations, you may want to read and write with _consistent_ customized metadata; |
| 21 | +you can create functions to wrap `pin_write()` and `pin_read()` for your particular use case. |
| 22 | + |
| 23 | +We'll begin by creating a temporary board for demonstration: |
| 24 | + |
| 25 | +```{python setup} |
| 26 | +import pins |
| 27 | +import pandas as pd |
| 28 | +
|
| 29 | +board = pins.board_temp() |
| 30 | +``` |
| 31 | + |
| 32 | + |
| 33 | +# A function to store pandas Categoricals |
| 34 | + |
| 35 | +Say you want to store a pandas Categorical object as JSON together with the _categories_ of the categorical in the metadata. |
| 36 | + |
| 37 | +For example, here is a simple categorical and its categories: |
| 38 | + |
| 39 | +```{python} |
| 40 | +some_cat = pd.Categorical(["a", "a", "b"]) |
| 41 | +
|
| 42 | +some_cat.categories |
| 43 | +``` |
| 44 | + |
| 45 | +Notice that categories is just the unique values in the categorical. |
| 46 | + |
| 47 | +We can write a function wrapping `pin_write()` that holds the categories in metadata, so we can easily re-create the categorical with them. |
| 48 | + |
| 49 | +```{python} |
| 50 | +def pin_write_cat_json( |
| 51 | + board, |
| 52 | + x: pd.Categorical, |
| 53 | + name, |
| 54 | + **kwargs |
| 55 | +): |
| 56 | + metadata = {"categories": x.categories.to_list()} |
| 57 | + json_data = x.to_list() |
| 58 | + board.pin_write(json_data, name = name, type = "json", metadata = metadata, **kwargs) |
| 59 | +``` |
| 60 | + |
| 61 | +We can use this new function to write a pin as JSON with our specific metadata: |
| 62 | + |
| 63 | +```{python} |
| 64 | +some_cat = pd.Categorical(["a", "a", "b", "c"]) |
| 65 | +pin_write_cat_json(board, some_cat, name = "some-cat") |
| 66 | +``` |
| 67 | + |
| 68 | +## A function to read factors |
| 69 | + |
| 70 | +It's possible to read this pin using the regular `pin_read()` function, but the object we get is no longer a factor! |
| 71 | + |
| 72 | +```{python} |
| 73 | +board.pin_read("some-cat") |
| 74 | +``` |
| 75 | + |
| 76 | +However, notice that if we use `board.pin_meta()`, the information we stored on categories is in the `.user` field. |
| 77 | + |
| 78 | +```{python} |
| 79 | +board.pin_meta("some-cat") |
| 80 | +``` |
| 81 | + |
| 82 | +This enables us to write a special function for reading, to reconstruct the categorical, using the categories stashed in metadata: |
| 83 | + |
| 84 | +```{python} |
| 85 | +def pin_read_factor_json(board, name, version=None, hash=None, **kwargs): |
| 86 | + data = board.pin_read(name = name, version = version, hash = hash, **kwargs) |
| 87 | + meta = board.pin_meta(name = name, version = version, **kwargs) |
| 88 | + return pd.Categorical(data, categories=meta.user["categories"]) |
| 89 | +
|
| 90 | +pin_read_factor_json(board, "some-cat") |
| 91 | +``` |
| 92 | + |
| 93 | +For an example of how this approach is used in a real project, look at how |
0 commit comments