Skip to content

Commit

Permalink
Handling pandas Series and single column DataFrames
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Dec 1, 2017
1 parent 5e928be commit fe57f1a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion holoviews/core/data/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def init(cls, eltype, data, kdims, vdims):

@classmethod
def validate(cls, dataset, vdims=True):
dim_types = 'key' if vdims else 'all'
dim_types = 'all' if vdims else 'key'
dimensions = dataset.dimensions(dim_types, label='name')
not_found = [d for d in dimensions if d not in dataset.data]
if not_found:
Expand Down
6 changes: 5 additions & 1 deletion holoviews/core/data/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ def init(cls, eltype, data, kdims, vdims):
element_params = eltype.params()
kdim_param = element_params['kdims']
vdim_param = element_params['vdims']
if util.is_series(data):
data = data.to_frame()
if util.is_dataframe(data):
if eltype._auto_indexable_1d and len(data.columns) == 1:
data = data.reset_index()
if isinstance(kdim_param.bounds[1], int):
ndim = min([kdim_param.bounds[1], len(kdim_param.default)])
else:
Expand Down Expand Up @@ -108,7 +112,7 @@ def isscalar(cls, dataset, dim):

@classmethod
def validate(cls, dataset, vdims=True):
dim_types = 'key' if vdims else 'all'
dim_types = 'all' if vdims else 'key'
dimensions = dataset.dimensions(dim_types, label='name')
not_found = [d for d in dimensions if d not in dataset.data.columns]
if not_found:
Expand Down
10 changes: 9 additions & 1 deletion holoviews/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,12 +1134,20 @@ def find_file(folder, filename):

def is_dataframe(data):
"""
Checks whether the supplied data is DataFrame type.
Checks whether the supplied data is of DataFrame type.
"""
return((pd is not None and isinstance(data, pd.DataFrame)) or
(dd is not None and isinstance(data, dd.DataFrame)))


def is_series(data):
"""
Checks whether the supplied data is of Series type.
"""
return((pd is not None and isinstance(data, pd.Series)) or
(dd is not None and isinstance(data, dd.Series)))


def get_param_values(data):
params = dict(kdims=data.kdims, vdims=data.vdims,
label=data.label)
Expand Down
8 changes: 8 additions & 0 deletions tests/testdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,14 @@ def setUp(self):
self.data_instance_type = pd.DataFrame
self.init_column_data()

def test_dataset_series_construct(self):
ds = Dataset(pd.Series([1, 2, 3], name='A'))
self.assertEqual(ds, Dataset(([0, 1, 2], [1, 2, 3]), ['index', 'A']))

def test_dataset_single_column_construct(self):
ds = Dataset(pd.DataFrame([1, 2, 3], columns=['A']))
self.assertEqual(ds, Dataset(([0, 1, 2], [1, 2, 3]), ['index', 'A']))

def test_dataset_extract_vdims(self):
df = pd.DataFrame({'x': [1, 2, 3], 'y': [1, 2, 3], 'z': [1, 2, 3]},
columns=['x', 'y', 'z'])
Expand Down
18 changes: 17 additions & 1 deletion tests/teststatselements.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import numpy as np
from unittest import SkipTest

import numpy as np
import holoviews
from holoviews.core.dimension import Dimension
from holoviews.core.util import pd
from holoviews.core.options import Compositor, Store
from holoviews.element import (Distribution, Bivariate, Points, Image,
Curve, Area, Contours, Polygons)
Expand All @@ -15,6 +17,20 @@ def test_distribution_array_constructor(self):
self.assertEqual(dist.kdims, [Dimension('Value')])
self.assertEqual(dist.vdims, [Dimension('Density')])

def test_distribution_series_constructor(self):
if pd is None:
raise SkipTest("Test requires pandas")
dist = Distribution(pd.Series([0, 1, 2], name='Value'))
self.assertEqual(dist.kdims, [Dimension('Value')])
self.assertEqual(dist.vdims, [Dimension('Density')])

def test_distribution_dframe_constructor(self):
if pd is None:
raise SkipTest("Test requires pandas")
dist = Distribution(pd.DataFrame({'Value': [0, 1, 2]}))
self.assertEqual(dist.kdims, [Dimension('Value')])
self.assertEqual(dist.vdims, [Dimension('Density')])

def test_distribution_array_constructor_custom_vdim(self):
dist = Distribution(np.array([0, 1, 2]), vdims=['Test'])
self.assertEqual(dist.kdims, [Dimension('Value')])
Expand Down

0 comments on commit fe57f1a

Please sign in to comment.