-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: GH17525 Function _get_standard_colors resets global random seed #17730
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 9 commits
3f0db16
6ec894b
f38bf55
831f70f
3bf3c0e
2f3da72
bf9678b
f711dd1
92dbac3
4041102
6e4e3e5
e4f1b0e
54b2138
092c2d5
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 |
---|---|---|
|
@@ -111,11 +111,13 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default', | |
if isinstance(colors, compat.string_types): | ||
colors = list(colors) | ||
elif color_type == 'random': | ||
import random | ||
from pandas.core.common import _random_state | ||
|
||
def random_color(column): | ||
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. I don't think
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. actually just use this
|
||
random.seed(column) | ||
return [random.random() for _ in range(3)] | ||
""" Returns a random color represented as a list of length 3""" | ||
# GH17525 use common._random_state to avoid resetting the seed | ||
rs = _random_state() | ||
return rs.rand(3) | ||
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.
|
||
|
||
colors = lmap(random_color, lrange(num_colors)) | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -284,3 +284,20 @@ def test_subplot_titles(self): | |
title=title[:-1]) | ||
title_list = [ax.get_title() for sublist in plot for ax in sublist] | ||
assert title_list == title[:3] + [''] | ||
|
||
def test_get_standard_colors_random_seed(self): | ||
""" For #17525 """ | ||
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. Generally, we just add a comment above, not a docstring. |
||
df = DataFrame(np.zeros((10, 10))) | ||
|
||
# Make sure that the random seed isn't reset by _get_standard_colors | ||
plotting.parallel_coordinates(df, 0) | ||
rand1 = random.random() | ||
plotting.parallel_coordinates(df, 0) | ||
rand2 = random.random() | ||
assert rand1 != rand2 | ||
|
||
# Make sure it produces the same colors every time it's called | ||
from pandas.plotting._style import _get_standard_colors | ||
color1 = _get_standard_colors(1, color_type='random') | ||
color2 = _get_standard_colors(1, color_type='random') | ||
assert color1 == color2 |
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.
you don't need the first part, a user won't know what you are talking about.