Skip to content

Commit

Permalink
Added Table.to_pandas and Table.from_pandas
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog committed Feb 14, 2015
1 parent ff68e77 commit 51f5264
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
7 changes: 6 additions & 1 deletion CHANGES.rst
@@ -1,8 +1,13 @@
1.1 (unreleased)
----------------

- Nothing changed yet.
New Features
^^^^^^^^^^^^

- ``astropy.table``

- Added ``Table.to_pandas`` and ``Table.from_pandas`` for converting to/from
pandas dataframes.

1.0rc1 (2015-01-27)
-------------------
Expand Down
36 changes: 36 additions & 0 deletions astropy/table/table.py
Expand Up @@ -2021,6 +2021,42 @@ def group_by(self, keys):

return groups.table_group_by(self, keys)

def to_pandas(self):
"""
Return a :class:`pandas.DataFrame` instance
Returns
-------
dataframe : :class:`pandas.DataFrame`
A pandas :class:`pandas.DataFrame` instance
"""
from pandas import DataFrame
print(self.has_mixin_columns)
if self.has_mixin_columns:
raise ValueError("Cannot convert a table with mixin columns to a pandas DataFrame")
for column in self.columns:
print(self.dtype[column].shape)
if len(self.dtype[column].shape) > 0:
raise ValueError("Cannot convert a table with multi-dimensional columns to a pandas DataFrame")
return DataFrame(self.as_array())

@classmethod
def from_pandas(cls, dataframe):
"""
Create a `Table` from a :class:`pandas.DataFrame` instance
Parameters
----------
dataframe : :class:`pandas.DataFrame`
The pandas :class:`pandas.DataFrame` instance
Returns
-------
table : `Table`
A `Table` instance
"""
return cls(dataframe.to_records())


class QTable(Table):
"""A class to represent tables of heterogeneous data.
Expand Down
62 changes: 62 additions & 0 deletions astropy/table/tests/test_table.py
Expand Up @@ -7,6 +7,7 @@
import gc

import numpy as np
from numpy.testing import assert_allclose

from ...extern import six
from ...tests.helper import pytest, assert_follows_unicode_guidelines
Expand Down Expand Up @@ -1364,3 +1365,64 @@ def test_nested_iteration():
for r2 in t:
out.append((r1['a'], r2['a']))
assert out == [(0, 0), (0, 1), (1, 0), (1, 1)]


class TestPandas(object):

def test_simple(self):

from pandas import DataFrame

t = table.Table()

for endian in ['<', '>']:
for kind in ['f', 'i']:
for byte in ['2','4','8']:
dtype = np.dtype(endian + kind + byte)
x = np.array([1,2,3], dtype=dtype)
t[endian + kind + byte] = x

t['u'] = ['a','b','c']
t['s'] = [b'a', b'b', b'c']

d = t.to_pandas()

for column in t.columns:
if column == 'u':
assert np.all(t['u'] == np.array(['a','b','c']))
elif column == 's':
assert np.all(t['s'] == np.array([b'a',b'b',b'c']))
else:
assert_allclose(t[column], d[column])

t2 = table.Table.from_pandas(d)

for column in t.columns:
if column in ('u', 's'):
assert np.all(t[column] == t2[column])
else:
assert_allclose(t[column], t2[column])

def test_2d(self):

from pandas import DataFrame

t = table.Table()
t['a'] = [1,2,3]
t['b'] = np.ones((3,2))

with pytest.raises(ValueError) as exc:
d = t.to_pandas()
assert exc.value.args[0] == "Cannot convert a table with multi-dimensional columns to a pandas DataFrame"

def test_mixin(self):

from pandas import DataFrame
from ...coordinates import SkyCoord

t = table.Table()
t['c'] = SkyCoord([1,2,3], [4,5,6], unit='deg')

with pytest.raises(ValueError) as exc:
d = t.to_pandas()
assert exc.value.args[0] == "Cannot convert a table with mixin columns to a pandas DataFrame"

0 comments on commit 51f5264

Please sign in to comment.