Skip to content

Grid Accept Dataframe #675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 27, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions plotly/grid_objs/grid_objs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

from requests.compat import json as _json

from plotly import exceptions, utils
from plotly import exceptions, optional_imports, utils

pd = optional_imports.get_module('pandas')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌


__all__ = None

Expand Down Expand Up @@ -148,7 +150,21 @@ def __init__(self, columns_or_json, fid=None):
```
"""
# TODO: verify that columns are actually columns
if isinstance(columns_or_json, dict):
if pd and isinstance(columns_or_json, pd.DataFrame):
column_names = [name for name in columns_or_json.columns]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐄 if you need this to be a list, why not just list(columns_or_json.columns)? If you don't need this to be a list, why not just leave it as a pandas array? column_names = columns_or_json.columns?

duplicate_name = utils.get_first_duplicate(column_names)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, here we go, can you not just do utils.get_first_duplicate(columns_or_json.columns)?

if duplicate_name:
err = exceptions.NON_UNIQUE_COLUMN_MESSAGE.format(duplicate_name)
raise exceptions.InputError(err)

# create columns from dataframe
all_columns = []
for name in column_names:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, embrace the 🦆. for name in columns_or_json.columns:.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know why I'm afraid of Ducks...

all_columns.append(Column(columns_or_json[name].tolist(), name))
self._columns = list(all_columns)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why list this? You've already instantiated a new list above all_columns = []. I don't see why an additional shallow copy is necessary?

self.id = ''

elif isinstance(columns_or_json, dict):
# check that fid is entered
if fid is None:
raise exceptions.PlotlyError(
Expand Down