Merge pull request #301 from jasonamyers/feature/dataframes

Adding initial DataFrames Support
This commit is contained in:
2017-08-27 03:22:42 -04:00
committed by GitHub
5 changed files with 56 additions and 1 deletions
+1
View File
@@ -48,6 +48,7 @@ install = [
'xlrd',
'xlwt',
'pyyaml',
'pandas',
]
with open('tablib/core.py', 'r') as fd:
+12
View File
@@ -570,6 +570,18 @@ class Dataset(object):
"""
pass
@property
def df():
"""A DataFrame representation of the :class:`Dataset` object.
A dataset object can also be imported by setting the :class:`Dataset.df` attribute: ::
data = tablib.Dataset()
data.df = DataFrame(np.random.randn(6,4))
Import assumes (for now) that headers exist.
"""
pass
@property
def json():
+2 -1
View File
@@ -13,5 +13,6 @@ from . import _xlsx as xlsx
from . import _ods as ods
from . import _dbf as dbf
from . import _latex as latex
from . import _df as df
available = (json, xls, yaml, csv, dbf, tsv, html, latex, xlsx, ods)
available = (json, xls, yaml, csv, dbf, tsv, html, latex, xlsx, ods, df)
+40
View File
@@ -0,0 +1,40 @@
""" Tablib - DataFrame Support.
"""
import sys
if sys.version_info[0] > 2:
from io import BytesIO
else:
from cStringIO import StringIO as BytesIO
from pandas import DataFrame
import tablib
from tablib.compat import unicode
title = 'df'
extensions = ('df', )
def detect(stream):
"""Returns True if given stream is a DataFrame."""
try:
DataFrame(stream)
return True
except ValueError:
return False
def export_set(dset, index=None):
"""Returns DataFrame representation of DataBook."""
dataframe = DataFrame(dset.dict, columns=dset.headers)
return dataframe
def import_set(dset, in_stream):
"""Returns dataset from DataFrame."""
dset.wipe()
dset.dict = in_stream.to_dict(orient='records')
+1
View File
@@ -383,6 +383,7 @@ class TablibTestCase(unittest.TestCase):
data.ods
data.html
data.latex
data.df
def test_datetime_append(self):
"""Passes in a single datetime and a single date and exports."""