Skip to content

DOC: Fix DataFrame.to_xarray doctests and allow the CI to run it. #22673

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

Merged
merged 5 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion ci/doctests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if [ "$DOCTEST" ]; then
fi

pytest --doctest-modules -v pandas/core/generic.py \
-k"-_set_axis_name -_xs -describe -droplevel -groupby -interpolate -pct_change -pipe -reindex -reindex_axis -resample -sample -to_json -to_xarray -transform -transpose -values -xs"
-k"-_set_axis_name -_xs -describe -droplevel -groupby -interpolate -pct_change -pipe -reindex -reindex_axis -resample -sample -to_json -transform -transpose -values -xs"

if [ $? -ne "0" ]; then
RET=1
Expand Down
68 changes: 35 additions & 33 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2498,11 +2498,15 @@ def to_xarray(self):
a Dataset for a DataFrame
a DataArray for higher dims
Copy link
Member

Choose a reason for hiding this comment

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

Do you mind replacing this to the standard format? Only the type in the first line, and a description in the next. For example:

xarray.DataArray or xarray.Dataset
    Data in the pandas structure converted to Dataset if the object is a DataFrame, or a DataArray if the object is a Series.


See also
--------
DataFrame.to_csv : Write out to a csv file.
Copy link
Member

Choose a reason for hiding this comment

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

See Also is with a capital A.

Personally I don't like "recommending" to_csv here, as besides not being a great format, it does not support multidimensional data. I think to_parquet and to_hdf seem more appropriate to me.


Examples
--------
>>> df = pd.DataFrame({'A' : [1, 1, 2],
'B' : ['foo', 'bar', 'foo'],
'C' : np.arange(4.,7)})
... 'B' : ['foo', 'bar', 'foo'],
... 'C' : np.arange(4.,7)})
Copy link
Member

Choose a reason for hiding this comment

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

I think using arange makes the code more complicated for no reason. Using [4., 5., 6.] is simpler and clearer I think.

Also, it'd be good to have a meaningful example. The code in the example is difficult to follow as there is no way to know that the object column is B, more than looking at the example. If we get something few samples with animals with name, num_legs, speed, it's obvious which is the float, which the int, and which the str/object.

>>> df
A B C
0 1 foo 4.0
Expand All @@ -2520,9 +2524,9 @@ def to_xarray(self):
C (index) float64 4.0 5.0 6.0

>>> df = pd.DataFrame({'A' : [1, 1, 2],
'B' : ['foo', 'bar', 'foo'],
'C' : np.arange(4.,7)}
).set_index(['B','A'])
... 'B' : ['foo', 'bar', 'foo'],
... 'C' : np.arange(4.,7)}
... ).set_index(['B','A'])
Copy link
Member

Choose a reason for hiding this comment

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

I don't see the need to repeat the previous DataFrame, we can just have df_multiindex = df.set_index(['B', 'A'])

>>> df
C
B A
Expand All @@ -2539,35 +2543,33 @@ def to_xarray(self):
Data variables:
C (B, A) float64 5.0 nan 4.0 6.0

>>> p = pd.Panel(np.arange(24).reshape(4,3,2),
items=list('ABCD'),
major_axis=pd.date_range('20130101', periods=3),
minor_axis=['first', 'second'])
>>> p
<class 'pandas.core.panel.Panel'>
Dimensions: 4 (items) x 3 (major_axis) x 2 (minor_axis)
Items axis: A to D
Major_axis axis: 2013-01-01 00:00:00 to 2013-01-03 00:00:00
Minor_axis axis: first to second

>>> p.to_xarray()
<xarray.DataArray (items: 4, major_axis: 3, minor_axis: 2)>
array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[ 8, 9],
[10, 11]],
[[12, 13],
[14, 15],
[16, 17]],
[[18, 19],
[20, 21],
[22, 23]]])
>>> index = pd.MultiIndex(levels=[['bar', 'baz', 'foo', 'qux'],
... ['one', 'two']],
... labels=[[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]],
... names=['first', 'second'])

>>> s = pd.Series(np.arange(8), index=index)
>>> s
Copy link
Member

Choose a reason for hiding this comment

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

I find this too complicated for what we need to show. To have a Series with a multiindex with a datetime level, we can have something like:

import pandas as pd

df = pd.DataFrame({'date': pd.to_datetime(['2018-01-01', '2018-01-01', '2018-01-02', '2018-01-02']),
                   'animal': ['falcon', 'parrot', 'falcon', 'parrot'],
                   'speed': [350, 18, 361, 15]}).set_index(['date', 'animal'])
df['speed']

I haven't used much xarray myself, and not sure what makes sense to show here. May be:

  • Series.to_xarray()
  • DataFrame.to_xarray()
  • DataFrame(with multiindex including datetime).to_xarray()

If that makes sense, I think with the first example, we can have df.to_xarray() and df['max_speed'].to_xarray(), and then a example like the one I wrote.

@jreback does this make sense?

Sorry for requesting the changes @Moisan, but my I find like the current version gives the idea that we're trying to show something more complex than what we are actually showing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem, I'm happy to make the examples more relevant :).

first second
bar one 0
two 1
baz one 2
two 3
foo one 4
two 5
qux one 6
two 7
dtype: int64

>>> s.to_xarray()
<xarray.DataArray (first: 4, second: 2)>
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
Coordinates:
* items (items) object 'A' 'B' 'C' 'D'
* major_axis (major_axis) datetime64[ns] 2013-01-01 2013-01-02 2013-01-03 # noqa
* minor_axis (minor_axis) object 'first' 'second'
* first (first) object 'bar' 'baz' 'foo' 'qux'
Copy link
Contributor

Choose a reason for hiding this comment

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

would be better to have a datetime index for 1 level

* second (second) object 'one' 'two'

Notes
-----
Expand Down