Compare commits

...

21 Commits

Author SHA1 Message Date
kennethreitz edbb16ec97 next version 2017-09-01 15:37:00 -04:00
kennethreitz dec5cea722 Merge pull request #307 from audiolion/make-pandas-optional
Make pandas optional
2017-09-01 13:49:44 -04:00
Ryan Castner 38183938dc Change how travis installs to get all test dependencies 2017-09-01 13:33:28 -04:00
Ryan Castner 7f1db4023f Raise NotImplementedError if pandas is not installed 2017-09-01 13:21:21 -04:00
Ryan Castner b09fface1b Make pandas an optional install 2017-09-01 13:20:54 -04:00
kennethreitz 69edb9def3 Update index.rst 2017-08-28 01:14:36 -04:00
kennethreitz ec54918f4a Update tutorial.rst 2017-08-28 01:06:43 -04:00
kennethreitz ab6633549f Update index.rst 2017-08-28 01:04:16 -04:00
kennethreitz 56005d8022 Update README.rst 2017-08-28 01:02:49 -04:00
kennethreitz 36fa7ef097 update docs
Signed-off-by: Kenneth Reitz <me@kennethreitz.org>
2017-08-27 03:56:14 -04:00
kennethreitz bb0abc863e bunk requirements file
Signed-off-by: Kenneth Reitz <me@kennethreitz.org>
2017-08-27 03:49:29 -04:00
kennethreitz 58f6eefe01 Merge branch 'master' of github.com:kennethreitz/tablib 2017-08-27 03:48:10 -04:00
kennethreitz e4726cb85c update docs
Signed-off-by: Kenneth Reitz <me@kennethreitz.org>
2017-08-27 03:48:01 -04:00
kennethreitz 412e690289 Update README.rst 2017-08-27 03:42:15 -04:00
kennethreitz 44e797d70e Update README.rst 2017-08-27 03:41:53 -04:00
kennethreitz 34c14aca18 Update README.rst 2017-08-27 03:41:26 -04:00
kennethreitz 7c318adde4 Update README.rst 2017-08-27 03:41:01 -04:00
kennethreitz 5dd74c0104 drop 2.6 2017-08-27 03:29:44 -04:00
kennethreitz a50ff92ff2 only require pandas if python isn't 2.6
Signed-off-by: Kenneth Reitz <me@kennethreitz.org>
2017-08-27 03:26:21 -04:00
kennethreitz 383d4b9c4e Merge pull request #301 from jasonamyers/feature/dataframes
Adding initial DataFrames Support
2017-08-27 03:22:42 -04:00
Jason Myers 00e2ffa2ef Adding initial DataFrames Support
Signed-off-by: Jason Myers <jason@jasonamyers.com>
2017-08-26 20:43:35 -05:00
11 changed files with 134 additions and 27 deletions
+1 -2
View File
@@ -1,11 +1,10 @@
language: python
python:
- 2.6
- 2.7
- 3.3
- 3.4
- 3.5
- 3.6
install:
- python setup.py install
- pip install -r requirements.txt
script: python test_tablib.py
+17 -7
View File
@@ -21,6 +21,7 @@ Output formats supported:
- Excel (Sets + Books)
- JSON (Sets + Books)
- YAML (Sets + Books)
- Pandas DataFrames (Sets)
- HTML (Sets)
- TSV (Sets)
- OSD (Sets)
@@ -64,13 +65,13 @@ Intelligently add new columns: ::
Slice rows: ::
>>> print data[:2]
>>> print(data[:2])
[('John', 'Adams', 90), ('George', 'Washington', 67)]
Slice columns by header: ::
>>> print data['first_name']
>>> print(data['first_name'])
['John', 'George', 'Henry']
Easily delete rows: ::
@@ -86,7 +87,7 @@ JSON!
+++++
::
>>> print data.json
>>> print(data.export('json'))
[
{
"last_name": "Adams",
@@ -105,7 +106,7 @@ YAML!
+++++
::
>>> print data.yaml
>>> print(data.export('yaml'))
- {age: 90, first_name: John, last_name: Adams}
- {age: 83, first_name: Henry, last_name: Ford}
@@ -113,7 +114,7 @@ CSV...
++++++
::
>>> print data.csv
>>> print(data.export('csv'))
first_name,last_name,age
John,Adams,90
Henry,Ford,83
@@ -123,14 +124,23 @@ EXCEL!
::
>>> with open('people.xls', 'wb') as f:
... f.write(data.xls)
... f.write(data.export('xls'))
DBF!
++++
::
>>> with open('people.dbf', 'wb') as f:
... f.write(data.dbf)
... f.write(data.export('dbf'))
Pandas DataFrame!
+++++++++++++++++
::
>>> print(data.export('df')):
first_name last_name age
0 John Adams 90
1 Henry Ford 83
It's that easy.
+9 -4
View File
@@ -29,18 +29,23 @@ Tablib is an :ref:`MIT Licensed <mit>` format-agnostic tabular dataset library,
>>> data = tablib.Dataset(headers=['First Name', 'Last Name', 'Age'])
>>> for i in [('Kenneth', 'Reitz', 22), ('Bessie', 'Monke', 21)]:
... data.append(i)
>>> print data.json
>>> print(data.export('json'))
[{"Last Name": "Reitz", "First Name": "Kenneth", "Age": 22}, {"Last Name": "Monke", "First Name": "Bessie", "Age": 21}]
>>> print data.yaml
>>> print(data.export('yaml'))
- {Age: 22, First Name: Kenneth, Last Name: Reitz}
- {Age: 21, First Name: Bessie, Last Name: Monke}
>>> data.xlsx
>>> data.export('xlsx')
<censored binary data>
>>> data.export('df')
First Name Last Name Age
0 Kenneth Reitz 22
1 Bessie Monke 21
Testimonials
------------
+1 -2
View File
@@ -49,7 +49,7 @@ Tablib is released under terms of `The MIT License`_.
Tablib License
--------------
Copyright 2016 Kenneth Reitz
Copyright 2017 Kenneth Reitz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -77,7 +77,6 @@ Pythons Supported
At this time, the following Python platforms are officially supported:
* cPython 2.6
* cPython 2.7
* cPython 3.3
* cPython 3.4
+15 -7
View File
@@ -115,30 +115,38 @@ Tablib's killer feature is the ability to export your :class:`Dataset` objects i
**Comma-Separated Values** ::
>>> data.csv
>>> data.export('csv')
Last Name,First Name,Age
Reitz,Kenneth,22
Monke,Bessie,20
**JavaScript Object Notation** ::
>>> data.json
>>> data.export('json')
[{"Last Name": "Reitz", "First Name": "Kenneth", "Age": 22}, {"Last Name": "Monke", "First Name": "Bessie", "Age": 20}]
**YAML Ain't Markup Language** ::
>>> data.yaml
>>> data.export('yaml')
- {Age: 22, First Name: Kenneth, Last Name: Reitz}
- {Age: 20, First Name: Bessie, Last Name: Monke}
**Microsoft Excel** ::
>>> data.xls
>>> data.export('xls')
<censored binary data>
**Pandas DataFrame** ::
>>> data.export('df')
First Name Last Name Age
0 Kenneth Reitz 22
1 Bessie Monke 21
------------------------
Selecting Rows & Columns
------------------------
@@ -216,7 +224,7 @@ Let's add a dynamic column to our :class:`Dataset` object. In this example, we h
Let's have a look at our data. ::
>>> data.yaml
>>> data.export('yaml')
- {Age: 22, First Name: Kenneth, Grade: 0.6, Last Name: Reitz}
- {Age: 20, First Name: Bessie, Grade: 0.75, Last Name: Monke}
@@ -246,7 +254,7 @@ For example, we can use the data available in the row to guess the gender of a s
Adding this function to our dataset as a dynamic column would result in: ::
>>> data.yaml
>>> data.export('yaml')
- {Age: 22, First Name: Kenneth, Gender: Male, Last Name: Reitz}
- {Age: 20, First Name: Bessie, Gender: Female, Last Name: Monke}
@@ -346,7 +354,7 @@ When, it's often useful to create a blank row containing information on the upco
# Write spreadsheet to disk
with open('grades.xls', 'wb') as f:
f.write(tests.xls)
f.write(tests.export('xls'))
The resulting **tests.xls** will have the following layout:
+21
View File
@@ -0,0 +1,21 @@
certifi==2017.7.27.1
chardet==3.0.4
et-xmlfile==1.0.1
idna==2.6
jdcal==1.3
numpy==1.13.1
odfpy==1.3.5
openpyxl==2.4.8
pandas==0.20.3
pkginfo==1.4.1
python-dateutil==2.6.1
pytz==2017.2
PyYAML==3.12
requests==2.18.4
requests-toolbelt==0.8.0
six==1.10.0
tqdm==4.15.0
unicodecsv==0.14.1
urllib3==1.22
xlrd==1.1.0
xlwt==1.3.0
+4 -1
View File
@@ -50,6 +50,7 @@ install = [
'pyyaml',
]
with open('tablib/core.py', 'r') as fd:
version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
fd.read(), re.MULTILINE).group(1)
@@ -71,7 +72,6 @@ setup(
'Natural Language :: English',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
@@ -80,4 +80,7 @@ setup(
],
tests_require=['pytest'],
install_requires=install,
extras_require={
'pandas': ['pandas'],
},
)
+14 -2
View File
@@ -18,8 +18,8 @@ from tablib.compat import OrderedDict, unicode
__title__ = 'tablib'
__version__ = '0.11.5'
__build__ = 0x001104
__version__ = '0.12.1'
__build__ = 0x001201
__author__ = 'Kenneth Reitz'
__license__ = 'MIT'
__copyright__ = 'Copyright 2017 Kenneth Reitz'
@@ -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)
+49
View File
@@ -0,0 +1,49 @@
""" Tablib - DataFrame Support.
"""
import sys
if sys.version_info[0] > 2:
from io import BytesIO
else:
from cStringIO import StringIO as BytesIO
try:
from pandas import DataFrame
except ImportError:
DataFrame = None
import tablib
from tablib.compat import unicode
title = 'df'
extensions = ('df', )
def detect(stream):
"""Returns True if given stream is a DataFrame."""
if DataFrame is None:
return False
try:
DataFrame(stream)
return True
except ValueError:
return False
def export_set(dset, index=None):
"""Returns DataFrame representation of DataBook."""
if DataFrame is None:
raise NotImplementedError(
'DataFrame Format requires `pandas` to be installed.'
' Try `pip install tablib[pandas]`.')
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 -1
View File
@@ -5,7 +5,6 @@
import json
import unittest
import sys
import os
import datetime
@@ -383,6 +382,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."""