Skip to content

Commit 6b28eda

Browse files
fix Pandas warning, use parameterized tests
1 parent e5b9679 commit 6b28eda

File tree

1 file changed

+69
-74
lines changed

1 file changed

+69
-74
lines changed

packages/python/plotly/plotly/tests/test_core/test_px/test_px_input.py

Lines changed: 69 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pandas as pd
55
import pytest
66
from plotly.express._core import build_dataframe
7-
from pandas.util.testing import assert_frame_equal
7+
from pandas.testing import assert_frame_equal
88

99

1010
def test_numpy():
@@ -389,37 +389,38 @@ def test_ranges():
389389
assert "time=" in fig.data[0].hovertemplate
390390

391391

392-
def test_auto_orient():
393-
categorical = ["a", "a", "b", "b"]
392+
@pytest.mark.parametrize(
393+
"fn",
394+
[px.scatter, px.line, px.area, px.violin, px.box, px.strip]
395+
+ [px.bar, px.funnel, px.histogram],
396+
)
397+
@pytest.mark.parametrize(
398+
"x,y,result",
399+
[
400+
("numerical", "categorical", "h"),
401+
("categorical", "numerical", "v"),
402+
("categorical", "categorical", "v"),
403+
("numerical", "numerical", "v"),
404+
("numerical", "none", "h"),
405+
("categorical", "none", "h"),
406+
("none", "categorical", "v"),
407+
("none", "numerical", "v"),
408+
],
409+
)
410+
def test_auto_orient_x_and_y(fn, x, y, result):
411+
series = dict(categorical=["a", "a", "b", "b"], numerical=[1, 2, 3, 4], none=None)
412+
413+
if "none" not in [x, y]:
414+
assert fn(x=series[x], y=series[y]).data[0].orientation == result
415+
else:
416+
if fn == px.histogram or (fn == px.bar and "categorical" in [x, y]):
417+
assert fn(x=series[x], y=series[y]).data[0].orientation != result
418+
else:
419+
assert fn(x=series[x], y=series[y]).data[0].orientation == result
420+
421+
422+
def test_histogram_auto_orient():
394423
numerical = [1, 2, 3, 4]
395-
396-
auto_orientable = [px.scatter, px.line, px.area, px.violin, px.box, px.strip]
397-
auto_orientable += [px.bar, px.funnel, px.histogram]
398-
399-
pattern_x_and_y = [
400-
(numerical, categorical, "h"), # auto
401-
(categorical, numerical, "v"), # auto/default
402-
(categorical, categorical, "v"), # default
403-
(numerical, numerical, "v"), # default
404-
]
405-
for fn in auto_orientable:
406-
for x, y, result in pattern_x_and_y:
407-
assert fn(x=x, y=y).data[0].orientation == result
408-
409-
pattern_x_or_y = [
410-
(numerical, None, "h"), # auto
411-
(categorical, None, "h"), # auto
412-
(None, categorical, "v"), # auto/default
413-
(None, numerical, "v"), # auto/default
414-
]
415-
416-
for fn in auto_orientable:
417-
for x, y, result in pattern_x_or_y:
418-
if fn == px.histogram or (fn == px.bar and categorical in [x, y]):
419-
assert fn(x=x, y=y).data[0].orientation != result
420-
else:
421-
assert fn(x=x, y=y).data[0].orientation == result
422-
423424
assert px.histogram(x=numerical, nbins=5).data[0].nbinsx == 5
424425
assert px.histogram(y=numerical, nbins=5).data[0].nbinsy == 5
425426
assert px.histogram(x=numerical, y=numerical, nbins=5).data[0].nbinsx == 5
@@ -437,65 +438,59 @@ def test_auto_histfunc():
437438
assert px.density_heatmap(x=a, y=a, z=a, histfunc="avg").data[0].histfunc == "avg"
438439

439440

440-
def test_auto_boxlike_overlay():
441+
@pytest.mark.parametrize(
442+
"fn,mode", [(px.violin, "violinmode"), (px.box, "boxmode"), (px.strip, "boxmode")]
443+
)
444+
@pytest.mark.parametrize(
445+
"x,y,color,result",
446+
[
447+
("categorical1", "numerical", None, "group"),
448+
("categorical1", "numerical", "categorical2", "group"),
449+
("categorical1", "numerical", "categorical1", "overlay"),
450+
("numerical", "categorical1", None, "group"),
451+
("numerical", "categorical1", "categorical2", "group"),
452+
("numerical", "categorical1", "categorical1", "overlay"),
453+
],
454+
)
455+
def test_auto_boxlike_overlay(fn, mode, x, y, color, result):
441456
df = pd.DataFrame(
442457
dict(
443458
categorical1=["a", "a", "b", "b"],
444459
categorical2=["a", "a", "b", "b"],
445460
numerical=[1, 2, 3, 4],
446461
)
447462
)
448-
449-
pattern = [
450-
("categorical1", "numerical", None, "group"),
451-
("categorical1", "numerical", "categorical2", "group"),
452-
("categorical1", "numerical", "categorical1", "overlay"),
453-
("numerical", "categorical1", None, "group"),
454-
("numerical", "categorical1", "categorical2", "group"),
455-
("numerical", "categorical1", "categorical1", "overlay"),
456-
]
457-
458-
fn_and_mode = [
459-
(px.violin, "violinmode"),
460-
(px.box, "boxmode"),
461-
(px.strip, "boxmode"),
462-
]
463-
464-
for fn, mode in fn_and_mode:
465-
for x, y, color, result in pattern:
466-
assert fn(df, x=x, y=y, color=color).layout[mode] == result
463+
assert fn(df, x=x, y=y, color=color).layout[mode] == result
467464

468465

469-
def test_x_or_y():
466+
@pytest.mark.parametrize("fn", [px.scatter, px.line, px.area, px.bar])
467+
def test_x_or_y(fn):
470468
categorical = ["a", "a", "b", "b"]
471469
numerical = [1, 2, 3, 4]
472470
constant = [1, 1, 1, 1]
473471
range_4 = [0, 1, 2, 3]
474472
index = [11, 12, 13, 14]
475473
numerical_df = pd.DataFrame(dict(col=numerical), index=index)
476474
categorical_df = pd.DataFrame(dict(col=categorical), index=index)
477-
scatter_like = [px.scatter, px.line, px.area]
478-
bar_like = [px.bar]
479-
480-
for fn in scatter_like + bar_like:
481-
fig = fn(x=numerical)
482-
assert list(fig.data[0].x) == numerical
483-
assert list(fig.data[0].y) == range_4
484-
assert fig.data[0].orientation == "h"
485-
fig = fn(y=numerical)
486-
assert list(fig.data[0].x) == range_4
487-
assert list(fig.data[0].y) == numerical
488-
assert fig.data[0].orientation == "v"
489-
fig = fn(numerical_df, x="col")
490-
assert list(fig.data[0].x) == numerical
491-
assert list(fig.data[0].y) == index
492-
assert fig.data[0].orientation == "h"
493-
fig = fn(numerical_df, y="col")
494-
assert list(fig.data[0].x) == index
495-
assert list(fig.data[0].y) == numerical
496-
assert fig.data[0].orientation == "v"
497475

498-
for fn in scatter_like:
476+
fig = fn(x=numerical)
477+
assert list(fig.data[0].x) == numerical
478+
assert list(fig.data[0].y) == range_4
479+
assert fig.data[0].orientation == "h"
480+
fig = fn(y=numerical)
481+
assert list(fig.data[0].x) == range_4
482+
assert list(fig.data[0].y) == numerical
483+
assert fig.data[0].orientation == "v"
484+
fig = fn(numerical_df, x="col")
485+
assert list(fig.data[0].x) == numerical
486+
assert list(fig.data[0].y) == index
487+
assert fig.data[0].orientation == "h"
488+
fig = fn(numerical_df, y="col")
489+
assert list(fig.data[0].x) == index
490+
assert list(fig.data[0].y) == numerical
491+
assert fig.data[0].orientation == "v"
492+
493+
if fn != px.bar:
499494
fig = fn(x=categorical)
500495
assert list(fig.data[0].x) == categorical
501496
assert list(fig.data[0].y) == range_4
@@ -513,7 +508,7 @@ def test_x_or_y():
513508
assert list(fig.data[0].y) == categorical
514509
assert fig.data[0].orientation == "v"
515510

516-
for fn in bar_like:
511+
else:
517512
fig = fn(x=categorical)
518513
assert list(fig.data[0].x) == categorical
519514
assert list(fig.data[0].y) == constant

0 commit comments

Comments
 (0)