-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
pandas casting issues #1734
pandas casting issues #1734
Conversation
Thanks for the PR! Doc improvements are always appreciated. Maybe this would be better placed in the computations section? (http://xarray.pydata.org/en/latest/computation.html) Also, I don't understand what you mean with |
In a nutshell, x = xr.DataArray([1,2,3])
pd.Series({'mean': x.mean()}) does not work as expected. You'll want one of pd.Series({'mean': float(x.mean())})
pd.Series({'mean': x.mean()}, dtype=float)
pd.Series({'mean': x.mean()}).astype(float) |
Can you merge master? #1747 should fix the build failure |
@shoyer all good |
doc/faq.rst
Outdated
@@ -58,6 +58,15 @@ fundamentally multi-dimensional. If your data is unstructured or | |||
one-dimensional, stick with pandas. | |||
|
|||
|
|||
Gotchas |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you frame this as a question, so it looks more similar to the other headings?
e.g., "Why don't aggregations return Python scalars?"
doc/faq.rst
Outdated
------- | ||
|
||
xarray tries hard to be self-consistent. Operations on xarray objects return | ||
other xarray objects. In particular, operations like `mean` or `sum` - even |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also mention indexing here?
doc/faq.rst
Outdated
xarray tries hard to be self-consistent. Operations on xarray objects return | ||
other xarray objects. In particular, operations like `mean` or `sum` - even | ||
when applied to all axes - will return xarray objects. Constructing `pandas` | ||
objects sometimes require explicit casting. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite understand what this last comment means, maybe an example would help?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add something similar to my reply to @fmaussion above
doc/faq.rst
Outdated
Gotchas | ||
------- | ||
|
||
xarray tries hard to be self-consistent. Operations on xarray objects return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would note that the self-consistency is that the DataArray methods already return DataArrays (and the same for Dataset), i.e., it preserves the type. This isn't true for NumPy or pandas, which return scalars.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed but numpy aggregation funcs return boxed objects that look like an ndarray
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, NumPy and pandas return boxed scalar types (subclasses of np.generic
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, I have one more suggestion to add to this entry.
doc/faq.rst
Outdated
other xarray objects. In particular, operations like `mean` or `sum` - even | ||
when applied to all axes - will return xarray objects. Constructing `pandas` | ||
objects sometimes require explicit casting. | ||
pd.Series({'x': arr[0], 'mean': arr.mean(), 'std': arr.std()}).astype(float) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also note float(arr[0])
and arr[0].item()
as two others ways to convert scalars to builtin types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. I'll leave this open for a little while before merging in case anyone else has suggestions.
Actually, one thing that could be nice is to call out a brief note in "what's new" pointing users to this new FAQ page entry.
@shoyer Thanks for the help for the writing, most of these are your own words ^^ |
doc/whats-new.rst
Outdated
Documentation | ||
~~~~~~~~~~~~~ | ||
|
||
- New entry `Why don’t aggregations return Python scalars?` in the :doc:`faq`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider giving yourself credit here :)
@0x0L - I gave you credit on the whats new page and synced with master. I'll merge once the tests pass here again. |
@jhamman Thank you |
Added a comment about constructing
pandas
objects fromxarray
objects