Skip to content

Infer dims & coords from xarray variables passed to pm.Data #5796

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions pymc/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ def determine_coords(
dim_name = value.index.name
if dim_name is not None:
coords[dim_name] = value.index
if dims is None:
dims = dims_name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dims should be a list

In addition, it might be a good idea to account for the case of Multi-Indexed DataFrames. (raise?)


# If value is a df, we also interpret the columns as coords:
if hasattr(value, "columns"):
Expand All @@ -495,7 +497,31 @@ def determine_coords(
dim_name = value.columns.name
if dim_name is not None:
coords[dim_name] = value.columns

if dims is None:
dims = dims_name

if isinstance(value, xr.DataArray):
dim_name = None
if dims is not None:
dim_name = dims[0]
if dim_name is None and value.dims[0] is not None:
dim_name = value.dims[0]
if dim_name is not None:
coords[dim_name] = dff.indexes.get(str(dff.dims[0])).values
if dims is None:
dims = dims_name

if isinstance(value, xr.DataArray):
dim_name = None
if dims is not None:
dim_name = dims[1]
if dim_name is None and value.dims[1] is not None:
dim_name = value.dims[1]
if dim_name is not None:
coords[dim_name] = dff.indexes.get(str(dff.dims[1])).values
if dims is None:
dims = dims_name

if isinstance(value, np.ndarray) and dims is not None:
if len(dims) != value.ndim:
raise pm.exceptions.ShapeError(
Expand All @@ -506,11 +532,7 @@ def determine_coords(
for size, dim in zip(value.shape, dims):
coord = model.coords.get(dim, None)
if coord is None and dim is not None:
coords[dim] = range(size)

if dims is None:
# TODO: Also determine dim names from the index
dims = [None] * np.ndim(value)
coords[dim] = range(size)

return coords, dims

Expand Down