-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
more flexible type of input arguments for px functions #1768
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
Changes from 1 commit
152f87f
fe4eda3
86937ea
1ad0f5c
7d0e985
915a5a1
5d5ab81
ea0fa6a
e5f6953
29d7e18
4a028a2
ab35b42
e4b8835
3b0d21a
2a6ff71
72b5d1c
19431dd
7297a7f
dadd645
2cb9dba
b09dc58
91e6cda
33db488
e58c82e
9d65a07
64010de
20bd812
a7241a5
dc1fc0a
61678c2
71756a3
40ba5b9
749db6c
cd8574a
299fdde
8753797
ed24d8b
2da2b21
102b89f
0a50f6a
58c9eba
0a92537
372bd8f
3d0aff0
5a329ad
e0e0dd5
6bced73
bcc41a2
382e768
82c6592
9caeee1
adc68d3
5dc0ab1
f5d056c
bedf74f
074342b
ead8430
db10967
ef29378
5e40653
78564cc
2534ca9
c88660f
b5adbcf
b375f91
c59a4d5
1774ade
16d95af
5c95786
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -795,7 +795,7 @@ def _initialize_argument_col_names(args, attrables, array_attrables): | |
return used_col_names | ||
|
||
|
||
def build_dataframe(args, attrables, array_attrables): | ||
def build_dataframe(input_args, attrables, array_attrables): | ||
""" | ||
Constructs a dataframe and modifies `args` in-place. | ||
|
||
|
@@ -813,6 +813,12 @@ def build_dataframe(args, attrables, array_attrables): | |
array_attrables : list | ||
argument names corresponding to iterables, such as `hover_data`, ... | ||
""" | ||
args = dict(input_args) | ||
for field in args: | ||
if field in array_attrables and isinstance( | ||
args[field], pd.core.indexes.base.Index | ||
): | ||
args[field] = list(args[field]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. following from comment above... if we just always do this, instead of in an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea! Then we don't have to do a deepcopy of the whole |
||
# Cast data_frame argument to DataFrame (it could be a numpy array, dict etc.) | ||
df_provided = args["data_frame"] is not None | ||
if df_provided and not isinstance(args["data_frame"], pd.DataFrame): | ||
|
@@ -864,8 +870,15 @@ def build_dataframe(args, attrables, array_attrables): | |
length = len(df) | ||
if argument is None: | ||
continue | ||
# Case of multiindex | ||
if isinstance(argument, pd.core.indexes.multi.MultiIndex): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
raise TypeError( | ||
"Argument '%s' is a pandas MultiIndex." | ||
"pandas MultiIndex is not supported by plotly express " | ||
"at the moment." % field | ||
) | ||
## ----------------- argument is a col name ---------------------- | ||
elif isinstance(argument, str) or isinstance( | ||
if isinstance(argument, str) or isinstance( | ||
argument, int | ||
): # just a column name given as str or int | ||
if not df_provided: | ||
|
@@ -902,19 +915,7 @@ def build_dataframe(args, attrables, array_attrables): | |
col_name = argument | ||
if isinstance(argument, int): | ||
col_name = _name_heuristic(argument, field, reserved_names) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. turns out we could write simpler code here. Thanks ! |
||
if field_name not in array_attrables: | ||
args[field_name] = col_name | ||
else: | ||
args[field_name][i] = col_name | ||
df[col_name] = args["data_frame"][argument] | ||
continue | ||
# Case of multiindex | ||
elif isinstance(argument, pd.core.indexes.multi.MultiIndex): | ||
raise TypeError( | ||
"Argument '%s' is a pandas MultiIndex." | ||
"pandas MultiIndex is not supported by plotly express " | ||
"at the moment." % field | ||
) | ||
# ----------------- argument is a column / array / list.... ------- | ||
else: | ||
is_index = isinstance(argument, pd.core.indexes.range.RangeIndex) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
@@ -980,7 +981,7 @@ def infer_config(args, constructor, trace_patch): | |
if group_attr in args: | ||
all_attrables += [group_attr] | ||
|
||
build_dataframe(args, all_attrables, array_attrables) | ||
args = build_dataframe(args, all_attrables, array_attrables) | ||
|
||
attrs = [k for k in attrables if k in args] | ||
grouped_attrs = [] | ||
|
@@ -1058,7 +1059,7 @@ def infer_config(args, constructor, trace_patch): | |
|
||
# Create trace specs | ||
trace_specs = make_trace_spec(args, constructor, attrs, trace_patch) | ||
return trace_specs, grouped_mappings, sizeref, show_colorbar | ||
return args, trace_specs, grouped_mappings, sizeref, show_colorbar | ||
|
||
|
||
def get_orderings(args, grouper, grouped): | ||
|
@@ -1096,7 +1097,7 @@ def get_orderings(args, grouper, grouped): | |
def make_figure(args, constructor, trace_patch={}, layout_patch={}): | ||
apply_default_cascade(args) | ||
|
||
trace_specs, grouped_mappings, sizeref, show_colorbar = infer_config( | ||
args, trace_specs, grouped_mappings, sizeref, show_colorbar = infer_config( | ||
args, constructor, trace_patch | ||
) | ||
grouper = [x.grouper or one_group for x in grouped_mappings] or [one_group] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this makes a deep copy...
But also think this is only an issue for
array_attrables
where we write intoargs[something][something]
so if we just copy those that's enough:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh no!
I knew it does a deep copy for a list so I had wrongly assumed I could transpose this pattern to adict
.