Skip to content

Commit 4cb6a8d

Browse files
committed
added a test
1 parent 0588eed commit 4cb6a8d

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

plotly/grid_objs/grid_objs.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,16 @@ def __init__(self, columns_or_json, fid=None):
151151
"""
152152
# TODO: verify that columns are actually columns
153153
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)
154+
duplicate_name = utils.get_first_duplicate(columns_or_json.columns)
156155
if duplicate_name:
157156
err = exceptions.NON_UNIQUE_COLUMN_MESSAGE.format(duplicate_name)
158157
raise exceptions.InputError(err)
159158

160159
# create columns from dataframe
161160
all_columns = []
162-
for name in column_names:
161+
for name in columns_or_json.columns:
163162
all_columns.append(Column(columns_or_json[name].tolist(), name))
164-
self._columns = list(all_columns)
163+
self._columns = all_columns
165164
self.id = ''
166165

167166
elif isinstance(columns_or_json, dict):

plotly/tests/test_optional/test_grid/__init__.py

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
test_grid:
3+
==========
4+
5+
A module intended for use with Nose.
6+
7+
"""
8+
from __future__ import absolute_import
9+
10+
from unittest import TestCase
11+
12+
from plotly.exceptions import InputError
13+
from plotly.grid_objs import Grid
14+
15+
import pandas as pd
16+
17+
18+
class Test_Dataframe_to_Grid(TestCase):
19+
20+
# Test duplicate columns
21+
def test_duplicate_columns(self):
22+
df = pd.DataFrame([[1, 'a'],
23+
[2, 'b']], columns=['col_1', 'col_1'])
24+
25+
NON_UNIQUE_COLUMN_MESSAGE = (
26+
"Yikes, plotly grids currently "
27+
"can't have duplicate column names. Rename "
28+
"the column \"{}\" and try again.".format('col_1')
29+
)
30+
31+
with self.assertRaisesRegexp(InputError, NON_UNIQUE_COLUMN_MESSAGE):
32+
Grid(df)

0 commit comments

Comments
 (0)