Skip to content
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

px.Constant and the "identity" problem #2119

Closed
nicolaskruchten opened this issue Jan 23, 2020 · 5 comments · Fixed by #2336
Closed

px.Constant and the "identity" problem #2119

nicolaskruchten opened this issue Jan 23, 2020 · 5 comments · Fixed by #2336
Milestone

Comments

@nicolaskruchten
Copy link
Contributor

nicolaskruchten commented Jan 23, 2020

px.Constant

In certain cases to get the kind of output one would want with PX we need to create a dummy column containing the same string or number in each row. I propose to create a special kind of object that we could pass into the px functions to do this like:

px.bar(df, y="the_y_value", x=px.Constant(1, label="the_optional_label"))

which would be a shortcut for:

px.bar(df, y="the_y_value", x=[1]*len(df), labels={"x": "the_optional_label"})

px.Identity + px.Direct (identity option 1)

Relatedly, but on a different note, one might have a column that directly encodes something like the color, and we might want a similar mechanism for doing something like

px.bar(x=[1,2,3], y=[1,2,3], color=px.Identity(["red", "green", "blue"]))
px.bar(x=[1,2,3], y=[1,2,3], color=px.Identity("column_with_colors"))

which would be a shortcut for:

px.bar(x=[1,2,3], y=[1,2,3], color=["red", "green", "blue"], 
color_discrete_map={"red":"red", "blue":"blue", "green": "green"})

Using the two together would allow you to force a single series to a single color:

px.bar(x=[1,2,3], y=[1,2,3], color=px.Identity(px.Constant("red")))

and this might warrant its own shortcut, maybe px.Direct?

px.bar(x=[1,2,3], y=[1,2,3], color=px.Direct("red"))

which would be a more-intuitive shortcut for the following equivalent calls, in terms of their output:

px.bar(x=[1,2,3], y=[1,2,3], color=px.Constant("red"),
    color_discrete_map={"red":"red"})
px.bar(x=[1,2,3], y=[1,2,3], color=["red"]*3, 
    color_discrete_map={"red":"red"})
px.bar(x=[1,2,3], y=[1,2,3], 
    color_discrete_sequence=["red"])

px.IDENTITY_MAP (identity option 2)

A slightly different way of solving the "identity" problem would be to be able to pass in a magic value to *_map (edit: which per @emmanuelle could be just the string "identity"!) like this:

px.bar(x=[1,2,3], y=[1,2,3], color=["red", "green", "blue"],  
color_discrete_map=px.IDENTITY_MAP)

which would also be the equivalent of

px.bar(x=[1,2,3], y=[1,2,3], color=["red", "green", "blue"], 
color_discrete_map={"red":"red", "blue":"blue", "green": "green"})

This would avoid the need for nesting px.Constant and px.Identity when you want to use constant and identity together, and would mean no need for px.Direct as shortcut for that:

px.bar(x=[1,2,3], y=[1,2,3], color=px.Constant("red"),  
color_discrete_map=px.IDENTITY_MAP)

(note that the foregoing could also apply to any groupable attrs beyond color like symbol or line_dash)

@nicolaskruchten
Copy link
Contributor Author

@emmanuelle thoughts?

@nicolaskruchten nicolaskruchten added this to the v4.6.0 milestone Jan 30, 2020
@nicolaskruchten nicolaskruchten changed the title px.constant px.constant, px.identity Mar 9, 2020
@emmanuelle
Copy link
Contributor

@nicolaskruchten px.constant would indeed be a useful addition. For the identity problem, I prefer the style of option 2, ie a constant object rather than a function (users might be confused about what the function returns for example, and the implementation in px code might also be more difficult). Since we will have to test for the value of px.IDENTITY_MAP inside the px code, I wonder why we could not pass just `color_discrete_map='identity'? Then we would not need a new object.

@nicolaskruchten
Copy link
Contributor Author

Ah yeah, a simple string would work too and we could build the identity map internally :)

I think also that since constant will be a class it should be capitalized also, right? px.Constant

@emmanuelle
Copy link
Contributor

Ah yes, it needs to be a class because of the label... so px.Constant indeed.

@nicolaskruchten nicolaskruchten changed the title px.constant, px.identity px.Constant and the "identity" problem Mar 9, 2020
@nicolaskruchten
Copy link
Contributor Author

Updated the description above to use a capital.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants