ENH: add to_xarray conversion method by jreback · Pull Request #11972 · pandas-dev/pandas (original) (raw)

In [1]:        df = DataFrame({'a': list('abc'),
   ...:                         'b': list(range(1, 4)),
   ...:                         'c': np.arange(3, 6).astype('u1'),
   ...:                         'd': np.arange(4.0, 7.0, dtype='float64'),
   ...:                         'e': [True, False, True],
   ...:                         'f': pd.Categorical(list('abc')),
   ...:                         'g': pd.date_range('20130101', periods=3),
   ...:                         'h': pd.date_range('20130101',
   ...:                                            periods=3,
   ...:                                            tz='US/Eastern')}
   ...:                        )

In [2]: df.to_xarray()
Out[2]: 
<xarray.Dataset>
Dimensions:  (index: 3)
Coordinates:
  * index    (index) int64 0 1 2
Data variables:
    a        (index) object 'a' 'b' 'c'
    b        (index) int64 1 2 3
    c        (index) uint8 3 4 5
    d        (index) float64 4.0 5.0 6.0
    e        (index) bool True False True
    f        (index) category 'a' 'b' 'c'
    g        (index) datetime64[ns] 2013-01-01 2013-01-02 2013-01-03
    h        (index) datetime64[ns] 2013-01-01T05:00:00 2013-01-02T05:00:00 ...

In [3]:        df = DataFrame({'a': list('abc'),
   ...:                         'b': list(range(1, 4)),
   ...:                         'c': np.arange(3, 6).astype('u1'),
   ...:                         'd': np.arange(4.0, 7.0, dtype='float64'),
   ...:                         'e': [True, False, True],
   ...:                         'f': pd.Categorical(list('abc')),
   ...:                         'g': pd.date_range('20130101', periods=3),
   ...:                         'h': pd.date_range('20130101',
   ...:                                            periods=3,
   ...:                                            tz='US/Eastern')}
   ...:                        )

In [4]:        df.index = pd.MultiIndex.from_product([['a'], range(3)],
   ...:                                               names=['one', 'two'])

In [5]: df
Out[5]: 
         a  b  c  d      e  f          g                         h
one two                                                           
a   0    a  1  3  4   True  a 2013-01-01 2013-01-01 00:00:00-05:00
    1    b  2  4  5  False  b 2013-01-02 2013-01-02 00:00:00-05:00
    2    c  3  5  6   True  c 2013-01-03 2013-01-03 00:00:00-05:00

In [6]: df.to_xarray()
ValueError: dimensions ('one', 'two') must have the same length as the number of data dimensions, ndim=1