Skip to content

Support pandas objects directly in the Dataset constructor? #676

Closed
@shoyer

Description

@shoyer

Currently, this fails:

In [32]: xray.Dataset(df)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-32-358846bebb6d> in <module>()
----> 1 xray.Dataset(df)

/Users/shoyer/dev/xray/xray/core/dataset.pyc in __init__(self, data_vars, coords, attrs, compat, **kwargs)
    206         if coords is None:
    207             coords = set()
--> 208         if data_vars or coords:
    209             self._set_init_vars_and_dims(data_vars, coords, compat)
    210         if attrs is not None:

/Users/shoyer/miniconda/envs/chinook-py2/lib/python2.7/site-packages/pandas/core/generic.pyc in __nonzero__(self)
    712         raise ValueError("The truth value of a {0} is ambiguous. "
    713                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 714                          .format(self.__class__.__name__))
    715
    716     __bool__ = __nonzero__

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

But there's no reason why this shouldn't work. After #671, this is as simple as converting the DataFrame or Series to an OrderedDict first:

In [31]: xray.Dataset(OrderedDict(df))
Out[31]:
<xray.Dataset>
Dimensions:  (x: 3)
Coordinates:
  * x        (x) object 'a' 'b' 'c'
Data variables:
    y        (x) int64 0 1 2

In [34]: xray.Dataset(OrderedDict(df.y))
Out[34]:
<xray.Dataset>
Dimensions:  ()
Coordinates:
    *empty*
Data variables:
    a        int64 0
    b        int64 1
    c        int64 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions