From 20c5ef86c35f09c899440357263401c44efe4c65 Mon Sep 17 00:00:00 2001
From: Philipp Rudiger
Date: Fri, 1 Dec 2017 03:30:33 +0000
Subject: [PATCH] Handling pandas Series and single column DataFrames
---
holoviews/core/data/dictionary.py | 2 +-
holoviews/core/data/pandas.py | 6 +++++-
holoviews/core/util.py | 10 +++++++++-
tests/testdataset.py | 8 ++++++++
tests/teststatselements.py | 7 +++++++
5 files changed, 30 insertions(+), 3 deletions(-)
diff --git a/holoviews/core/data/dictionary.py b/holoviews/core/data/dictionary.py
index ea272a9591..e5f9b640dd 100644
--- a/holoviews/core/data/dictionary.py
+++ b/holoviews/core/data/dictionary.py
@@ -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:
diff --git a/holoviews/core/data/pandas.py b/holoviews/core/data/pandas.py
index b88073bef6..c90877a8c5 100644
--- a/holoviews/core/data/pandas.py
+++ b/holoviews/core/data/pandas.py
@@ -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:
@@ -108,7 +112,7 @@ def isscalar(cls, dataset, dim):
@classmethod
def validate(cls, dataset, vdims=True):
- dim_types = 'all' if vdims else 'key'
+ 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:
diff --git a/holoviews/core/util.py b/holoviews/core/util.py
index 361fe10286..4706610995 100644
--- a/holoviews/core/util.py
+++ b/holoviews/core/util.py
@@ -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)
diff --git a/tests/testdataset.py b/tests/testdataset.py
index 0f9028eca5..d9b9059f0d 100644
--- a/tests/testdataset.py
+++ b/tests/testdataset.py
@@ -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'])
diff --git a/tests/teststatselements.py b/tests/teststatselements.py
index 39a4acff81..ba60ebbfcd 100644
--- a/tests/teststatselements.py
+++ b/tests/teststatselements.py
@@ -24,6 +24,13 @@ def test_distribution_dframe_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_dict_constructor(self):
dist = Distribution({'Value': [0, 1, 2]})
self.assertEqual(dist.kdims, [Dimension('Value')])