diff options
| author | Jason Myers <jason@jasonamyers.com> | 2017-08-26 20:43:35 -0500 |
|---|---|---|
| committer | Jason Myers <jason@jasonamyers.com> | 2017-08-26 20:43:35 -0500 |
| commit | 00e2ffa2efd1a7dd47d2a4a2a4303e1a8beb71e3 (patch) | |
| tree | 6b888ef7e4467a0c1e02d9d8db5b6d70cbb817b9 /tablib | |
| parent | a3cd2c9cffaa2940762453f5a3c4f2ae69065aa7 (diff) | |
| download | tablib-00e2ffa2efd1a7dd47d2a4a2a4303e1a8beb71e3.tar.gz | |
Adding initial DataFrames Support
Signed-off-by: Jason Myers <jason@jasonamyers.com>
Diffstat (limited to 'tablib')
| -rw-r--r-- | tablib/core.py | 12 | ||||
| -rw-r--r-- | tablib/formats/__init__.py | 3 | ||||
| -rw-r--r-- | tablib/formats/_df.py | 40 |
3 files changed, 54 insertions, 1 deletions
diff --git a/tablib/core.py b/tablib/core.py index be648c2..13a4514 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -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(): diff --git a/tablib/formats/__init__.py b/tablib/formats/__init__.py index 5cca19f..94b5bc9 100644 --- a/tablib/formats/__init__.py +++ b/tablib/formats/__init__.py @@ -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) diff --git a/tablib/formats/_df.py b/tablib/formats/_df.py new file mode 100644 index 0000000..5996ce9 --- /dev/null +++ b/tablib/formats/_df.py @@ -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') |
