mirror of
https://github.com/kennethreitz/tablib.git
synced 2026-06-05 06:56:13 +00:00
Adding initial DataFrames Support
Signed-off-by: Jason Myers <jason@jasonamyers.com>
This commit is contained in:
@@ -48,6 +48,7 @@ install = [
|
||||
'xlrd',
|
||||
'xlwt',
|
||||
'pyyaml',
|
||||
'pandas',
|
||||
]
|
||||
|
||||
with open('tablib/core.py', 'r') as fd:
|
||||
|
||||
@@ -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():
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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')
|
||||
@@ -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."""
|
||||
|
||||
Reference in New Issue
Block a user