Skip to content

Commit 0588eed

Browse files
committed
first pass at accepting dataframes for a Grid
1 parent 871114d commit 0588eed

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

plotly/grid_objs/grid_objs.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
from requests.compat import json as _json
1111

12-
from plotly import exceptions, utils
12+
from plotly import exceptions, optional_imports, utils
13+
14+
pd = optional_imports.get_module('pandas')
1315

1416
__all__ = None
1517

@@ -148,7 +150,21 @@ def __init__(self, columns_or_json, fid=None):
148150
```
149151
"""
150152
# TODO: verify that columns are actually columns
151-
if isinstance(columns_or_json, dict):
153+
if pd and isinstance(columns_or_json, pd.DataFrame):
154+
column_names = [name for name in columns_or_json.columns]
155+
duplicate_name = utils.get_first_duplicate(column_names)
156+
if duplicate_name:
157+
err = exceptions.NON_UNIQUE_COLUMN_MESSAGE.format(duplicate_name)
158+
raise exceptions.InputError(err)
159+
160+
# create columns from dataframe
161+
all_columns = []
162+
for name in column_names:
163+
all_columns.append(Column(columns_or_json[name].tolist(), name))
164+
self._columns = list(all_columns)
165+
self.id = ''
166+
167+
elif isinstance(columns_or_json, dict):
152168
# check that fid is entered
153169
if fid is None:
154170
raise exceptions.PlotlyError(

0 commit comments

Comments
 (0)