Skip to content

Commit

Permalink
Support additional Pandas formats if dependencies are satisfied - see a…
Browse files Browse the repository at this point in the history
  • Loading branch information
rhunwicks committed Oct 2, 2017
1 parent fbfd685 commit 38d6c03
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
37 changes: 26 additions & 11 deletions contrib/connectors/pandas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@
from superset.utils import QueryStatus


FORMATS = [
('csv', 'csv'),
('html', 'html'),
('json', 'json'),
('excel', 'Microsoft Excel'),
('stata', 'Stata'),
]

try:
import tables # NOQA
FORMATS.append(('hdf', 'HDF5'))
except ImportError:
pass

try:
import feather # NOQA
FORMATS.append(('feather', 'Feather'))
except ImportError:
pass


class PandasDatabase(object):
"""Non-ORM object for a Pandas Source"""

Expand Down Expand Up @@ -123,13 +144,6 @@ def perm(self):
class PandasDatasource(Model, BaseDatasource):
"""A datasource based on a Pandas DataFrame"""

FORMATS = [
('csv', 'csv'),
('html', 'html'),
('json', 'json'),
('excel', 'Microsoft Excel'),
]

# See http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases # NOQA
GRAINS = OrderedDict([
('5 seconds', '5S'),
Expand Down Expand Up @@ -577,7 +591,7 @@ def process_dataframe(
# If there is more than one DataFrame in the list then
# concatenate them along the index
if len(dfs) > 1:
df = pd.concat(dfs, axis=1)
df = pd.concat(dfs, axis=0)
query_str = 'pd.concat([{}])'.format(', '.join(query_strs))
else:
df = dfs[0]
Expand Down Expand Up @@ -627,9 +641,10 @@ def process_dataframe(
ascending=ascending)

# Remove metrics only added for post-aggregation filtering
df = df.drop(filtered_metrics, axis=1)
query_str += '.drop({filtered_metrics}, axis=1)'.format(
filtered_metrics=filtered_metrics)
if filtered_metrics:
df = df.drop(filtered_metrics, axis=1)
query_str += '.drop({filtered_metrics}, axis=1)'.format(
filtered_metrics=filtered_metrics)

elif groupby:
# Group by without any metrics is equivalent to SELECT DISTINCT,
Expand Down
8 changes: 3 additions & 5 deletions contrib/connectors/pandas/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
get_datasource_exist_error_mgs,
)

from .models import PandasDatasource, PandasColumn, PandasMetric
from .models import FORMATS, PandasDatasource, PandasColumn, PandasMetric


class ChoiceTypeSelectField(SelectField):
Expand Down Expand Up @@ -158,8 +158,7 @@ class PandasDatasourceModelView(DatasourceModelView, DeleteMixin): # noqa
'link', 'changed_on_']
add_columns = ['name', 'source_url', 'format']
add_form_extra_fields = {
'format': ChoiceTypeSelectField(_('Format'),
choices=PandasDatasource.FORMATS)
'format': ChoiceTypeSelectField(_('Format'), choices=FORMATS)
}
edit_columns = [
'name', 'source_url', 'format',
Expand All @@ -168,8 +167,7 @@ class PandasDatasourceModelView(DatasourceModelView, DeleteMixin): # noqa
'description', 'owner',
'main_dttm_col', 'default_endpoint', 'offset', 'cache_timeout']
edit_form_extra_fields = {
'format': ChoiceTypeSelectField(_('Format'),
choices=PandasDatasource.FORMATS)
'format': ChoiceTypeSelectField(_('Format'), choices=FORMATS)
}
show_columns = edit_columns + ['perm']
related_views = [PandasColumnInlineView, PandasMetricInlineView]
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def get_git_sha():
install_requires=[
'beautifulsoup4==4.6.0',
'boto3==1.4.4',
'bottleneck==1.2.1',
'celery==3.1.25',
'colorama==0.3.9',
'cryptography==1.9',
Expand All @@ -63,6 +64,7 @@ def get_git_sha():
'idna==2.5',
'lxml==3.8.0',
'markdown==2.6.8',
'numexpr==2.6.4',
'pandas==0.20.3',
'parsedatetime==2.0.0',
'pydruid==0.3.1',
Expand Down

0 comments on commit 38d6c03

Please sign in to comment.