Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let Container.write_dataframe accept a dict #733

Open
danielballan opened this issue May 1, 2024 · 1 comment
Open

Let Container.write_dataframe accept a dict #733

danielballan opened this issue May 1, 2024 · 1 comment
Labels
good first issue Good for newcomers

Comments

@danielballan
Copy link
Member

It is convenient that Container.write_array accepts a list, like:

$ tiled serve catalog --temp --api-key=secret
In [1]: from tiled.client import from_uri

In [2]: c = from_uri('http://localhost:8000', api_key='secret')

In [3]: c
Out[3]: <Container {}>

In [4]: c.write_array([1,2,3], key='x')
Out[4]: <ArrayClient shape=(3,) chunks=((3,),) dtype=int64>

It would be convenient if

c.write_dataframe({'a': [1,2,3], 'b':, [4,5,6]}, key='y')

were likewise acceptable. Currently, a pandas.DataFrame or dask.dataframe object must be passed.

@danielballan danielballan added the good first issue Good for newcomers label May 7, 2024
@danielballan
Copy link
Member Author

This needs two additions:

  1. Add a branch to this conditional that checks for isinstance(dataframe, dict) and, if so, attempts dataframe = pandas.DataFrame(dataframe).

if isinstance(dataframe, dask.dataframe.DataFrame):
structure = TableStructure.from_dask_dataframe(dataframe)
else:
structure = TableStructure.from_pandas(dataframe)

  1. Add a variation on this test that passes the dict data to write_dataframe instead of the pandas.DataFrame df.

def test_write_dataframe_full(tree):
with Context.from_app(
build_app(tree, validation_registry=validation_registry)
) as context:
client = from_context(context)
data = {f"Column{i}": (1 + i) * numpy.ones(5) for i in range(5)}
df = pandas.DataFrame(data)
metadata = {"scan_id": 1, "method": "A"}
specs = [Spec("SomeSpec")]
with record_history() as history:
client.write_dataframe(df, metadata=metadata, specs=specs)
# one request for metadata, one for data
assert len(history.requests) == 1 + 1
results = client.search(Key("scan_id") == 1)
result = results.values().first()
result_dataframe = result.read()
pandas.testing.assert_frame_equal(result_dataframe, df)
assert result.metadata == metadata
assert result.specs == specs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant