Skip to content

Can't Dataset class create table directly from dict? #7202

Answered by keewis
forestbat asked this question in Q&A
Discussion options

You must be logged in to vote

Given the structure you posted in #7202 (reply in thread) (mapping of timestep to row), you can translate to something .from_dict accepts with:

In [8]: import xarray as xr
   ...: from datetime import datetime
   ...: 
   ...: original_data = {
   ...:     datetime(2022, 10, 24, 0, 0, 0): [0, 1, 2, 3, 4, 5],
   ...:     datetime(2022, 10, 24, 1, 0, 0): [1, 2, 3, 4, 5, 6],
   ...:     datetime(2022, 10, 24, 2, 0, 0): [2, 3, 4, 5, 6, 7],
   ...: }
   ...: data = {
   ...:     "time": {"dims": "time", "data": list(original_data.keys())},
   ...:     "data": {"dims": ["time", "columns"], "data": list(original_data.values())},
   ...: }
   ...: ds = xr.Dataset.from_dict(data)
   ...: ds
Out[8]: 

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@forestbat
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by forestbat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants