Skip to content

Support pandas objects directly in the Dataset constructor? #676

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
shoyer opened this issue Dec 10, 2015 · 2 comments
Closed

Support pandas objects directly in the Dataset constructor? #676

shoyer opened this issue Dec 10, 2015 · 2 comments

Comments

@shoyer
Copy link
Member

shoyer commented Dec 10, 2015

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
@max-sixty
Copy link
Collaborator

@shoyer I think this can be closed now?

@shoyer
Copy link
Member Author

shoyer commented Feb 2, 2016

Yes. Just added #740 so we don't forget about Series.

@shoyer shoyer closed this as completed Feb 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants