Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- ckpts/tensorboard/events.out.tfevents.1713955089.peacock-3.29161.0 +3 -0
- ckpts/universal/global_step80/zero/14.mlp.dense_h_to_4h.weight/exp_avg_sq.pt +3 -0
- ckpts/universal/global_step80/zero/18.attention.dense.weight/exp_avg.pt +3 -0
- ckpts/universal/global_step80/zero/26.mlp.dense_h_to_4h_swiglu.weight/exp_avg_sq.pt +3 -0
- ckpts/universal/global_step80/zero/26.mlp.dense_h_to_4h_swiglu.weight/fp32.pt +3 -0
- ckpts/universal/global_step80/zero/5.attention.dense.weight/exp_avg.pt +3 -0
- venv/lib/python3.10/site-packages/pandas/tests/dtypes/__init__.py +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/common.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/conftest.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_backend.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_boxplot_method.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_common.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_datetimelike.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_groupby.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_hist_method.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_misc.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_series.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__init__.py +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/test_frame.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/test_frame_color.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/test_frame_groupby.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/test_frame_legend.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/test_frame_subplots.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/test_hist_box_by.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/test_frame.py +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/test_frame_color.py +670 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/test_frame_groupby.py +72 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/test_frame_legend.py +272 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/test_frame_subplots.py +752 -0
- venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/test_hist_box_by.py +342 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/__pycache__/test_na_scalar.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/__pycache__/test_nat.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__init__.py +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_arithmetic.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_constructors.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_contains.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_formats.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_interval.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_overlaps.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_arithmetic.py +192 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_constructors.py +51 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_contains.py +73 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_formats.py +11 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_interval.py +87 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_overlaps.py +67 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__init__.py +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__pycache__/__init__.cpython-310.pyc +0 -0
ckpts/tensorboard/events.out.tfevents.1713955089.peacock-3.29161.0
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ab325104bd88dc3a9fe1b7c137dd6f0fbb2b3e02ce835ed0186cc75dd680052e
|
3 |
+
size 216569
|
ckpts/universal/global_step80/zero/14.mlp.dense_h_to_4h.weight/exp_avg_sq.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:822c165438324adc02ec40f3f376cc6330a501cb17f3b5aa9fc6d434730cb67b
|
3 |
+
size 33555627
|
ckpts/universal/global_step80/zero/18.attention.dense.weight/exp_avg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e8f91eede38a4e639ccfe2330bec2e8478fe9a6f5da7a7d3792a2164596e67cb
|
3 |
+
size 16778396
|
ckpts/universal/global_step80/zero/26.mlp.dense_h_to_4h_swiglu.weight/exp_avg_sq.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a226f98e93c6dc8d51dca1aefd53c9920fe55740e13d5a6652d34c3db83bca96
|
3 |
+
size 33555627
|
ckpts/universal/global_step80/zero/26.mlp.dense_h_to_4h_swiglu.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:492cc30afeb946f8219e4d5e0dbb2419daf574050c9bfa6b018e97d4a9499e76
|
3 |
+
size 33555533
|
ckpts/universal/global_step80/zero/5.attention.dense.weight/exp_avg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3c169d1bef4ba4303a79edffa5be71e6867f0a000a87ff40add4279b6d9a53d8
|
3 |
+
size 16778396
|
venv/lib/python3.10/site-packages/pandas/tests/dtypes/__init__.py
ADDED
File without changes
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/common.cpython-310.pyc
ADDED
Binary file (14.9 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/conftest.cpython-310.pyc
ADDED
Binary file (1.33 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_backend.cpython-310.pyc
ADDED
Binary file (3.52 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_boxplot_method.cpython-310.pyc
ADDED
Binary file (27.1 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_common.cpython-310.pyc
ADDED
Binary file (2.36 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_datetimelike.cpython-310.pyc
ADDED
Binary file (58.7 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_groupby.cpython-310.pyc
ADDED
Binary file (6.94 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_hist_method.cpython-310.pyc
ADDED
Binary file (32.8 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_misc.cpython-310.pyc
ADDED
Binary file (23.8 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/__pycache__/test_series.cpython-310.pyc
ADDED
Binary file (36.9 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__init__.py
ADDED
File without changes
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (195 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/test_frame.cpython-310.pyc
ADDED
Binary file (89.5 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/test_frame_color.cpython-310.pyc
ADDED
Binary file (31.9 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/test_frame_groupby.cpython-310.pyc
ADDED
Binary file (1.96 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/test_frame_legend.cpython-310.pyc
ADDED
Binary file (8.67 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/test_frame_subplots.cpython-310.pyc
ADDED
Binary file (23.8 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/__pycache__/test_hist_box_by.cpython-310.pyc
ADDED
Binary file (8.81 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/test_frame.py
ADDED
The diff for this file is too large to render.
See raw diff
|
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/test_frame_color.py
ADDED
@@ -0,0 +1,670 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""" Test cases for DataFrame.plot """
|
2 |
+
import re
|
3 |
+
|
4 |
+
import numpy as np
|
5 |
+
import pytest
|
6 |
+
|
7 |
+
import pandas as pd
|
8 |
+
from pandas import DataFrame
|
9 |
+
import pandas._testing as tm
|
10 |
+
from pandas.tests.plotting.common import (
|
11 |
+
_check_colors,
|
12 |
+
_check_plot_works,
|
13 |
+
_unpack_cycler,
|
14 |
+
)
|
15 |
+
from pandas.util.version import Version
|
16 |
+
|
17 |
+
mpl = pytest.importorskip("matplotlib")
|
18 |
+
plt = pytest.importorskip("matplotlib.pyplot")
|
19 |
+
cm = pytest.importorskip("matplotlib.cm")
|
20 |
+
|
21 |
+
|
22 |
+
def _check_colors_box(bp, box_c, whiskers_c, medians_c, caps_c="k", fliers_c=None):
|
23 |
+
if fliers_c is None:
|
24 |
+
fliers_c = "k"
|
25 |
+
_check_colors(bp["boxes"], linecolors=[box_c] * len(bp["boxes"]))
|
26 |
+
_check_colors(bp["whiskers"], linecolors=[whiskers_c] * len(bp["whiskers"]))
|
27 |
+
_check_colors(bp["medians"], linecolors=[medians_c] * len(bp["medians"]))
|
28 |
+
_check_colors(bp["fliers"], linecolors=[fliers_c] * len(bp["fliers"]))
|
29 |
+
_check_colors(bp["caps"], linecolors=[caps_c] * len(bp["caps"]))
|
30 |
+
|
31 |
+
|
32 |
+
class TestDataFrameColor:
|
33 |
+
@pytest.mark.parametrize(
|
34 |
+
"color", ["C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9"]
|
35 |
+
)
|
36 |
+
def test_mpl2_color_cycle_str(self, color):
|
37 |
+
# GH 15516
|
38 |
+
df = DataFrame(
|
39 |
+
np.random.default_rng(2).standard_normal((10, 3)), columns=["a", "b", "c"]
|
40 |
+
)
|
41 |
+
_check_plot_works(df.plot, color=color)
|
42 |
+
|
43 |
+
def test_color_single_series_list(self):
|
44 |
+
# GH 3486
|
45 |
+
df = DataFrame({"A": [1, 2, 3]})
|
46 |
+
_check_plot_works(df.plot, color=["red"])
|
47 |
+
|
48 |
+
@pytest.mark.parametrize("color", [(1, 0, 0), (1, 0, 0, 0.5)])
|
49 |
+
def test_rgb_tuple_color(self, color):
|
50 |
+
# GH 16695
|
51 |
+
df = DataFrame({"x": [1, 2], "y": [3, 4]})
|
52 |
+
_check_plot_works(df.plot, x="x", y="y", color=color)
|
53 |
+
|
54 |
+
def test_color_empty_string(self):
|
55 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((10, 2)))
|
56 |
+
with pytest.raises(ValueError, match="Invalid color argument:"):
|
57 |
+
df.plot(color="")
|
58 |
+
|
59 |
+
def test_color_and_style_arguments(self):
|
60 |
+
df = DataFrame({"x": [1, 2], "y": [3, 4]})
|
61 |
+
# passing both 'color' and 'style' arguments should be allowed
|
62 |
+
# if there is no color symbol in the style strings:
|
63 |
+
ax = df.plot(color=["red", "black"], style=["-", "--"])
|
64 |
+
# check that the linestyles are correctly set:
|
65 |
+
linestyle = [line.get_linestyle() for line in ax.lines]
|
66 |
+
assert linestyle == ["-", "--"]
|
67 |
+
# check that the colors are correctly set:
|
68 |
+
color = [line.get_color() for line in ax.lines]
|
69 |
+
assert color == ["red", "black"]
|
70 |
+
# passing both 'color' and 'style' arguments should not be allowed
|
71 |
+
# if there is a color symbol in the style strings:
|
72 |
+
msg = (
|
73 |
+
"Cannot pass 'style' string with a color symbol and 'color' keyword "
|
74 |
+
"argument. Please use one or the other or pass 'style' without a color "
|
75 |
+
"symbol"
|
76 |
+
)
|
77 |
+
with pytest.raises(ValueError, match=msg):
|
78 |
+
df.plot(color=["red", "black"], style=["k-", "r--"])
|
79 |
+
|
80 |
+
@pytest.mark.parametrize(
|
81 |
+
"color, expected",
|
82 |
+
[
|
83 |
+
("green", ["green"] * 4),
|
84 |
+
(["yellow", "red", "green", "blue"], ["yellow", "red", "green", "blue"]),
|
85 |
+
],
|
86 |
+
)
|
87 |
+
def test_color_and_marker(self, color, expected):
|
88 |
+
# GH 21003
|
89 |
+
df = DataFrame(np.random.default_rng(2).random((7, 4)))
|
90 |
+
ax = df.plot(color=color, style="d--")
|
91 |
+
# check colors
|
92 |
+
result = [i.get_color() for i in ax.lines]
|
93 |
+
assert result == expected
|
94 |
+
# check markers and linestyles
|
95 |
+
assert all(i.get_linestyle() == "--" for i in ax.lines)
|
96 |
+
assert all(i.get_marker() == "d" for i in ax.lines)
|
97 |
+
|
98 |
+
def test_bar_colors(self):
|
99 |
+
default_colors = _unpack_cycler(plt.rcParams)
|
100 |
+
|
101 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
102 |
+
ax = df.plot.bar()
|
103 |
+
_check_colors(ax.patches[::5], facecolors=default_colors[:5])
|
104 |
+
|
105 |
+
def test_bar_colors_custom(self):
|
106 |
+
custom_colors = "rgcby"
|
107 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
108 |
+
ax = df.plot.bar(color=custom_colors)
|
109 |
+
_check_colors(ax.patches[::5], facecolors=custom_colors)
|
110 |
+
|
111 |
+
@pytest.mark.parametrize("colormap", ["jet", cm.jet])
|
112 |
+
def test_bar_colors_cmap(self, colormap):
|
113 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
114 |
+
|
115 |
+
ax = df.plot.bar(colormap=colormap)
|
116 |
+
rgba_colors = [cm.jet(n) for n in np.linspace(0, 1, 5)]
|
117 |
+
_check_colors(ax.patches[::5], facecolors=rgba_colors)
|
118 |
+
|
119 |
+
def test_bar_colors_single_col(self):
|
120 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
121 |
+
ax = df.loc[:, [0]].plot.bar(color="DodgerBlue")
|
122 |
+
_check_colors([ax.patches[0]], facecolors=["DodgerBlue"])
|
123 |
+
|
124 |
+
def test_bar_colors_green(self):
|
125 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
126 |
+
ax = df.plot(kind="bar", color="green")
|
127 |
+
_check_colors(ax.patches[::5], facecolors=["green"] * 5)
|
128 |
+
|
129 |
+
def test_bar_user_colors(self):
|
130 |
+
df = DataFrame(
|
131 |
+
{"A": range(4), "B": range(1, 5), "color": ["red", "blue", "blue", "red"]}
|
132 |
+
)
|
133 |
+
# This should *only* work when `y` is specified, else
|
134 |
+
# we use one color per column
|
135 |
+
ax = df.plot.bar(y="A", color=df["color"])
|
136 |
+
result = [p.get_facecolor() for p in ax.patches]
|
137 |
+
expected = [
|
138 |
+
(1.0, 0.0, 0.0, 1.0),
|
139 |
+
(0.0, 0.0, 1.0, 1.0),
|
140 |
+
(0.0, 0.0, 1.0, 1.0),
|
141 |
+
(1.0, 0.0, 0.0, 1.0),
|
142 |
+
]
|
143 |
+
assert result == expected
|
144 |
+
|
145 |
+
def test_if_scatterplot_colorbar_affects_xaxis_visibility(self):
|
146 |
+
# addressing issue #10611, to ensure colobar does not
|
147 |
+
# interfere with x-axis label and ticklabels with
|
148 |
+
# ipython inline backend.
|
149 |
+
random_array = np.random.default_rng(2).random((10, 3))
|
150 |
+
df = DataFrame(random_array, columns=["A label", "B label", "C label"])
|
151 |
+
|
152 |
+
ax1 = df.plot.scatter(x="A label", y="B label")
|
153 |
+
ax2 = df.plot.scatter(x="A label", y="B label", c="C label")
|
154 |
+
|
155 |
+
vis1 = [vis.get_visible() for vis in ax1.xaxis.get_minorticklabels()]
|
156 |
+
vis2 = [vis.get_visible() for vis in ax2.xaxis.get_minorticklabels()]
|
157 |
+
assert vis1 == vis2
|
158 |
+
|
159 |
+
vis1 = [vis.get_visible() for vis in ax1.xaxis.get_majorticklabels()]
|
160 |
+
vis2 = [vis.get_visible() for vis in ax2.xaxis.get_majorticklabels()]
|
161 |
+
assert vis1 == vis2
|
162 |
+
|
163 |
+
assert (
|
164 |
+
ax1.xaxis.get_label().get_visible() == ax2.xaxis.get_label().get_visible()
|
165 |
+
)
|
166 |
+
|
167 |
+
def test_if_hexbin_xaxis_label_is_visible(self):
|
168 |
+
# addressing issue #10678, to ensure colobar does not
|
169 |
+
# interfere with x-axis label and ticklabels with
|
170 |
+
# ipython inline backend.
|
171 |
+
random_array = np.random.default_rng(2).random((10, 3))
|
172 |
+
df = DataFrame(random_array, columns=["A label", "B label", "C label"])
|
173 |
+
|
174 |
+
ax = df.plot.hexbin("A label", "B label", gridsize=12)
|
175 |
+
assert all(vis.get_visible() for vis in ax.xaxis.get_minorticklabels())
|
176 |
+
assert all(vis.get_visible() for vis in ax.xaxis.get_majorticklabels())
|
177 |
+
assert ax.xaxis.get_label().get_visible()
|
178 |
+
|
179 |
+
def test_if_scatterplot_colorbars_are_next_to_parent_axes(self):
|
180 |
+
random_array = np.random.default_rng(2).random((10, 3))
|
181 |
+
df = DataFrame(random_array, columns=["A label", "B label", "C label"])
|
182 |
+
|
183 |
+
fig, axes = plt.subplots(1, 2)
|
184 |
+
df.plot.scatter("A label", "B label", c="C label", ax=axes[0])
|
185 |
+
df.plot.scatter("A label", "B label", c="C label", ax=axes[1])
|
186 |
+
plt.tight_layout()
|
187 |
+
|
188 |
+
points = np.array([ax.get_position().get_points() for ax in fig.axes])
|
189 |
+
axes_x_coords = points[:, :, 0]
|
190 |
+
parent_distance = axes_x_coords[1, :] - axes_x_coords[0, :]
|
191 |
+
colorbar_distance = axes_x_coords[3, :] - axes_x_coords[2, :]
|
192 |
+
assert np.isclose(parent_distance, colorbar_distance, atol=1e-7).all()
|
193 |
+
|
194 |
+
@pytest.mark.parametrize("cmap", [None, "Greys"])
|
195 |
+
def test_scatter_with_c_column_name_with_colors(self, cmap):
|
196 |
+
# https://github.com/pandas-dev/pandas/issues/34316
|
197 |
+
|
198 |
+
df = DataFrame(
|
199 |
+
[[5.1, 3.5], [4.9, 3.0], [7.0, 3.2], [6.4, 3.2], [5.9, 3.0]],
|
200 |
+
columns=["length", "width"],
|
201 |
+
)
|
202 |
+
df["species"] = ["r", "r", "g", "g", "b"]
|
203 |
+
if cmap is not None:
|
204 |
+
with tm.assert_produces_warning(UserWarning, check_stacklevel=False):
|
205 |
+
ax = df.plot.scatter(x=0, y=1, cmap=cmap, c="species")
|
206 |
+
else:
|
207 |
+
ax = df.plot.scatter(x=0, y=1, c="species", cmap=cmap)
|
208 |
+
assert ax.collections[0].colorbar is None
|
209 |
+
|
210 |
+
def test_scatter_colors(self):
|
211 |
+
df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 3], "c": [1, 2, 3]})
|
212 |
+
with pytest.raises(TypeError, match="Specify exactly one of `c` and `color`"):
|
213 |
+
df.plot.scatter(x="a", y="b", c="c", color="green")
|
214 |
+
|
215 |
+
def test_scatter_colors_not_raising_warnings(self):
|
216 |
+
# GH-53908. Do not raise UserWarning: No data for colormapping
|
217 |
+
# provided via 'c'. Parameters 'cmap' will be ignored
|
218 |
+
df = DataFrame({"x": [1, 2, 3], "y": [1, 2, 3]})
|
219 |
+
with tm.assert_produces_warning(None):
|
220 |
+
df.plot.scatter(x="x", y="y", c="b")
|
221 |
+
|
222 |
+
def test_scatter_colors_default(self):
|
223 |
+
df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 3], "c": [1, 2, 3]})
|
224 |
+
default_colors = _unpack_cycler(mpl.pyplot.rcParams)
|
225 |
+
|
226 |
+
ax = df.plot.scatter(x="a", y="b", c="c")
|
227 |
+
tm.assert_numpy_array_equal(
|
228 |
+
ax.collections[0].get_facecolor()[0],
|
229 |
+
np.array(mpl.colors.ColorConverter.to_rgba(default_colors[0])),
|
230 |
+
)
|
231 |
+
|
232 |
+
def test_scatter_colors_white(self):
|
233 |
+
df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 3], "c": [1, 2, 3]})
|
234 |
+
ax = df.plot.scatter(x="a", y="b", color="white")
|
235 |
+
tm.assert_numpy_array_equal(
|
236 |
+
ax.collections[0].get_facecolor()[0],
|
237 |
+
np.array([1, 1, 1, 1], dtype=np.float64),
|
238 |
+
)
|
239 |
+
|
240 |
+
def test_scatter_colorbar_different_cmap(self):
|
241 |
+
# GH 33389
|
242 |
+
df = DataFrame({"x": [1, 2, 3], "y": [1, 3, 2], "c": [1, 2, 3]})
|
243 |
+
df["x2"] = df["x"] + 1
|
244 |
+
|
245 |
+
_, ax = plt.subplots()
|
246 |
+
df.plot("x", "y", c="c", kind="scatter", cmap="cividis", ax=ax)
|
247 |
+
df.plot("x2", "y", c="c", kind="scatter", cmap="magma", ax=ax)
|
248 |
+
|
249 |
+
assert ax.collections[0].cmap.name == "cividis"
|
250 |
+
assert ax.collections[1].cmap.name == "magma"
|
251 |
+
|
252 |
+
def test_line_colors(self):
|
253 |
+
custom_colors = "rgcby"
|
254 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
255 |
+
|
256 |
+
ax = df.plot(color=custom_colors)
|
257 |
+
_check_colors(ax.get_lines(), linecolors=custom_colors)
|
258 |
+
|
259 |
+
plt.close("all")
|
260 |
+
|
261 |
+
ax2 = df.plot(color=custom_colors)
|
262 |
+
lines2 = ax2.get_lines()
|
263 |
+
|
264 |
+
for l1, l2 in zip(ax.get_lines(), lines2):
|
265 |
+
assert l1.get_color() == l2.get_color()
|
266 |
+
|
267 |
+
@pytest.mark.parametrize("colormap", ["jet", cm.jet])
|
268 |
+
def test_line_colors_cmap(self, colormap):
|
269 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
270 |
+
ax = df.plot(colormap=colormap)
|
271 |
+
rgba_colors = [cm.jet(n) for n in np.linspace(0, 1, len(df))]
|
272 |
+
_check_colors(ax.get_lines(), linecolors=rgba_colors)
|
273 |
+
|
274 |
+
def test_line_colors_single_col(self):
|
275 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
276 |
+
# make color a list if plotting one column frame
|
277 |
+
# handles cases like df.plot(color='DodgerBlue')
|
278 |
+
ax = df.loc[:, [0]].plot(color="DodgerBlue")
|
279 |
+
_check_colors(ax.lines, linecolors=["DodgerBlue"])
|
280 |
+
|
281 |
+
def test_line_colors_single_color(self):
|
282 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
283 |
+
ax = df.plot(color="red")
|
284 |
+
_check_colors(ax.get_lines(), linecolors=["red"] * 5)
|
285 |
+
|
286 |
+
def test_line_colors_hex(self):
|
287 |
+
# GH 10299
|
288 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
289 |
+
custom_colors = ["#FF0000", "#0000FF", "#FFFF00", "#000000", "#FFFFFF"]
|
290 |
+
ax = df.plot(color=custom_colors)
|
291 |
+
_check_colors(ax.get_lines(), linecolors=custom_colors)
|
292 |
+
|
293 |
+
def test_dont_modify_colors(self):
|
294 |
+
colors = ["r", "g", "b"]
|
295 |
+
DataFrame(np.random.default_rng(2).random((10, 2))).plot(color=colors)
|
296 |
+
assert len(colors) == 3
|
297 |
+
|
298 |
+
def test_line_colors_and_styles_subplots(self):
|
299 |
+
# GH 9894
|
300 |
+
default_colors = _unpack_cycler(mpl.pyplot.rcParams)
|
301 |
+
|
302 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
303 |
+
|
304 |
+
axes = df.plot(subplots=True)
|
305 |
+
for ax, c in zip(axes, list(default_colors)):
|
306 |
+
_check_colors(ax.get_lines(), linecolors=[c])
|
307 |
+
|
308 |
+
@pytest.mark.parametrize("color", ["k", "green"])
|
309 |
+
def test_line_colors_and_styles_subplots_single_color_str(self, color):
|
310 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
311 |
+
axes = df.plot(subplots=True, color=color)
|
312 |
+
for ax in axes:
|
313 |
+
_check_colors(ax.get_lines(), linecolors=[color])
|
314 |
+
|
315 |
+
@pytest.mark.parametrize("color", ["rgcby", list("rgcby")])
|
316 |
+
def test_line_colors_and_styles_subplots_custom_colors(self, color):
|
317 |
+
# GH 9894
|
318 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
319 |
+
axes = df.plot(color=color, subplots=True)
|
320 |
+
for ax, c in zip(axes, list(color)):
|
321 |
+
_check_colors(ax.get_lines(), linecolors=[c])
|
322 |
+
|
323 |
+
def test_line_colors_and_styles_subplots_colormap_hex(self):
|
324 |
+
# GH 9894
|
325 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
326 |
+
# GH 10299
|
327 |
+
custom_colors = ["#FF0000", "#0000FF", "#FFFF00", "#000000", "#FFFFFF"]
|
328 |
+
axes = df.plot(color=custom_colors, subplots=True)
|
329 |
+
for ax, c in zip(axes, list(custom_colors)):
|
330 |
+
_check_colors(ax.get_lines(), linecolors=[c])
|
331 |
+
|
332 |
+
@pytest.mark.parametrize("cmap", ["jet", cm.jet])
|
333 |
+
def test_line_colors_and_styles_subplots_colormap_subplot(self, cmap):
|
334 |
+
# GH 9894
|
335 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
336 |
+
rgba_colors = [cm.jet(n) for n in np.linspace(0, 1, len(df))]
|
337 |
+
axes = df.plot(colormap=cmap, subplots=True)
|
338 |
+
for ax, c in zip(axes, rgba_colors):
|
339 |
+
_check_colors(ax.get_lines(), linecolors=[c])
|
340 |
+
|
341 |
+
def test_line_colors_and_styles_subplots_single_col(self):
|
342 |
+
# GH 9894
|
343 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
344 |
+
# make color a list if plotting one column frame
|
345 |
+
# handles cases like df.plot(color='DodgerBlue')
|
346 |
+
axes = df.loc[:, [0]].plot(color="DodgerBlue", subplots=True)
|
347 |
+
_check_colors(axes[0].lines, linecolors=["DodgerBlue"])
|
348 |
+
|
349 |
+
def test_line_colors_and_styles_subplots_single_char(self):
|
350 |
+
# GH 9894
|
351 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
352 |
+
# single character style
|
353 |
+
axes = df.plot(style="r", subplots=True)
|
354 |
+
for ax in axes:
|
355 |
+
_check_colors(ax.get_lines(), linecolors=["r"])
|
356 |
+
|
357 |
+
def test_line_colors_and_styles_subplots_list_styles(self):
|
358 |
+
# GH 9894
|
359 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
360 |
+
# list of styles
|
361 |
+
styles = list("rgcby")
|
362 |
+
axes = df.plot(style=styles, subplots=True)
|
363 |
+
for ax, c in zip(axes, styles):
|
364 |
+
_check_colors(ax.get_lines(), linecolors=[c])
|
365 |
+
|
366 |
+
def test_area_colors(self):
|
367 |
+
from matplotlib.collections import PolyCollection
|
368 |
+
|
369 |
+
custom_colors = "rgcby"
|
370 |
+
df = DataFrame(np.random.default_rng(2).random((5, 5)))
|
371 |
+
|
372 |
+
ax = df.plot.area(color=custom_colors)
|
373 |
+
_check_colors(ax.get_lines(), linecolors=custom_colors)
|
374 |
+
poly = [o for o in ax.get_children() if isinstance(o, PolyCollection)]
|
375 |
+
_check_colors(poly, facecolors=custom_colors)
|
376 |
+
|
377 |
+
handles, _ = ax.get_legend_handles_labels()
|
378 |
+
_check_colors(handles, facecolors=custom_colors)
|
379 |
+
|
380 |
+
for h in handles:
|
381 |
+
assert h.get_alpha() is None
|
382 |
+
|
383 |
+
def test_area_colors_poly(self):
|
384 |
+
from matplotlib import cm
|
385 |
+
from matplotlib.collections import PolyCollection
|
386 |
+
|
387 |
+
df = DataFrame(np.random.default_rng(2).random((5, 5)))
|
388 |
+
ax = df.plot.area(colormap="jet")
|
389 |
+
jet_colors = [cm.jet(n) for n in np.linspace(0, 1, len(df))]
|
390 |
+
_check_colors(ax.get_lines(), linecolors=jet_colors)
|
391 |
+
poly = [o for o in ax.get_children() if isinstance(o, PolyCollection)]
|
392 |
+
_check_colors(poly, facecolors=jet_colors)
|
393 |
+
|
394 |
+
handles, _ = ax.get_legend_handles_labels()
|
395 |
+
_check_colors(handles, facecolors=jet_colors)
|
396 |
+
for h in handles:
|
397 |
+
assert h.get_alpha() is None
|
398 |
+
|
399 |
+
def test_area_colors_stacked_false(self):
|
400 |
+
from matplotlib import cm
|
401 |
+
from matplotlib.collections import PolyCollection
|
402 |
+
|
403 |
+
df = DataFrame(np.random.default_rng(2).random((5, 5)))
|
404 |
+
jet_colors = [cm.jet(n) for n in np.linspace(0, 1, len(df))]
|
405 |
+
# When stacked=False, alpha is set to 0.5
|
406 |
+
ax = df.plot.area(colormap=cm.jet, stacked=False)
|
407 |
+
_check_colors(ax.get_lines(), linecolors=jet_colors)
|
408 |
+
poly = [o for o in ax.get_children() if isinstance(o, PolyCollection)]
|
409 |
+
jet_with_alpha = [(c[0], c[1], c[2], 0.5) for c in jet_colors]
|
410 |
+
_check_colors(poly, facecolors=jet_with_alpha)
|
411 |
+
|
412 |
+
handles, _ = ax.get_legend_handles_labels()
|
413 |
+
linecolors = jet_with_alpha
|
414 |
+
_check_colors(handles[: len(jet_colors)], linecolors=linecolors)
|
415 |
+
for h in handles:
|
416 |
+
assert h.get_alpha() == 0.5
|
417 |
+
|
418 |
+
def test_hist_colors(self):
|
419 |
+
default_colors = _unpack_cycler(mpl.pyplot.rcParams)
|
420 |
+
|
421 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
422 |
+
ax = df.plot.hist()
|
423 |
+
_check_colors(ax.patches[::10], facecolors=default_colors[:5])
|
424 |
+
|
425 |
+
def test_hist_colors_single_custom(self):
|
426 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
427 |
+
custom_colors = "rgcby"
|
428 |
+
ax = df.plot.hist(color=custom_colors)
|
429 |
+
_check_colors(ax.patches[::10], facecolors=custom_colors)
|
430 |
+
|
431 |
+
@pytest.mark.parametrize("colormap", ["jet", cm.jet])
|
432 |
+
def test_hist_colors_cmap(self, colormap):
|
433 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
434 |
+
ax = df.plot.hist(colormap=colormap)
|
435 |
+
rgba_colors = [cm.jet(n) for n in np.linspace(0, 1, 5)]
|
436 |
+
_check_colors(ax.patches[::10], facecolors=rgba_colors)
|
437 |
+
|
438 |
+
def test_hist_colors_single_col(self):
|
439 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
440 |
+
ax = df.loc[:, [0]].plot.hist(color="DodgerBlue")
|
441 |
+
_check_colors([ax.patches[0]], facecolors=["DodgerBlue"])
|
442 |
+
|
443 |
+
def test_hist_colors_single_color(self):
|
444 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
445 |
+
ax = df.plot(kind="hist", color="green")
|
446 |
+
_check_colors(ax.patches[::10], facecolors=["green"] * 5)
|
447 |
+
|
448 |
+
def test_kde_colors(self):
|
449 |
+
pytest.importorskip("scipy")
|
450 |
+
custom_colors = "rgcby"
|
451 |
+
df = DataFrame(np.random.default_rng(2).random((5, 5)))
|
452 |
+
|
453 |
+
ax = df.plot.kde(color=custom_colors)
|
454 |
+
_check_colors(ax.get_lines(), linecolors=custom_colors)
|
455 |
+
|
456 |
+
@pytest.mark.parametrize("colormap", ["jet", cm.jet])
|
457 |
+
def test_kde_colors_cmap(self, colormap):
|
458 |
+
pytest.importorskip("scipy")
|
459 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
460 |
+
ax = df.plot.kde(colormap=colormap)
|
461 |
+
rgba_colors = [cm.jet(n) for n in np.linspace(0, 1, len(df))]
|
462 |
+
_check_colors(ax.get_lines(), linecolors=rgba_colors)
|
463 |
+
|
464 |
+
def test_kde_colors_and_styles_subplots(self):
|
465 |
+
pytest.importorskip("scipy")
|
466 |
+
default_colors = _unpack_cycler(mpl.pyplot.rcParams)
|
467 |
+
|
468 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
469 |
+
|
470 |
+
axes = df.plot(kind="kde", subplots=True)
|
471 |
+
for ax, c in zip(axes, list(default_colors)):
|
472 |
+
_check_colors(ax.get_lines(), linecolors=[c])
|
473 |
+
|
474 |
+
@pytest.mark.parametrize("colormap", ["k", "red"])
|
475 |
+
def test_kde_colors_and_styles_subplots_single_col_str(self, colormap):
|
476 |
+
pytest.importorskip("scipy")
|
477 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
478 |
+
axes = df.plot(kind="kde", color=colormap, subplots=True)
|
479 |
+
for ax in axes:
|
480 |
+
_check_colors(ax.get_lines(), linecolors=[colormap])
|
481 |
+
|
482 |
+
def test_kde_colors_and_styles_subplots_custom_color(self):
|
483 |
+
pytest.importorskip("scipy")
|
484 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
485 |
+
custom_colors = "rgcby"
|
486 |
+
axes = df.plot(kind="kde", color=custom_colors, subplots=True)
|
487 |
+
for ax, c in zip(axes, list(custom_colors)):
|
488 |
+
_check_colors(ax.get_lines(), linecolors=[c])
|
489 |
+
|
490 |
+
@pytest.mark.parametrize("colormap", ["jet", cm.jet])
|
491 |
+
def test_kde_colors_and_styles_subplots_cmap(self, colormap):
|
492 |
+
pytest.importorskip("scipy")
|
493 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
494 |
+
rgba_colors = [cm.jet(n) for n in np.linspace(0, 1, len(df))]
|
495 |
+
axes = df.plot(kind="kde", colormap=colormap, subplots=True)
|
496 |
+
for ax, c in zip(axes, rgba_colors):
|
497 |
+
_check_colors(ax.get_lines(), linecolors=[c])
|
498 |
+
|
499 |
+
def test_kde_colors_and_styles_subplots_single_col(self):
|
500 |
+
pytest.importorskip("scipy")
|
501 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
502 |
+
# make color a list if plotting one column frame
|
503 |
+
# handles cases like df.plot(color='DodgerBlue')
|
504 |
+
axes = df.loc[:, [0]].plot(kind="kde", color="DodgerBlue", subplots=True)
|
505 |
+
_check_colors(axes[0].lines, linecolors=["DodgerBlue"])
|
506 |
+
|
507 |
+
def test_kde_colors_and_styles_subplots_single_char(self):
|
508 |
+
pytest.importorskip("scipy")
|
509 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
510 |
+
# list of styles
|
511 |
+
# single character style
|
512 |
+
axes = df.plot(kind="kde", style="r", subplots=True)
|
513 |
+
for ax in axes:
|
514 |
+
_check_colors(ax.get_lines(), linecolors=["r"])
|
515 |
+
|
516 |
+
def test_kde_colors_and_styles_subplots_list(self):
|
517 |
+
pytest.importorskip("scipy")
|
518 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
519 |
+
# list of styles
|
520 |
+
styles = list("rgcby")
|
521 |
+
axes = df.plot(kind="kde", style=styles, subplots=True)
|
522 |
+
for ax, c in zip(axes, styles):
|
523 |
+
_check_colors(ax.get_lines(), linecolors=[c])
|
524 |
+
|
525 |
+
def test_boxplot_colors(self):
|
526 |
+
default_colors = _unpack_cycler(mpl.pyplot.rcParams)
|
527 |
+
|
528 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
529 |
+
bp = df.plot.box(return_type="dict")
|
530 |
+
_check_colors_box(
|
531 |
+
bp,
|
532 |
+
default_colors[0],
|
533 |
+
default_colors[0],
|
534 |
+
default_colors[2],
|
535 |
+
default_colors[0],
|
536 |
+
)
|
537 |
+
|
538 |
+
def test_boxplot_colors_dict_colors(self):
|
539 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
540 |
+
dict_colors = {
|
541 |
+
"boxes": "#572923",
|
542 |
+
"whiskers": "#982042",
|
543 |
+
"medians": "#804823",
|
544 |
+
"caps": "#123456",
|
545 |
+
}
|
546 |
+
bp = df.plot.box(color=dict_colors, sym="r+", return_type="dict")
|
547 |
+
_check_colors_box(
|
548 |
+
bp,
|
549 |
+
dict_colors["boxes"],
|
550 |
+
dict_colors["whiskers"],
|
551 |
+
dict_colors["medians"],
|
552 |
+
dict_colors["caps"],
|
553 |
+
"r",
|
554 |
+
)
|
555 |
+
|
556 |
+
def test_boxplot_colors_default_color(self):
|
557 |
+
default_colors = _unpack_cycler(mpl.pyplot.rcParams)
|
558 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
559 |
+
# partial colors
|
560 |
+
dict_colors = {"whiskers": "c", "medians": "m"}
|
561 |
+
bp = df.plot.box(color=dict_colors, return_type="dict")
|
562 |
+
_check_colors_box(bp, default_colors[0], "c", "m", default_colors[0])
|
563 |
+
|
564 |
+
@pytest.mark.parametrize("colormap", ["jet", cm.jet])
|
565 |
+
def test_boxplot_colors_cmap(self, colormap):
|
566 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
567 |
+
bp = df.plot.box(colormap=colormap, return_type="dict")
|
568 |
+
jet_colors = [cm.jet(n) for n in np.linspace(0, 1, 3)]
|
569 |
+
_check_colors_box(
|
570 |
+
bp, jet_colors[0], jet_colors[0], jet_colors[2], jet_colors[0]
|
571 |
+
)
|
572 |
+
|
573 |
+
def test_boxplot_colors_single(self):
|
574 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
575 |
+
# string color is applied to all artists except fliers
|
576 |
+
bp = df.plot.box(color="DodgerBlue", return_type="dict")
|
577 |
+
_check_colors_box(bp, "DodgerBlue", "DodgerBlue", "DodgerBlue", "DodgerBlue")
|
578 |
+
|
579 |
+
def test_boxplot_colors_tuple(self):
|
580 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
581 |
+
# tuple is also applied to all artists except fliers
|
582 |
+
bp = df.plot.box(color=(0, 1, 0), sym="#123456", return_type="dict")
|
583 |
+
_check_colors_box(bp, (0, 1, 0), (0, 1, 0), (0, 1, 0), (0, 1, 0), "#123456")
|
584 |
+
|
585 |
+
def test_boxplot_colors_invalid(self):
|
586 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
587 |
+
msg = re.escape(
|
588 |
+
"color dict contains invalid key 'xxxx'. The key must be either "
|
589 |
+
"['boxes', 'whiskers', 'medians', 'caps']"
|
590 |
+
)
|
591 |
+
with pytest.raises(ValueError, match=msg):
|
592 |
+
# Color contains invalid key results in ValueError
|
593 |
+
df.plot.box(color={"boxes": "red", "xxxx": "blue"})
|
594 |
+
|
595 |
+
def test_default_color_cycle(self):
|
596 |
+
import cycler
|
597 |
+
|
598 |
+
colors = list("rgbk")
|
599 |
+
plt.rcParams["axes.prop_cycle"] = cycler.cycler("color", colors)
|
600 |
+
|
601 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 3)))
|
602 |
+
ax = df.plot()
|
603 |
+
|
604 |
+
expected = _unpack_cycler(plt.rcParams)[:3]
|
605 |
+
_check_colors(ax.get_lines(), linecolors=expected)
|
606 |
+
|
607 |
+
def test_no_color_bar(self):
|
608 |
+
df = DataFrame(
|
609 |
+
{
|
610 |
+
"A": np.random.default_rng(2).uniform(size=20),
|
611 |
+
"B": np.random.default_rng(2).uniform(size=20),
|
612 |
+
"C": np.arange(20) + np.random.default_rng(2).uniform(size=20),
|
613 |
+
}
|
614 |
+
)
|
615 |
+
ax = df.plot.hexbin(x="A", y="B", colorbar=None)
|
616 |
+
assert ax.collections[0].colorbar is None
|
617 |
+
|
618 |
+
def test_mixing_cmap_and_colormap_raises(self):
|
619 |
+
df = DataFrame(
|
620 |
+
{
|
621 |
+
"A": np.random.default_rng(2).uniform(size=20),
|
622 |
+
"B": np.random.default_rng(2).uniform(size=20),
|
623 |
+
"C": np.arange(20) + np.random.default_rng(2).uniform(size=20),
|
624 |
+
}
|
625 |
+
)
|
626 |
+
msg = "Only specify one of `cmap` and `colormap`"
|
627 |
+
with pytest.raises(TypeError, match=msg):
|
628 |
+
df.plot.hexbin(x="A", y="B", cmap="YlGn", colormap="BuGn")
|
629 |
+
|
630 |
+
def test_passed_bar_colors(self):
|
631 |
+
color_tuples = [(0.9, 0, 0, 1), (0, 0.9, 0, 1), (0, 0, 0.9, 1)]
|
632 |
+
colormap = mpl.colors.ListedColormap(color_tuples)
|
633 |
+
barplot = DataFrame([[1, 2, 3]]).plot(kind="bar", cmap=colormap)
|
634 |
+
assert color_tuples == [c.get_facecolor() for c in barplot.patches]
|
635 |
+
|
636 |
+
def test_rcParams_bar_colors(self):
|
637 |
+
color_tuples = [(0.9, 0, 0, 1), (0, 0.9, 0, 1), (0, 0, 0.9, 1)]
|
638 |
+
with mpl.rc_context(rc={"axes.prop_cycle": mpl.cycler("color", color_tuples)}):
|
639 |
+
barplot = DataFrame([[1, 2, 3]]).plot(kind="bar")
|
640 |
+
assert color_tuples == [c.get_facecolor() for c in barplot.patches]
|
641 |
+
|
642 |
+
def test_colors_of_columns_with_same_name(self):
|
643 |
+
# ISSUE 11136 -> https://github.com/pandas-dev/pandas/issues/11136
|
644 |
+
# Creating a DataFrame with duplicate column labels and testing colors of them.
|
645 |
+
df = DataFrame({"b": [0, 1, 0], "a": [1, 2, 3]})
|
646 |
+
df1 = DataFrame({"a": [2, 4, 6]})
|
647 |
+
df_concat = pd.concat([df, df1], axis=1)
|
648 |
+
result = df_concat.plot()
|
649 |
+
legend = result.get_legend()
|
650 |
+
if Version(mpl.__version__) < Version("3.7"):
|
651 |
+
handles = legend.legendHandles
|
652 |
+
else:
|
653 |
+
handles = legend.legend_handles
|
654 |
+
for legend, line in zip(handles, result.lines):
|
655 |
+
assert legend.get_color() == line.get_color()
|
656 |
+
|
657 |
+
def test_invalid_colormap(self):
|
658 |
+
df = DataFrame(
|
659 |
+
np.random.default_rng(2).standard_normal((3, 2)), columns=["A", "B"]
|
660 |
+
)
|
661 |
+
msg = "(is not a valid value)|(is not a known colormap)"
|
662 |
+
with pytest.raises((ValueError, KeyError), match=msg):
|
663 |
+
df.plot(colormap="invalid_colormap")
|
664 |
+
|
665 |
+
def test_dataframe_none_color(self):
|
666 |
+
# GH51953
|
667 |
+
df = DataFrame([[1, 2, 3]])
|
668 |
+
ax = df.plot(color=None)
|
669 |
+
expected = _unpack_cycler(mpl.pyplot.rcParams)[:3]
|
670 |
+
_check_colors(ax.get_lines(), linecolors=expected)
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/test_frame_groupby.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""" Test cases for DataFrame.plot """
|
2 |
+
|
3 |
+
import pytest
|
4 |
+
|
5 |
+
from pandas import DataFrame
|
6 |
+
from pandas.tests.plotting.common import _check_visible
|
7 |
+
|
8 |
+
pytest.importorskip("matplotlib")
|
9 |
+
|
10 |
+
|
11 |
+
class TestDataFramePlotsGroupby:
|
12 |
+
def _assert_ytickslabels_visibility(self, axes, expected):
|
13 |
+
for ax, exp in zip(axes, expected):
|
14 |
+
_check_visible(ax.get_yticklabels(), visible=exp)
|
15 |
+
|
16 |
+
def _assert_xtickslabels_visibility(self, axes, expected):
|
17 |
+
for ax, exp in zip(axes, expected):
|
18 |
+
_check_visible(ax.get_xticklabels(), visible=exp)
|
19 |
+
|
20 |
+
@pytest.mark.parametrize(
|
21 |
+
"kwargs, expected",
|
22 |
+
[
|
23 |
+
# behavior without keyword
|
24 |
+
({}, [True, False, True, False]),
|
25 |
+
# set sharey=True should be identical
|
26 |
+
({"sharey": True}, [True, False, True, False]),
|
27 |
+
# sharey=False, all yticklabels should be visible
|
28 |
+
({"sharey": False}, [True, True, True, True]),
|
29 |
+
],
|
30 |
+
)
|
31 |
+
def test_groupby_boxplot_sharey(self, kwargs, expected):
|
32 |
+
# https://github.com/pandas-dev/pandas/issues/20968
|
33 |
+
# sharey can now be switched check whether the right
|
34 |
+
# pair of axes is turned on or off
|
35 |
+
df = DataFrame(
|
36 |
+
{
|
37 |
+
"a": [-1.43, -0.15, -3.70, -1.43, -0.14],
|
38 |
+
"b": [0.56, 0.84, 0.29, 0.56, 0.85],
|
39 |
+
"c": [0, 1, 2, 3, 1],
|
40 |
+
},
|
41 |
+
index=[0, 1, 2, 3, 4],
|
42 |
+
)
|
43 |
+
axes = df.groupby("c").boxplot(**kwargs)
|
44 |
+
self._assert_ytickslabels_visibility(axes, expected)
|
45 |
+
|
46 |
+
@pytest.mark.parametrize(
|
47 |
+
"kwargs, expected",
|
48 |
+
[
|
49 |
+
# behavior without keyword
|
50 |
+
({}, [True, True, True, True]),
|
51 |
+
# set sharex=False should be identical
|
52 |
+
({"sharex": False}, [True, True, True, True]),
|
53 |
+
# sharex=True, xticklabels should be visible
|
54 |
+
# only for bottom plots
|
55 |
+
({"sharex": True}, [False, False, True, True]),
|
56 |
+
],
|
57 |
+
)
|
58 |
+
def test_groupby_boxplot_sharex(self, kwargs, expected):
|
59 |
+
# https://github.com/pandas-dev/pandas/issues/20968
|
60 |
+
# sharex can now be switched check whether the right
|
61 |
+
# pair of axes is turned on or off
|
62 |
+
|
63 |
+
df = DataFrame(
|
64 |
+
{
|
65 |
+
"a": [-1.43, -0.15, -3.70, -1.43, -0.14],
|
66 |
+
"b": [0.56, 0.84, 0.29, 0.56, 0.85],
|
67 |
+
"c": [0, 1, 2, 3, 1],
|
68 |
+
},
|
69 |
+
index=[0, 1, 2, 3, 4],
|
70 |
+
)
|
71 |
+
axes = df.groupby("c").boxplot(**kwargs)
|
72 |
+
self._assert_xtickslabels_visibility(axes, expected)
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/test_frame_legend.py
ADDED
@@ -0,0 +1,272 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas.util._test_decorators as td
|
5 |
+
|
6 |
+
from pandas import (
|
7 |
+
DataFrame,
|
8 |
+
date_range,
|
9 |
+
)
|
10 |
+
from pandas.tests.plotting.common import (
|
11 |
+
_check_legend_labels,
|
12 |
+
_check_legend_marker,
|
13 |
+
_check_text_labels,
|
14 |
+
)
|
15 |
+
from pandas.util.version import Version
|
16 |
+
|
17 |
+
mpl = pytest.importorskip("matplotlib")
|
18 |
+
|
19 |
+
|
20 |
+
class TestFrameLegend:
|
21 |
+
@pytest.mark.xfail(
|
22 |
+
reason=(
|
23 |
+
"Open bug in matplotlib "
|
24 |
+
"https://github.com/matplotlib/matplotlib/issues/11357"
|
25 |
+
)
|
26 |
+
)
|
27 |
+
def test_mixed_yerr(self):
|
28 |
+
# https://github.com/pandas-dev/pandas/issues/39522
|
29 |
+
from matplotlib.collections import LineCollection
|
30 |
+
from matplotlib.lines import Line2D
|
31 |
+
|
32 |
+
df = DataFrame([{"x": 1, "a": 1, "b": 1}, {"x": 2, "a": 2, "b": 3}])
|
33 |
+
|
34 |
+
ax = df.plot("x", "a", c="orange", yerr=0.1, label="orange")
|
35 |
+
df.plot("x", "b", c="blue", yerr=None, ax=ax, label="blue")
|
36 |
+
|
37 |
+
legend = ax.get_legend()
|
38 |
+
if Version(mpl.__version__) < Version("3.7"):
|
39 |
+
result_handles = legend.legendHandles
|
40 |
+
else:
|
41 |
+
result_handles = legend.legend_handles
|
42 |
+
|
43 |
+
assert isinstance(result_handles[0], LineCollection)
|
44 |
+
assert isinstance(result_handles[1], Line2D)
|
45 |
+
|
46 |
+
def test_legend_false(self):
|
47 |
+
# https://github.com/pandas-dev/pandas/issues/40044
|
48 |
+
df = DataFrame({"a": [1, 1], "b": [2, 3]})
|
49 |
+
df2 = DataFrame({"d": [2.5, 2.5]})
|
50 |
+
|
51 |
+
ax = df.plot(legend=True, color={"a": "blue", "b": "green"}, secondary_y="b")
|
52 |
+
df2.plot(legend=True, color={"d": "red"}, ax=ax)
|
53 |
+
legend = ax.get_legend()
|
54 |
+
if Version(mpl.__version__) < Version("3.7"):
|
55 |
+
handles = legend.legendHandles
|
56 |
+
else:
|
57 |
+
handles = legend.legend_handles
|
58 |
+
result = [handle.get_color() for handle in handles]
|
59 |
+
expected = ["blue", "green", "red"]
|
60 |
+
assert result == expected
|
61 |
+
|
62 |
+
@pytest.mark.parametrize("kind", ["line", "bar", "barh", "kde", "area", "hist"])
|
63 |
+
def test_df_legend_labels(self, kind):
|
64 |
+
pytest.importorskip("scipy")
|
65 |
+
df = DataFrame(np.random.default_rng(2).random((3, 3)), columns=["a", "b", "c"])
|
66 |
+
df2 = DataFrame(
|
67 |
+
np.random.default_rng(2).random((3, 3)), columns=["d", "e", "f"]
|
68 |
+
)
|
69 |
+
df3 = DataFrame(
|
70 |
+
np.random.default_rng(2).random((3, 3)), columns=["g", "h", "i"]
|
71 |
+
)
|
72 |
+
df4 = DataFrame(
|
73 |
+
np.random.default_rng(2).random((3, 3)), columns=["j", "k", "l"]
|
74 |
+
)
|
75 |
+
|
76 |
+
ax = df.plot(kind=kind, legend=True)
|
77 |
+
_check_legend_labels(ax, labels=df.columns)
|
78 |
+
|
79 |
+
ax = df2.plot(kind=kind, legend=False, ax=ax)
|
80 |
+
_check_legend_labels(ax, labels=df.columns)
|
81 |
+
|
82 |
+
ax = df3.plot(kind=kind, legend=True, ax=ax)
|
83 |
+
_check_legend_labels(ax, labels=df.columns.union(df3.columns))
|
84 |
+
|
85 |
+
ax = df4.plot(kind=kind, legend="reverse", ax=ax)
|
86 |
+
expected = list(df.columns.union(df3.columns)) + list(reversed(df4.columns))
|
87 |
+
_check_legend_labels(ax, labels=expected)
|
88 |
+
|
89 |
+
def test_df_legend_labels_secondary_y(self):
|
90 |
+
pytest.importorskip("scipy")
|
91 |
+
df = DataFrame(np.random.default_rng(2).random((3, 3)), columns=["a", "b", "c"])
|
92 |
+
df2 = DataFrame(
|
93 |
+
np.random.default_rng(2).random((3, 3)), columns=["d", "e", "f"]
|
94 |
+
)
|
95 |
+
df3 = DataFrame(
|
96 |
+
np.random.default_rng(2).random((3, 3)), columns=["g", "h", "i"]
|
97 |
+
)
|
98 |
+
# Secondary Y
|
99 |
+
ax = df.plot(legend=True, secondary_y="b")
|
100 |
+
_check_legend_labels(ax, labels=["a", "b (right)", "c"])
|
101 |
+
ax = df2.plot(legend=False, ax=ax)
|
102 |
+
_check_legend_labels(ax, labels=["a", "b (right)", "c"])
|
103 |
+
ax = df3.plot(kind="bar", legend=True, secondary_y="h", ax=ax)
|
104 |
+
_check_legend_labels(ax, labels=["a", "b (right)", "c", "g", "h (right)", "i"])
|
105 |
+
|
106 |
+
def test_df_legend_labels_time_series(self):
|
107 |
+
# Time Series
|
108 |
+
pytest.importorskip("scipy")
|
109 |
+
ind = date_range("1/1/2014", periods=3)
|
110 |
+
df = DataFrame(
|
111 |
+
np.random.default_rng(2).standard_normal((3, 3)),
|
112 |
+
columns=["a", "b", "c"],
|
113 |
+
index=ind,
|
114 |
+
)
|
115 |
+
df2 = DataFrame(
|
116 |
+
np.random.default_rng(2).standard_normal((3, 3)),
|
117 |
+
columns=["d", "e", "f"],
|
118 |
+
index=ind,
|
119 |
+
)
|
120 |
+
df3 = DataFrame(
|
121 |
+
np.random.default_rng(2).standard_normal((3, 3)),
|
122 |
+
columns=["g", "h", "i"],
|
123 |
+
index=ind,
|
124 |
+
)
|
125 |
+
ax = df.plot(legend=True, secondary_y="b")
|
126 |
+
_check_legend_labels(ax, labels=["a", "b (right)", "c"])
|
127 |
+
ax = df2.plot(legend=False, ax=ax)
|
128 |
+
_check_legend_labels(ax, labels=["a", "b (right)", "c"])
|
129 |
+
ax = df3.plot(legend=True, ax=ax)
|
130 |
+
_check_legend_labels(ax, labels=["a", "b (right)", "c", "g", "h", "i"])
|
131 |
+
|
132 |
+
def test_df_legend_labels_time_series_scatter(self):
|
133 |
+
# Time Series
|
134 |
+
pytest.importorskip("scipy")
|
135 |
+
ind = date_range("1/1/2014", periods=3)
|
136 |
+
df = DataFrame(
|
137 |
+
np.random.default_rng(2).standard_normal((3, 3)),
|
138 |
+
columns=["a", "b", "c"],
|
139 |
+
index=ind,
|
140 |
+
)
|
141 |
+
df2 = DataFrame(
|
142 |
+
np.random.default_rng(2).standard_normal((3, 3)),
|
143 |
+
columns=["d", "e", "f"],
|
144 |
+
index=ind,
|
145 |
+
)
|
146 |
+
df3 = DataFrame(
|
147 |
+
np.random.default_rng(2).standard_normal((3, 3)),
|
148 |
+
columns=["g", "h", "i"],
|
149 |
+
index=ind,
|
150 |
+
)
|
151 |
+
# scatter
|
152 |
+
ax = df.plot.scatter(x="a", y="b", label="data1")
|
153 |
+
_check_legend_labels(ax, labels=["data1"])
|
154 |
+
ax = df2.plot.scatter(x="d", y="e", legend=False, label="data2", ax=ax)
|
155 |
+
_check_legend_labels(ax, labels=["data1"])
|
156 |
+
ax = df3.plot.scatter(x="g", y="h", label="data3", ax=ax)
|
157 |
+
_check_legend_labels(ax, labels=["data1", "data3"])
|
158 |
+
|
159 |
+
def test_df_legend_labels_time_series_no_mutate(self):
|
160 |
+
pytest.importorskip("scipy")
|
161 |
+
ind = date_range("1/1/2014", periods=3)
|
162 |
+
df = DataFrame(
|
163 |
+
np.random.default_rng(2).standard_normal((3, 3)),
|
164 |
+
columns=["a", "b", "c"],
|
165 |
+
index=ind,
|
166 |
+
)
|
167 |
+
# ensure label args pass through and
|
168 |
+
# index name does not mutate
|
169 |
+
# column names don't mutate
|
170 |
+
df5 = df.set_index("a")
|
171 |
+
ax = df5.plot(y="b")
|
172 |
+
_check_legend_labels(ax, labels=["b"])
|
173 |
+
ax = df5.plot(y="b", label="LABEL_b")
|
174 |
+
_check_legend_labels(ax, labels=["LABEL_b"])
|
175 |
+
_check_text_labels(ax.xaxis.get_label(), "a")
|
176 |
+
ax = df5.plot(y="c", label="LABEL_c", ax=ax)
|
177 |
+
_check_legend_labels(ax, labels=["LABEL_b", "LABEL_c"])
|
178 |
+
assert df5.columns.tolist() == ["b", "c"]
|
179 |
+
|
180 |
+
def test_missing_marker_multi_plots_on_same_ax(self):
|
181 |
+
# GH 18222
|
182 |
+
df = DataFrame(data=[[1, 1, 1, 1], [2, 2, 4, 8]], columns=["x", "r", "g", "b"])
|
183 |
+
_, ax = mpl.pyplot.subplots(nrows=1, ncols=3)
|
184 |
+
# Left plot
|
185 |
+
df.plot(x="x", y="r", linewidth=0, marker="o", color="r", ax=ax[0])
|
186 |
+
df.plot(x="x", y="g", linewidth=1, marker="x", color="g", ax=ax[0])
|
187 |
+
df.plot(x="x", y="b", linewidth=1, marker="o", color="b", ax=ax[0])
|
188 |
+
_check_legend_labels(ax[0], labels=["r", "g", "b"])
|
189 |
+
_check_legend_marker(ax[0], expected_markers=["o", "x", "o"])
|
190 |
+
# Center plot
|
191 |
+
df.plot(x="x", y="b", linewidth=1, marker="o", color="b", ax=ax[1])
|
192 |
+
df.plot(x="x", y="r", linewidth=0, marker="o", color="r", ax=ax[1])
|
193 |
+
df.plot(x="x", y="g", linewidth=1, marker="x", color="g", ax=ax[1])
|
194 |
+
_check_legend_labels(ax[1], labels=["b", "r", "g"])
|
195 |
+
_check_legend_marker(ax[1], expected_markers=["o", "o", "x"])
|
196 |
+
# Right plot
|
197 |
+
df.plot(x="x", y="g", linewidth=1, marker="x", color="g", ax=ax[2])
|
198 |
+
df.plot(x="x", y="b", linewidth=1, marker="o", color="b", ax=ax[2])
|
199 |
+
df.plot(x="x", y="r", linewidth=0, marker="o", color="r", ax=ax[2])
|
200 |
+
_check_legend_labels(ax[2], labels=["g", "b", "r"])
|
201 |
+
_check_legend_marker(ax[2], expected_markers=["x", "o", "o"])
|
202 |
+
|
203 |
+
def test_legend_name(self):
|
204 |
+
multi = DataFrame(
|
205 |
+
np.random.default_rng(2).standard_normal((4, 4)),
|
206 |
+
columns=[np.array(["a", "a", "b", "b"]), np.array(["x", "y", "x", "y"])],
|
207 |
+
)
|
208 |
+
multi.columns.names = ["group", "individual"]
|
209 |
+
|
210 |
+
ax = multi.plot()
|
211 |
+
leg_title = ax.legend_.get_title()
|
212 |
+
_check_text_labels(leg_title, "group,individual")
|
213 |
+
|
214 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
215 |
+
ax = df.plot(legend=True, ax=ax)
|
216 |
+
leg_title = ax.legend_.get_title()
|
217 |
+
_check_text_labels(leg_title, "group,individual")
|
218 |
+
|
219 |
+
df.columns.name = "new"
|
220 |
+
ax = df.plot(legend=False, ax=ax)
|
221 |
+
leg_title = ax.legend_.get_title()
|
222 |
+
_check_text_labels(leg_title, "group,individual")
|
223 |
+
|
224 |
+
ax = df.plot(legend=True, ax=ax)
|
225 |
+
leg_title = ax.legend_.get_title()
|
226 |
+
_check_text_labels(leg_title, "new")
|
227 |
+
|
228 |
+
@pytest.mark.parametrize(
|
229 |
+
"kind",
|
230 |
+
[
|
231 |
+
"line",
|
232 |
+
"bar",
|
233 |
+
"barh",
|
234 |
+
pytest.param("kde", marks=td.skip_if_no("scipy")),
|
235 |
+
"area",
|
236 |
+
"hist",
|
237 |
+
],
|
238 |
+
)
|
239 |
+
def test_no_legend(self, kind):
|
240 |
+
df = DataFrame(np.random.default_rng(2).random((3, 3)), columns=["a", "b", "c"])
|
241 |
+
ax = df.plot(kind=kind, legend=False)
|
242 |
+
_check_legend_labels(ax, visible=False)
|
243 |
+
|
244 |
+
def test_missing_markers_legend(self):
|
245 |
+
# 14958
|
246 |
+
df = DataFrame(
|
247 |
+
np.random.default_rng(2).standard_normal((8, 3)), columns=["A", "B", "C"]
|
248 |
+
)
|
249 |
+
ax = df.plot(y=["A"], marker="x", linestyle="solid")
|
250 |
+
df.plot(y=["B"], marker="o", linestyle="dotted", ax=ax)
|
251 |
+
df.plot(y=["C"], marker="<", linestyle="dotted", ax=ax)
|
252 |
+
|
253 |
+
_check_legend_labels(ax, labels=["A", "B", "C"])
|
254 |
+
_check_legend_marker(ax, expected_markers=["x", "o", "<"])
|
255 |
+
|
256 |
+
def test_missing_markers_legend_using_style(self):
|
257 |
+
# 14563
|
258 |
+
df = DataFrame(
|
259 |
+
{
|
260 |
+
"A": [1, 2, 3, 4, 5, 6],
|
261 |
+
"B": [2, 4, 1, 3, 2, 4],
|
262 |
+
"C": [3, 3, 2, 6, 4, 2],
|
263 |
+
"X": [1, 2, 3, 4, 5, 6],
|
264 |
+
}
|
265 |
+
)
|
266 |
+
|
267 |
+
_, ax = mpl.pyplot.subplots()
|
268 |
+
for kind in "ABC":
|
269 |
+
df.plot("X", kind, label=kind, ax=ax, style=".")
|
270 |
+
|
271 |
+
_check_legend_labels(ax, labels=["A", "B", "C"])
|
272 |
+
_check_legend_marker(ax, expected_markers=[".", ".", "."])
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/test_frame_subplots.py
ADDED
@@ -0,0 +1,752 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""" Test cases for DataFrame.plot """
|
2 |
+
|
3 |
+
import string
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
import pytest
|
7 |
+
|
8 |
+
from pandas.compat import is_platform_linux
|
9 |
+
from pandas.compat.numpy import np_version_gte1p24
|
10 |
+
|
11 |
+
import pandas as pd
|
12 |
+
from pandas import (
|
13 |
+
DataFrame,
|
14 |
+
Series,
|
15 |
+
date_range,
|
16 |
+
)
|
17 |
+
import pandas._testing as tm
|
18 |
+
from pandas.tests.plotting.common import (
|
19 |
+
_check_axes_shape,
|
20 |
+
_check_box_return_type,
|
21 |
+
_check_legend_labels,
|
22 |
+
_check_ticks_props,
|
23 |
+
_check_visible,
|
24 |
+
_flatten_visible,
|
25 |
+
)
|
26 |
+
|
27 |
+
from pandas.io.formats.printing import pprint_thing
|
28 |
+
|
29 |
+
mpl = pytest.importorskip("matplotlib")
|
30 |
+
plt = pytest.importorskip("matplotlib.pyplot")
|
31 |
+
|
32 |
+
|
33 |
+
class TestDataFramePlotsSubplots:
|
34 |
+
@pytest.mark.slow
|
35 |
+
@pytest.mark.parametrize("kind", ["bar", "barh", "line", "area"])
|
36 |
+
def test_subplots(self, kind):
|
37 |
+
df = DataFrame(
|
38 |
+
np.random.default_rng(2).random((10, 3)),
|
39 |
+
index=list(string.ascii_letters[:10]),
|
40 |
+
)
|
41 |
+
|
42 |
+
axes = df.plot(kind=kind, subplots=True, sharex=True, legend=True)
|
43 |
+
_check_axes_shape(axes, axes_num=3, layout=(3, 1))
|
44 |
+
assert axes.shape == (3,)
|
45 |
+
|
46 |
+
for ax, column in zip(axes, df.columns):
|
47 |
+
_check_legend_labels(ax, labels=[pprint_thing(column)])
|
48 |
+
|
49 |
+
for ax in axes[:-2]:
|
50 |
+
_check_visible(ax.xaxis) # xaxis must be visible for grid
|
51 |
+
_check_visible(ax.get_xticklabels(), visible=False)
|
52 |
+
if kind != "bar":
|
53 |
+
# change https://github.com/pandas-dev/pandas/issues/26714
|
54 |
+
_check_visible(ax.get_xticklabels(minor=True), visible=False)
|
55 |
+
_check_visible(ax.xaxis.get_label(), visible=False)
|
56 |
+
_check_visible(ax.get_yticklabels())
|
57 |
+
|
58 |
+
_check_visible(axes[-1].xaxis)
|
59 |
+
_check_visible(axes[-1].get_xticklabels())
|
60 |
+
_check_visible(axes[-1].get_xticklabels(minor=True))
|
61 |
+
_check_visible(axes[-1].xaxis.get_label())
|
62 |
+
_check_visible(axes[-1].get_yticklabels())
|
63 |
+
|
64 |
+
@pytest.mark.slow
|
65 |
+
@pytest.mark.parametrize("kind", ["bar", "barh", "line", "area"])
|
66 |
+
def test_subplots_no_share_x(self, kind):
|
67 |
+
df = DataFrame(
|
68 |
+
np.random.default_rng(2).random((10, 3)),
|
69 |
+
index=list(string.ascii_letters[:10]),
|
70 |
+
)
|
71 |
+
axes = df.plot(kind=kind, subplots=True, sharex=False)
|
72 |
+
for ax in axes:
|
73 |
+
_check_visible(ax.xaxis)
|
74 |
+
_check_visible(ax.get_xticklabels())
|
75 |
+
_check_visible(ax.get_xticklabels(minor=True))
|
76 |
+
_check_visible(ax.xaxis.get_label())
|
77 |
+
_check_visible(ax.get_yticklabels())
|
78 |
+
|
79 |
+
@pytest.mark.slow
|
80 |
+
@pytest.mark.parametrize("kind", ["bar", "barh", "line", "area"])
|
81 |
+
def test_subplots_no_legend(self, kind):
|
82 |
+
df = DataFrame(
|
83 |
+
np.random.default_rng(2).random((10, 3)),
|
84 |
+
index=list(string.ascii_letters[:10]),
|
85 |
+
)
|
86 |
+
axes = df.plot(kind=kind, subplots=True, legend=False)
|
87 |
+
for ax in axes:
|
88 |
+
assert ax.get_legend() is None
|
89 |
+
|
90 |
+
@pytest.mark.parametrize("kind", ["line", "area"])
|
91 |
+
def test_subplots_timeseries(self, kind):
|
92 |
+
idx = date_range(start="2014-07-01", freq="ME", periods=10)
|
93 |
+
df = DataFrame(np.random.default_rng(2).random((10, 3)), index=idx)
|
94 |
+
|
95 |
+
axes = df.plot(kind=kind, subplots=True, sharex=True)
|
96 |
+
_check_axes_shape(axes, axes_num=3, layout=(3, 1))
|
97 |
+
|
98 |
+
for ax in axes[:-2]:
|
99 |
+
# GH 7801
|
100 |
+
_check_visible(ax.xaxis) # xaxis must be visible for grid
|
101 |
+
_check_visible(ax.get_xticklabels(), visible=False)
|
102 |
+
_check_visible(ax.get_xticklabels(minor=True), visible=False)
|
103 |
+
_check_visible(ax.xaxis.get_label(), visible=False)
|
104 |
+
_check_visible(ax.get_yticklabels())
|
105 |
+
|
106 |
+
_check_visible(axes[-1].xaxis)
|
107 |
+
_check_visible(axes[-1].get_xticklabels())
|
108 |
+
_check_visible(axes[-1].get_xticklabels(minor=True))
|
109 |
+
_check_visible(axes[-1].xaxis.get_label())
|
110 |
+
_check_visible(axes[-1].get_yticklabels())
|
111 |
+
_check_ticks_props(axes, xrot=0)
|
112 |
+
|
113 |
+
@pytest.mark.parametrize("kind", ["line", "area"])
|
114 |
+
def test_subplots_timeseries_rot(self, kind):
|
115 |
+
idx = date_range(start="2014-07-01", freq="ME", periods=10)
|
116 |
+
df = DataFrame(np.random.default_rng(2).random((10, 3)), index=idx)
|
117 |
+
axes = df.plot(kind=kind, subplots=True, sharex=False, rot=45, fontsize=7)
|
118 |
+
for ax in axes:
|
119 |
+
_check_visible(ax.xaxis)
|
120 |
+
_check_visible(ax.get_xticklabels())
|
121 |
+
_check_visible(ax.get_xticklabels(minor=True))
|
122 |
+
_check_visible(ax.xaxis.get_label())
|
123 |
+
_check_visible(ax.get_yticklabels())
|
124 |
+
_check_ticks_props(ax, xlabelsize=7, xrot=45, ylabelsize=7)
|
125 |
+
|
126 |
+
@pytest.mark.parametrize(
|
127 |
+
"col", ["numeric", "timedelta", "datetime_no_tz", "datetime_all_tz"]
|
128 |
+
)
|
129 |
+
def test_subplots_timeseries_y_axis(self, col):
|
130 |
+
# GH16953
|
131 |
+
data = {
|
132 |
+
"numeric": np.array([1, 2, 5]),
|
133 |
+
"timedelta": [
|
134 |
+
pd.Timedelta(-10, unit="s"),
|
135 |
+
pd.Timedelta(10, unit="m"),
|
136 |
+
pd.Timedelta(10, unit="h"),
|
137 |
+
],
|
138 |
+
"datetime_no_tz": [
|
139 |
+
pd.to_datetime("2017-08-01 00:00:00"),
|
140 |
+
pd.to_datetime("2017-08-01 02:00:00"),
|
141 |
+
pd.to_datetime("2017-08-02 00:00:00"),
|
142 |
+
],
|
143 |
+
"datetime_all_tz": [
|
144 |
+
pd.to_datetime("2017-08-01 00:00:00", utc=True),
|
145 |
+
pd.to_datetime("2017-08-01 02:00:00", utc=True),
|
146 |
+
pd.to_datetime("2017-08-02 00:00:00", utc=True),
|
147 |
+
],
|
148 |
+
"text": ["This", "should", "fail"],
|
149 |
+
}
|
150 |
+
testdata = DataFrame(data)
|
151 |
+
|
152 |
+
ax = testdata.plot(y=col)
|
153 |
+
result = ax.get_lines()[0].get_data()[1]
|
154 |
+
expected = testdata[col].values
|
155 |
+
assert (result == expected).all()
|
156 |
+
|
157 |
+
def test_subplots_timeseries_y_text_error(self):
|
158 |
+
# GH16953
|
159 |
+
data = {
|
160 |
+
"numeric": np.array([1, 2, 5]),
|
161 |
+
"text": ["This", "should", "fail"],
|
162 |
+
}
|
163 |
+
testdata = DataFrame(data)
|
164 |
+
msg = "no numeric data to plot"
|
165 |
+
with pytest.raises(TypeError, match=msg):
|
166 |
+
testdata.plot(y="text")
|
167 |
+
|
168 |
+
@pytest.mark.xfail(reason="not support for period, categorical, datetime_mixed_tz")
|
169 |
+
def test_subplots_timeseries_y_axis_not_supported(self):
|
170 |
+
"""
|
171 |
+
This test will fail for:
|
172 |
+
period:
|
173 |
+
since period isn't yet implemented in ``select_dtypes``
|
174 |
+
and because it will need a custom value converter +
|
175 |
+
tick formatter (as was done for x-axis plots)
|
176 |
+
|
177 |
+
categorical:
|
178 |
+
because it will need a custom value converter +
|
179 |
+
tick formatter (also doesn't work for x-axis, as of now)
|
180 |
+
|
181 |
+
datetime_mixed_tz:
|
182 |
+
because of the way how pandas handles ``Series`` of
|
183 |
+
``datetime`` objects with different timezone,
|
184 |
+
generally converting ``datetime`` objects in a tz-aware
|
185 |
+
form could help with this problem
|
186 |
+
"""
|
187 |
+
data = {
|
188 |
+
"numeric": np.array([1, 2, 5]),
|
189 |
+
"period": [
|
190 |
+
pd.Period("2017-08-01 00:00:00", freq="H"),
|
191 |
+
pd.Period("2017-08-01 02:00", freq="H"),
|
192 |
+
pd.Period("2017-08-02 00:00:00", freq="H"),
|
193 |
+
],
|
194 |
+
"categorical": pd.Categorical(
|
195 |
+
["c", "b", "a"], categories=["a", "b", "c"], ordered=False
|
196 |
+
),
|
197 |
+
"datetime_mixed_tz": [
|
198 |
+
pd.to_datetime("2017-08-01 00:00:00", utc=True),
|
199 |
+
pd.to_datetime("2017-08-01 02:00:00"),
|
200 |
+
pd.to_datetime("2017-08-02 00:00:00"),
|
201 |
+
],
|
202 |
+
}
|
203 |
+
testdata = DataFrame(data)
|
204 |
+
ax_period = testdata.plot(x="numeric", y="period")
|
205 |
+
assert (
|
206 |
+
ax_period.get_lines()[0].get_data()[1] == testdata["period"].values
|
207 |
+
).all()
|
208 |
+
ax_categorical = testdata.plot(x="numeric", y="categorical")
|
209 |
+
assert (
|
210 |
+
ax_categorical.get_lines()[0].get_data()[1]
|
211 |
+
== testdata["categorical"].values
|
212 |
+
).all()
|
213 |
+
ax_datetime_mixed_tz = testdata.plot(x="numeric", y="datetime_mixed_tz")
|
214 |
+
assert (
|
215 |
+
ax_datetime_mixed_tz.get_lines()[0].get_data()[1]
|
216 |
+
== testdata["datetime_mixed_tz"].values
|
217 |
+
).all()
|
218 |
+
|
219 |
+
@pytest.mark.parametrize(
|
220 |
+
"layout, exp_layout",
|
221 |
+
[
|
222 |
+
[(2, 2), (2, 2)],
|
223 |
+
[(-1, 2), (2, 2)],
|
224 |
+
[(2, -1), (2, 2)],
|
225 |
+
[(1, 4), (1, 4)],
|
226 |
+
[(-1, 4), (1, 4)],
|
227 |
+
[(4, -1), (4, 1)],
|
228 |
+
],
|
229 |
+
)
|
230 |
+
def test_subplots_layout_multi_column(self, layout, exp_layout):
|
231 |
+
# GH 6667
|
232 |
+
df = DataFrame(
|
233 |
+
np.random.default_rng(2).random((10, 3)),
|
234 |
+
index=list(string.ascii_letters[:10]),
|
235 |
+
)
|
236 |
+
|
237 |
+
axes = df.plot(subplots=True, layout=layout)
|
238 |
+
_check_axes_shape(axes, axes_num=3, layout=exp_layout)
|
239 |
+
assert axes.shape == exp_layout
|
240 |
+
|
241 |
+
def test_subplots_layout_multi_column_error(self):
|
242 |
+
# GH 6667
|
243 |
+
df = DataFrame(
|
244 |
+
np.random.default_rng(2).random((10, 3)),
|
245 |
+
index=list(string.ascii_letters[:10]),
|
246 |
+
)
|
247 |
+
msg = "Layout of 1x1 must be larger than required size 3"
|
248 |
+
|
249 |
+
with pytest.raises(ValueError, match=msg):
|
250 |
+
df.plot(subplots=True, layout=(1, 1))
|
251 |
+
|
252 |
+
msg = "At least one dimension of layout must be positive"
|
253 |
+
with pytest.raises(ValueError, match=msg):
|
254 |
+
df.plot(subplots=True, layout=(-1, -1))
|
255 |
+
|
256 |
+
@pytest.mark.parametrize(
|
257 |
+
"kwargs, expected_axes_num, expected_layout, expected_shape",
|
258 |
+
[
|
259 |
+
({}, 1, (1, 1), (1,)),
|
260 |
+
({"layout": (3, 3)}, 1, (3, 3), (3, 3)),
|
261 |
+
],
|
262 |
+
)
|
263 |
+
def test_subplots_layout_single_column(
|
264 |
+
self, kwargs, expected_axes_num, expected_layout, expected_shape
|
265 |
+
):
|
266 |
+
# GH 6667
|
267 |
+
df = DataFrame(
|
268 |
+
np.random.default_rng(2).random((10, 1)),
|
269 |
+
index=list(string.ascii_letters[:10]),
|
270 |
+
)
|
271 |
+
axes = df.plot(subplots=True, **kwargs)
|
272 |
+
_check_axes_shape(
|
273 |
+
axes,
|
274 |
+
axes_num=expected_axes_num,
|
275 |
+
layout=expected_layout,
|
276 |
+
)
|
277 |
+
assert axes.shape == expected_shape
|
278 |
+
|
279 |
+
@pytest.mark.slow
|
280 |
+
@pytest.mark.parametrize("idx", [range(5), date_range("1/1/2000", periods=5)])
|
281 |
+
def test_subplots_warnings(self, idx):
|
282 |
+
# GH 9464
|
283 |
+
with tm.assert_produces_warning(None):
|
284 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 4)), index=idx)
|
285 |
+
df.plot(subplots=True, layout=(3, 2))
|
286 |
+
|
287 |
+
def test_subplots_multiple_axes(self):
|
288 |
+
# GH 5353, 6970, GH 7069
|
289 |
+
fig, axes = mpl.pyplot.subplots(2, 3)
|
290 |
+
df = DataFrame(
|
291 |
+
np.random.default_rng(2).random((10, 3)),
|
292 |
+
index=list(string.ascii_letters[:10]),
|
293 |
+
)
|
294 |
+
|
295 |
+
returned = df.plot(subplots=True, ax=axes[0], sharex=False, sharey=False)
|
296 |
+
_check_axes_shape(returned, axes_num=3, layout=(1, 3))
|
297 |
+
assert returned.shape == (3,)
|
298 |
+
assert returned[0].figure is fig
|
299 |
+
# draw on second row
|
300 |
+
returned = df.plot(subplots=True, ax=axes[1], sharex=False, sharey=False)
|
301 |
+
_check_axes_shape(returned, axes_num=3, layout=(1, 3))
|
302 |
+
assert returned.shape == (3,)
|
303 |
+
assert returned[0].figure is fig
|
304 |
+
_check_axes_shape(axes, axes_num=6, layout=(2, 3))
|
305 |
+
|
306 |
+
def test_subplots_multiple_axes_error(self):
|
307 |
+
# GH 5353, 6970, GH 7069
|
308 |
+
df = DataFrame(
|
309 |
+
np.random.default_rng(2).random((10, 3)),
|
310 |
+
index=list(string.ascii_letters[:10]),
|
311 |
+
)
|
312 |
+
msg = "The number of passed axes must be 3, the same as the output plot"
|
313 |
+
_, axes = mpl.pyplot.subplots(2, 3)
|
314 |
+
|
315 |
+
with pytest.raises(ValueError, match=msg):
|
316 |
+
# pass different number of axes from required
|
317 |
+
df.plot(subplots=True, ax=axes)
|
318 |
+
|
319 |
+
@pytest.mark.parametrize(
|
320 |
+
"layout, exp_layout",
|
321 |
+
[
|
322 |
+
[(2, 1), (2, 2)],
|
323 |
+
[(2, -1), (2, 2)],
|
324 |
+
[(-1, 2), (2, 2)],
|
325 |
+
],
|
326 |
+
)
|
327 |
+
def test_subplots_multiple_axes_2_dim(self, layout, exp_layout):
|
328 |
+
# GH 5353, 6970, GH 7069
|
329 |
+
# pass 2-dim axes and invalid layout
|
330 |
+
# invalid lauout should not affect to input and return value
|
331 |
+
# (show warning is tested in
|
332 |
+
# TestDataFrameGroupByPlots.test_grouped_box_multiple_axes
|
333 |
+
_, axes = mpl.pyplot.subplots(2, 2)
|
334 |
+
df = DataFrame(
|
335 |
+
np.random.default_rng(2).random((10, 4)),
|
336 |
+
index=list(string.ascii_letters[:10]),
|
337 |
+
)
|
338 |
+
with tm.assert_produces_warning(UserWarning):
|
339 |
+
returned = df.plot(
|
340 |
+
subplots=True, ax=axes, layout=layout, sharex=False, sharey=False
|
341 |
+
)
|
342 |
+
_check_axes_shape(returned, axes_num=4, layout=exp_layout)
|
343 |
+
assert returned.shape == (4,)
|
344 |
+
|
345 |
+
def test_subplots_multiple_axes_single_col(self):
|
346 |
+
# GH 5353, 6970, GH 7069
|
347 |
+
# single column
|
348 |
+
_, axes = mpl.pyplot.subplots(1, 1)
|
349 |
+
df = DataFrame(
|
350 |
+
np.random.default_rng(2).random((10, 1)),
|
351 |
+
index=list(string.ascii_letters[:10]),
|
352 |
+
)
|
353 |
+
|
354 |
+
axes = df.plot(subplots=True, ax=[axes], sharex=False, sharey=False)
|
355 |
+
_check_axes_shape(axes, axes_num=1, layout=(1, 1))
|
356 |
+
assert axes.shape == (1,)
|
357 |
+
|
358 |
+
def test_subplots_ts_share_axes(self):
|
359 |
+
# GH 3964
|
360 |
+
_, axes = mpl.pyplot.subplots(3, 3, sharex=True, sharey=True)
|
361 |
+
mpl.pyplot.subplots_adjust(left=0.05, right=0.95, hspace=0.3, wspace=0.3)
|
362 |
+
df = DataFrame(
|
363 |
+
np.random.default_rng(2).standard_normal((10, 9)),
|
364 |
+
index=date_range(start="2014-07-01", freq="ME", periods=10),
|
365 |
+
)
|
366 |
+
for i, ax in enumerate(axes.ravel()):
|
367 |
+
df[i].plot(ax=ax, fontsize=5)
|
368 |
+
|
369 |
+
# Rows other than bottom should not be visible
|
370 |
+
for ax in axes[0:-1].ravel():
|
371 |
+
_check_visible(ax.get_xticklabels(), visible=False)
|
372 |
+
|
373 |
+
# Bottom row should be visible
|
374 |
+
for ax in axes[-1].ravel():
|
375 |
+
_check_visible(ax.get_xticklabels(), visible=True)
|
376 |
+
|
377 |
+
# First column should be visible
|
378 |
+
for ax in axes[[0, 1, 2], [0]].ravel():
|
379 |
+
_check_visible(ax.get_yticklabels(), visible=True)
|
380 |
+
|
381 |
+
# Other columns should not be visible
|
382 |
+
for ax in axes[[0, 1, 2], [1]].ravel():
|
383 |
+
_check_visible(ax.get_yticklabels(), visible=False)
|
384 |
+
for ax in axes[[0, 1, 2], [2]].ravel():
|
385 |
+
_check_visible(ax.get_yticklabels(), visible=False)
|
386 |
+
|
387 |
+
def test_subplots_sharex_axes_existing_axes(self):
|
388 |
+
# GH 9158
|
389 |
+
d = {"A": [1.0, 2.0, 3.0, 4.0], "B": [4.0, 3.0, 2.0, 1.0], "C": [5, 1, 3, 4]}
|
390 |
+
df = DataFrame(d, index=date_range("2014 10 11", "2014 10 14"))
|
391 |
+
|
392 |
+
axes = df[["A", "B"]].plot(subplots=True)
|
393 |
+
df["C"].plot(ax=axes[0], secondary_y=True)
|
394 |
+
|
395 |
+
_check_visible(axes[0].get_xticklabels(), visible=False)
|
396 |
+
_check_visible(axes[1].get_xticklabels(), visible=True)
|
397 |
+
for ax in axes.ravel():
|
398 |
+
_check_visible(ax.get_yticklabels(), visible=True)
|
399 |
+
|
400 |
+
def test_subplots_dup_columns(self):
|
401 |
+
# GH 10962
|
402 |
+
df = DataFrame(np.random.default_rng(2).random((5, 5)), columns=list("aaaaa"))
|
403 |
+
axes = df.plot(subplots=True)
|
404 |
+
for ax in axes:
|
405 |
+
_check_legend_labels(ax, labels=["a"])
|
406 |
+
assert len(ax.lines) == 1
|
407 |
+
|
408 |
+
def test_subplots_dup_columns_secondary_y(self):
|
409 |
+
# GH 10962
|
410 |
+
df = DataFrame(np.random.default_rng(2).random((5, 5)), columns=list("aaaaa"))
|
411 |
+
axes = df.plot(subplots=True, secondary_y="a")
|
412 |
+
for ax in axes:
|
413 |
+
# (right) is only attached when subplots=False
|
414 |
+
_check_legend_labels(ax, labels=["a"])
|
415 |
+
assert len(ax.lines) == 1
|
416 |
+
|
417 |
+
def test_subplots_dup_columns_secondary_y_no_subplot(self):
|
418 |
+
# GH 10962
|
419 |
+
df = DataFrame(np.random.default_rng(2).random((5, 5)), columns=list("aaaaa"))
|
420 |
+
ax = df.plot(secondary_y="a")
|
421 |
+
_check_legend_labels(ax, labels=["a (right)"] * 5)
|
422 |
+
assert len(ax.lines) == 0
|
423 |
+
assert len(ax.right_ax.lines) == 5
|
424 |
+
|
425 |
+
@pytest.mark.xfail(
|
426 |
+
np_version_gte1p24 and is_platform_linux(),
|
427 |
+
reason="Weird rounding problems",
|
428 |
+
strict=False,
|
429 |
+
)
|
430 |
+
def test_bar_log_no_subplots(self):
|
431 |
+
# GH3254, GH3298 matplotlib/matplotlib#1882, #1892
|
432 |
+
# regressions in 1.2.1
|
433 |
+
expected = np.array([0.1, 1.0, 10.0, 100])
|
434 |
+
|
435 |
+
# no subplots
|
436 |
+
df = DataFrame({"A": [3] * 5, "B": list(range(1, 6))}, index=range(5))
|
437 |
+
ax = df.plot.bar(grid=True, log=True)
|
438 |
+
tm.assert_numpy_array_equal(ax.yaxis.get_ticklocs(), expected)
|
439 |
+
|
440 |
+
@pytest.mark.xfail(
|
441 |
+
np_version_gte1p24 and is_platform_linux(),
|
442 |
+
reason="Weird rounding problems",
|
443 |
+
strict=False,
|
444 |
+
)
|
445 |
+
def test_bar_log_subplots(self):
|
446 |
+
expected = np.array([0.1, 1.0, 10.0, 100.0, 1000.0, 1e4])
|
447 |
+
|
448 |
+
ax = DataFrame([Series([200, 300]), Series([300, 500])]).plot.bar(
|
449 |
+
log=True, subplots=True
|
450 |
+
)
|
451 |
+
|
452 |
+
tm.assert_numpy_array_equal(ax[0].yaxis.get_ticklocs(), expected)
|
453 |
+
tm.assert_numpy_array_equal(ax[1].yaxis.get_ticklocs(), expected)
|
454 |
+
|
455 |
+
def test_boxplot_subplots_return_type_default(self, hist_df):
|
456 |
+
df = hist_df
|
457 |
+
|
458 |
+
# normal style: return_type=None
|
459 |
+
result = df.plot.box(subplots=True)
|
460 |
+
assert isinstance(result, Series)
|
461 |
+
_check_box_return_type(
|
462 |
+
result, None, expected_keys=["height", "weight", "category"]
|
463 |
+
)
|
464 |
+
|
465 |
+
@pytest.mark.parametrize("rt", ["dict", "axes", "both"])
|
466 |
+
def test_boxplot_subplots_return_type(self, hist_df, rt):
|
467 |
+
df = hist_df
|
468 |
+
returned = df.plot.box(return_type=rt, subplots=True)
|
469 |
+
_check_box_return_type(
|
470 |
+
returned,
|
471 |
+
rt,
|
472 |
+
expected_keys=["height", "weight", "category"],
|
473 |
+
check_ax_title=False,
|
474 |
+
)
|
475 |
+
|
476 |
+
def test_df_subplots_patterns_minorticks(self):
|
477 |
+
# GH 10657
|
478 |
+
df = DataFrame(
|
479 |
+
np.random.default_rng(2).standard_normal((10, 2)),
|
480 |
+
index=date_range("1/1/2000", periods=10),
|
481 |
+
columns=list("AB"),
|
482 |
+
)
|
483 |
+
|
484 |
+
# shared subplots
|
485 |
+
_, axes = plt.subplots(2, 1, sharex=True)
|
486 |
+
axes = df.plot(subplots=True, ax=axes)
|
487 |
+
for ax in axes:
|
488 |
+
assert len(ax.lines) == 1
|
489 |
+
_check_visible(ax.get_yticklabels(), visible=True)
|
490 |
+
# xaxis of 1st ax must be hidden
|
491 |
+
_check_visible(axes[0].get_xticklabels(), visible=False)
|
492 |
+
_check_visible(axes[0].get_xticklabels(minor=True), visible=False)
|
493 |
+
_check_visible(axes[1].get_xticklabels(), visible=True)
|
494 |
+
_check_visible(axes[1].get_xticklabels(minor=True), visible=True)
|
495 |
+
|
496 |
+
def test_df_subplots_patterns_minorticks_1st_ax_hidden(self):
|
497 |
+
# GH 10657
|
498 |
+
df = DataFrame(
|
499 |
+
np.random.default_rng(2).standard_normal((10, 2)),
|
500 |
+
index=date_range("1/1/2000", periods=10),
|
501 |
+
columns=list("AB"),
|
502 |
+
)
|
503 |
+
_, axes = plt.subplots(2, 1)
|
504 |
+
with tm.assert_produces_warning(UserWarning):
|
505 |
+
axes = df.plot(subplots=True, ax=axes, sharex=True)
|
506 |
+
for ax in axes:
|
507 |
+
assert len(ax.lines) == 1
|
508 |
+
_check_visible(ax.get_yticklabels(), visible=True)
|
509 |
+
# xaxis of 1st ax must be hidden
|
510 |
+
_check_visible(axes[0].get_xticklabels(), visible=False)
|
511 |
+
_check_visible(axes[0].get_xticklabels(minor=True), visible=False)
|
512 |
+
_check_visible(axes[1].get_xticklabels(), visible=True)
|
513 |
+
_check_visible(axes[1].get_xticklabels(minor=True), visible=True)
|
514 |
+
|
515 |
+
def test_df_subplots_patterns_minorticks_not_shared(self):
|
516 |
+
# GH 10657
|
517 |
+
df = DataFrame(
|
518 |
+
np.random.default_rng(2).standard_normal((10, 2)),
|
519 |
+
index=date_range("1/1/2000", periods=10),
|
520 |
+
columns=list("AB"),
|
521 |
+
)
|
522 |
+
# not shared
|
523 |
+
_, axes = plt.subplots(2, 1)
|
524 |
+
axes = df.plot(subplots=True, ax=axes)
|
525 |
+
for ax in axes:
|
526 |
+
assert len(ax.lines) == 1
|
527 |
+
_check_visible(ax.get_yticklabels(), visible=True)
|
528 |
+
_check_visible(ax.get_xticklabels(), visible=True)
|
529 |
+
_check_visible(ax.get_xticklabels(minor=True), visible=True)
|
530 |
+
|
531 |
+
def test_subplots_sharex_false(self):
|
532 |
+
# test when sharex is set to False, two plots should have different
|
533 |
+
# labels, GH 25160
|
534 |
+
df = DataFrame(np.random.default_rng(2).random((10, 2)))
|
535 |
+
df.iloc[5:, 1] = np.nan
|
536 |
+
df.iloc[:5, 0] = np.nan
|
537 |
+
|
538 |
+
_, axs = mpl.pyplot.subplots(2, 1)
|
539 |
+
df.plot.line(ax=axs, subplots=True, sharex=False)
|
540 |
+
|
541 |
+
expected_ax1 = np.arange(4.5, 10, 0.5)
|
542 |
+
expected_ax2 = np.arange(-0.5, 5, 0.5)
|
543 |
+
|
544 |
+
tm.assert_numpy_array_equal(axs[0].get_xticks(), expected_ax1)
|
545 |
+
tm.assert_numpy_array_equal(axs[1].get_xticks(), expected_ax2)
|
546 |
+
|
547 |
+
def test_subplots_constrained_layout(self):
|
548 |
+
# GH 25261
|
549 |
+
idx = date_range(start="now", periods=10)
|
550 |
+
df = DataFrame(np.random.default_rng(2).random((10, 3)), index=idx)
|
551 |
+
kwargs = {}
|
552 |
+
if hasattr(mpl.pyplot.Figure, "get_constrained_layout"):
|
553 |
+
kwargs["constrained_layout"] = True
|
554 |
+
_, axes = mpl.pyplot.subplots(2, **kwargs)
|
555 |
+
with tm.assert_produces_warning(None):
|
556 |
+
df.plot(ax=axes[0])
|
557 |
+
with tm.ensure_clean(return_filelike=True) as path:
|
558 |
+
mpl.pyplot.savefig(path)
|
559 |
+
|
560 |
+
@pytest.mark.parametrize(
|
561 |
+
"index_name, old_label, new_label",
|
562 |
+
[
|
563 |
+
(None, "", "new"),
|
564 |
+
("old", "old", "new"),
|
565 |
+
(None, "", ""),
|
566 |
+
(None, "", 1),
|
567 |
+
(None, "", [1, 2]),
|
568 |
+
],
|
569 |
+
)
|
570 |
+
@pytest.mark.parametrize("kind", ["line", "area", "bar"])
|
571 |
+
def test_xlabel_ylabel_dataframe_subplots(
|
572 |
+
self, kind, index_name, old_label, new_label
|
573 |
+
):
|
574 |
+
# GH 9093
|
575 |
+
df = DataFrame([[1, 2], [2, 5]], columns=["Type A", "Type B"])
|
576 |
+
df.index.name = index_name
|
577 |
+
|
578 |
+
# default is the ylabel is not shown and xlabel is index name
|
579 |
+
axes = df.plot(kind=kind, subplots=True)
|
580 |
+
assert all(ax.get_ylabel() == "" for ax in axes)
|
581 |
+
assert all(ax.get_xlabel() == old_label for ax in axes)
|
582 |
+
|
583 |
+
# old xlabel will be overridden and assigned ylabel will be used as ylabel
|
584 |
+
axes = df.plot(kind=kind, ylabel=new_label, xlabel=new_label, subplots=True)
|
585 |
+
assert all(ax.get_ylabel() == str(new_label) for ax in axes)
|
586 |
+
assert all(ax.get_xlabel() == str(new_label) for ax in axes)
|
587 |
+
|
588 |
+
@pytest.mark.parametrize(
|
589 |
+
"kwargs",
|
590 |
+
[
|
591 |
+
# stacked center
|
592 |
+
{"kind": "bar", "stacked": True},
|
593 |
+
{"kind": "bar", "stacked": True, "width": 0.9},
|
594 |
+
{"kind": "barh", "stacked": True},
|
595 |
+
{"kind": "barh", "stacked": True, "width": 0.9},
|
596 |
+
# center
|
597 |
+
{"kind": "bar", "stacked": False},
|
598 |
+
{"kind": "bar", "stacked": False, "width": 0.9},
|
599 |
+
{"kind": "barh", "stacked": False},
|
600 |
+
{"kind": "barh", "stacked": False, "width": 0.9},
|
601 |
+
# subplots center
|
602 |
+
{"kind": "bar", "subplots": True},
|
603 |
+
{"kind": "bar", "subplots": True, "width": 0.9},
|
604 |
+
{"kind": "barh", "subplots": True},
|
605 |
+
{"kind": "barh", "subplots": True, "width": 0.9},
|
606 |
+
# align edge
|
607 |
+
{"kind": "bar", "stacked": True, "align": "edge"},
|
608 |
+
{"kind": "bar", "stacked": True, "width": 0.9, "align": "edge"},
|
609 |
+
{"kind": "barh", "stacked": True, "align": "edge"},
|
610 |
+
{"kind": "barh", "stacked": True, "width": 0.9, "align": "edge"},
|
611 |
+
{"kind": "bar", "stacked": False, "align": "edge"},
|
612 |
+
{"kind": "bar", "stacked": False, "width": 0.9, "align": "edge"},
|
613 |
+
{"kind": "barh", "stacked": False, "align": "edge"},
|
614 |
+
{"kind": "barh", "stacked": False, "width": 0.9, "align": "edge"},
|
615 |
+
{"kind": "bar", "subplots": True, "align": "edge"},
|
616 |
+
{"kind": "bar", "subplots": True, "width": 0.9, "align": "edge"},
|
617 |
+
{"kind": "barh", "subplots": True, "align": "edge"},
|
618 |
+
{"kind": "barh", "subplots": True, "width": 0.9, "align": "edge"},
|
619 |
+
],
|
620 |
+
)
|
621 |
+
def test_bar_align_multiple_columns(self, kwargs):
|
622 |
+
# GH2157
|
623 |
+
df = DataFrame({"A": [3] * 5, "B": list(range(5))}, index=range(5))
|
624 |
+
self._check_bar_alignment(df, **kwargs)
|
625 |
+
|
626 |
+
@pytest.mark.parametrize(
|
627 |
+
"kwargs",
|
628 |
+
[
|
629 |
+
{"kind": "bar", "stacked": False},
|
630 |
+
{"kind": "bar", "stacked": True},
|
631 |
+
{"kind": "barh", "stacked": False},
|
632 |
+
{"kind": "barh", "stacked": True},
|
633 |
+
{"kind": "bar", "subplots": True},
|
634 |
+
{"kind": "barh", "subplots": True},
|
635 |
+
],
|
636 |
+
)
|
637 |
+
def test_bar_align_single_column(self, kwargs):
|
638 |
+
df = DataFrame(np.random.default_rng(2).standard_normal(5))
|
639 |
+
self._check_bar_alignment(df, **kwargs)
|
640 |
+
|
641 |
+
@pytest.mark.parametrize(
|
642 |
+
"kwargs",
|
643 |
+
[
|
644 |
+
{"kind": "bar", "stacked": False},
|
645 |
+
{"kind": "bar", "stacked": True},
|
646 |
+
{"kind": "barh", "stacked": False},
|
647 |
+
{"kind": "barh", "stacked": True},
|
648 |
+
{"kind": "bar", "subplots": True},
|
649 |
+
{"kind": "barh", "subplots": True},
|
650 |
+
],
|
651 |
+
)
|
652 |
+
def test_bar_barwidth_position(self, kwargs):
|
653 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
654 |
+
self._check_bar_alignment(df, width=0.9, position=0.2, **kwargs)
|
655 |
+
|
656 |
+
@pytest.mark.parametrize("w", [1, 1.0])
|
657 |
+
def test_bar_barwidth_position_int(self, w):
|
658 |
+
# GH 12979
|
659 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
660 |
+
ax = df.plot.bar(stacked=True, width=w)
|
661 |
+
ticks = ax.xaxis.get_ticklocs()
|
662 |
+
tm.assert_numpy_array_equal(ticks, np.array([0, 1, 2, 3, 4]))
|
663 |
+
assert ax.get_xlim() == (-0.75, 4.75)
|
664 |
+
# check left-edge of bars
|
665 |
+
assert ax.patches[0].get_x() == -0.5
|
666 |
+
assert ax.patches[-1].get_x() == 3.5
|
667 |
+
|
668 |
+
@pytest.mark.parametrize(
|
669 |
+
"kind, kwargs",
|
670 |
+
[
|
671 |
+
["bar", {"stacked": True}],
|
672 |
+
["barh", {"stacked": False}],
|
673 |
+
["barh", {"stacked": True}],
|
674 |
+
["bar", {"subplots": True}],
|
675 |
+
["barh", {"subplots": True}],
|
676 |
+
],
|
677 |
+
)
|
678 |
+
def test_bar_barwidth_position_int_width_1(self, kind, kwargs):
|
679 |
+
# GH 12979
|
680 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((5, 5)))
|
681 |
+
self._check_bar_alignment(df, kind=kind, width=1, **kwargs)
|
682 |
+
|
683 |
+
def _check_bar_alignment(
|
684 |
+
self,
|
685 |
+
df,
|
686 |
+
kind="bar",
|
687 |
+
stacked=False,
|
688 |
+
subplots=False,
|
689 |
+
align="center",
|
690 |
+
width=0.5,
|
691 |
+
position=0.5,
|
692 |
+
):
|
693 |
+
axes = df.plot(
|
694 |
+
kind=kind,
|
695 |
+
stacked=stacked,
|
696 |
+
subplots=subplots,
|
697 |
+
align=align,
|
698 |
+
width=width,
|
699 |
+
position=position,
|
700 |
+
grid=True,
|
701 |
+
)
|
702 |
+
|
703 |
+
axes = _flatten_visible(axes)
|
704 |
+
|
705 |
+
for ax in axes:
|
706 |
+
if kind == "bar":
|
707 |
+
axis = ax.xaxis
|
708 |
+
ax_min, ax_max = ax.get_xlim()
|
709 |
+
min_edge = min(p.get_x() for p in ax.patches)
|
710 |
+
max_edge = max(p.get_x() + p.get_width() for p in ax.patches)
|
711 |
+
elif kind == "barh":
|
712 |
+
axis = ax.yaxis
|
713 |
+
ax_min, ax_max = ax.get_ylim()
|
714 |
+
min_edge = min(p.get_y() for p in ax.patches)
|
715 |
+
max_edge = max(p.get_y() + p.get_height() for p in ax.patches)
|
716 |
+
else:
|
717 |
+
raise ValueError
|
718 |
+
|
719 |
+
# GH 7498
|
720 |
+
# compare margins between lim and bar edges
|
721 |
+
tm.assert_almost_equal(ax_min, min_edge - 0.25)
|
722 |
+
tm.assert_almost_equal(ax_max, max_edge + 0.25)
|
723 |
+
|
724 |
+
p = ax.patches[0]
|
725 |
+
if kind == "bar" and (stacked is True or subplots is True):
|
726 |
+
edge = p.get_x()
|
727 |
+
center = edge + p.get_width() * position
|
728 |
+
elif kind == "bar" and stacked is False:
|
729 |
+
center = p.get_x() + p.get_width() * len(df.columns) * position
|
730 |
+
edge = p.get_x()
|
731 |
+
elif kind == "barh" and (stacked is True or subplots is True):
|
732 |
+
center = p.get_y() + p.get_height() * position
|
733 |
+
edge = p.get_y()
|
734 |
+
elif kind == "barh" and stacked is False:
|
735 |
+
center = p.get_y() + p.get_height() * len(df.columns) * position
|
736 |
+
edge = p.get_y()
|
737 |
+
else:
|
738 |
+
raise ValueError
|
739 |
+
|
740 |
+
# Check the ticks locates on integer
|
741 |
+
assert (axis.get_ticklocs() == np.arange(len(df))).all()
|
742 |
+
|
743 |
+
if align == "center":
|
744 |
+
# Check whether the bar locates on center
|
745 |
+
tm.assert_almost_equal(axis.get_ticklocs()[0], center)
|
746 |
+
elif align == "edge":
|
747 |
+
# Check whether the bar's edge starts from the tick
|
748 |
+
tm.assert_almost_equal(axis.get_ticklocs()[0], edge)
|
749 |
+
else:
|
750 |
+
raise ValueError
|
751 |
+
|
752 |
+
return axes
|
venv/lib/python3.10/site-packages/pandas/tests/plotting/frame/test_hist_box_by.py
ADDED
@@ -0,0 +1,342 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
from pandas import DataFrame
|
7 |
+
import pandas._testing as tm
|
8 |
+
from pandas.tests.plotting.common import (
|
9 |
+
_check_axes_shape,
|
10 |
+
_check_plot_works,
|
11 |
+
get_x_axis,
|
12 |
+
get_y_axis,
|
13 |
+
)
|
14 |
+
|
15 |
+
pytest.importorskip("matplotlib")
|
16 |
+
|
17 |
+
|
18 |
+
@pytest.fixture
|
19 |
+
def hist_df():
|
20 |
+
df = DataFrame(
|
21 |
+
np.random.default_rng(2).standard_normal((30, 2)), columns=["A", "B"]
|
22 |
+
)
|
23 |
+
df["C"] = np.random.default_rng(2).choice(["a", "b", "c"], 30)
|
24 |
+
df["D"] = np.random.default_rng(2).choice(["a", "b", "c"], 30)
|
25 |
+
return df
|
26 |
+
|
27 |
+
|
28 |
+
class TestHistWithBy:
|
29 |
+
@pytest.mark.slow
|
30 |
+
@pytest.mark.parametrize(
|
31 |
+
"by, column, titles, legends",
|
32 |
+
[
|
33 |
+
("C", "A", ["a", "b", "c"], [["A"]] * 3),
|
34 |
+
("C", ["A", "B"], ["a", "b", "c"], [["A", "B"]] * 3),
|
35 |
+
("C", None, ["a", "b", "c"], [["A", "B"]] * 3),
|
36 |
+
(
|
37 |
+
["C", "D"],
|
38 |
+
"A",
|
39 |
+
[
|
40 |
+
"(a, a)",
|
41 |
+
"(b, b)",
|
42 |
+
"(c, c)",
|
43 |
+
],
|
44 |
+
[["A"]] * 3,
|
45 |
+
),
|
46 |
+
(
|
47 |
+
["C", "D"],
|
48 |
+
["A", "B"],
|
49 |
+
[
|
50 |
+
"(a, a)",
|
51 |
+
"(b, b)",
|
52 |
+
"(c, c)",
|
53 |
+
],
|
54 |
+
[["A", "B"]] * 3,
|
55 |
+
),
|
56 |
+
(
|
57 |
+
["C", "D"],
|
58 |
+
None,
|
59 |
+
[
|
60 |
+
"(a, a)",
|
61 |
+
"(b, b)",
|
62 |
+
"(c, c)",
|
63 |
+
],
|
64 |
+
[["A", "B"]] * 3,
|
65 |
+
),
|
66 |
+
],
|
67 |
+
)
|
68 |
+
def test_hist_plot_by_argument(self, by, column, titles, legends, hist_df):
|
69 |
+
# GH 15079
|
70 |
+
axes = _check_plot_works(
|
71 |
+
hist_df.plot.hist, column=column, by=by, default_axes=True
|
72 |
+
)
|
73 |
+
result_titles = [ax.get_title() for ax in axes]
|
74 |
+
result_legends = [
|
75 |
+
[legend.get_text() for legend in ax.get_legend().texts] for ax in axes
|
76 |
+
]
|
77 |
+
|
78 |
+
assert result_legends == legends
|
79 |
+
assert result_titles == titles
|
80 |
+
|
81 |
+
@pytest.mark.parametrize(
|
82 |
+
"by, column, titles, legends",
|
83 |
+
[
|
84 |
+
(0, "A", ["a", "b", "c"], [["A"]] * 3),
|
85 |
+
(0, None, ["a", "b", "c"], [["A", "B"]] * 3),
|
86 |
+
(
|
87 |
+
[0, "D"],
|
88 |
+
"A",
|
89 |
+
[
|
90 |
+
"(a, a)",
|
91 |
+
"(b, b)",
|
92 |
+
"(c, c)",
|
93 |
+
],
|
94 |
+
[["A"]] * 3,
|
95 |
+
),
|
96 |
+
],
|
97 |
+
)
|
98 |
+
def test_hist_plot_by_0(self, by, column, titles, legends, hist_df):
|
99 |
+
# GH 15079
|
100 |
+
df = hist_df.copy()
|
101 |
+
df = df.rename(columns={"C": 0})
|
102 |
+
|
103 |
+
axes = _check_plot_works(df.plot.hist, default_axes=True, column=column, by=by)
|
104 |
+
result_titles = [ax.get_title() for ax in axes]
|
105 |
+
result_legends = [
|
106 |
+
[legend.get_text() for legend in ax.get_legend().texts] for ax in axes
|
107 |
+
]
|
108 |
+
|
109 |
+
assert result_legends == legends
|
110 |
+
assert result_titles == titles
|
111 |
+
|
112 |
+
@pytest.mark.parametrize(
|
113 |
+
"by, column",
|
114 |
+
[
|
115 |
+
([], ["A"]),
|
116 |
+
([], ["A", "B"]),
|
117 |
+
((), None),
|
118 |
+
((), ["A", "B"]),
|
119 |
+
],
|
120 |
+
)
|
121 |
+
def test_hist_plot_empty_list_string_tuple_by(self, by, column, hist_df):
|
122 |
+
# GH 15079
|
123 |
+
msg = "No group keys passed"
|
124 |
+
with pytest.raises(ValueError, match=msg):
|
125 |
+
_check_plot_works(
|
126 |
+
hist_df.plot.hist, default_axes=True, column=column, by=by
|
127 |
+
)
|
128 |
+
|
129 |
+
@pytest.mark.slow
|
130 |
+
@pytest.mark.parametrize(
|
131 |
+
"by, column, layout, axes_num",
|
132 |
+
[
|
133 |
+
(["C"], "A", (2, 2), 3),
|
134 |
+
("C", "A", (2, 2), 3),
|
135 |
+
(["C"], ["A"], (1, 3), 3),
|
136 |
+
("C", None, (3, 1), 3),
|
137 |
+
("C", ["A", "B"], (3, 1), 3),
|
138 |
+
(["C", "D"], "A", (9, 1), 3),
|
139 |
+
(["C", "D"], "A", (3, 3), 3),
|
140 |
+
(["C", "D"], ["A"], (5, 2), 3),
|
141 |
+
(["C", "D"], ["A", "B"], (9, 1), 3),
|
142 |
+
(["C", "D"], None, (9, 1), 3),
|
143 |
+
(["C", "D"], ["A", "B"], (5, 2), 3),
|
144 |
+
],
|
145 |
+
)
|
146 |
+
def test_hist_plot_layout_with_by(self, by, column, layout, axes_num, hist_df):
|
147 |
+
# GH 15079
|
148 |
+
# _check_plot_works adds an ax so catch warning. see GH #13188
|
149 |
+
with tm.assert_produces_warning(UserWarning, check_stacklevel=False):
|
150 |
+
axes = _check_plot_works(
|
151 |
+
hist_df.plot.hist, column=column, by=by, layout=layout
|
152 |
+
)
|
153 |
+
_check_axes_shape(axes, axes_num=axes_num, layout=layout)
|
154 |
+
|
155 |
+
@pytest.mark.parametrize(
|
156 |
+
"msg, by, layout",
|
157 |
+
[
|
158 |
+
("larger than required size", ["C", "D"], (1, 1)),
|
159 |
+
(re.escape("Layout must be a tuple of (rows, columns)"), "C", (1,)),
|
160 |
+
("At least one dimension of layout must be positive", "C", (-1, -1)),
|
161 |
+
],
|
162 |
+
)
|
163 |
+
def test_hist_plot_invalid_layout_with_by_raises(self, msg, by, layout, hist_df):
|
164 |
+
# GH 15079, test if error is raised when invalid layout is given
|
165 |
+
|
166 |
+
with pytest.raises(ValueError, match=msg):
|
167 |
+
hist_df.plot.hist(column=["A", "B"], by=by, layout=layout)
|
168 |
+
|
169 |
+
@pytest.mark.slow
|
170 |
+
def test_axis_share_x_with_by(self, hist_df):
|
171 |
+
# GH 15079
|
172 |
+
ax1, ax2, ax3 = hist_df.plot.hist(column="A", by="C", sharex=True)
|
173 |
+
|
174 |
+
# share x
|
175 |
+
assert get_x_axis(ax1).joined(ax1, ax2)
|
176 |
+
assert get_x_axis(ax2).joined(ax1, ax2)
|
177 |
+
assert get_x_axis(ax3).joined(ax1, ax3)
|
178 |
+
assert get_x_axis(ax3).joined(ax2, ax3)
|
179 |
+
|
180 |
+
# don't share y
|
181 |
+
assert not get_y_axis(ax1).joined(ax1, ax2)
|
182 |
+
assert not get_y_axis(ax2).joined(ax1, ax2)
|
183 |
+
assert not get_y_axis(ax3).joined(ax1, ax3)
|
184 |
+
assert not get_y_axis(ax3).joined(ax2, ax3)
|
185 |
+
|
186 |
+
@pytest.mark.slow
|
187 |
+
def test_axis_share_y_with_by(self, hist_df):
|
188 |
+
# GH 15079
|
189 |
+
ax1, ax2, ax3 = hist_df.plot.hist(column="A", by="C", sharey=True)
|
190 |
+
|
191 |
+
# share y
|
192 |
+
assert get_y_axis(ax1).joined(ax1, ax2)
|
193 |
+
assert get_y_axis(ax2).joined(ax1, ax2)
|
194 |
+
assert get_y_axis(ax3).joined(ax1, ax3)
|
195 |
+
assert get_y_axis(ax3).joined(ax2, ax3)
|
196 |
+
|
197 |
+
# don't share x
|
198 |
+
assert not get_x_axis(ax1).joined(ax1, ax2)
|
199 |
+
assert not get_x_axis(ax2).joined(ax1, ax2)
|
200 |
+
assert not get_x_axis(ax3).joined(ax1, ax3)
|
201 |
+
assert not get_x_axis(ax3).joined(ax2, ax3)
|
202 |
+
|
203 |
+
@pytest.mark.parametrize("figsize", [(12, 8), (20, 10)])
|
204 |
+
def test_figure_shape_hist_with_by(self, figsize, hist_df):
|
205 |
+
# GH 15079
|
206 |
+
axes = hist_df.plot.hist(column="A", by="C", figsize=figsize)
|
207 |
+
_check_axes_shape(axes, axes_num=3, figsize=figsize)
|
208 |
+
|
209 |
+
|
210 |
+
class TestBoxWithBy:
|
211 |
+
@pytest.mark.parametrize(
|
212 |
+
"by, column, titles, xticklabels",
|
213 |
+
[
|
214 |
+
("C", "A", ["A"], [["a", "b", "c"]]),
|
215 |
+
(
|
216 |
+
["C", "D"],
|
217 |
+
"A",
|
218 |
+
["A"],
|
219 |
+
[
|
220 |
+
[
|
221 |
+
"(a, a)",
|
222 |
+
"(b, b)",
|
223 |
+
"(c, c)",
|
224 |
+
]
|
225 |
+
],
|
226 |
+
),
|
227 |
+
("C", ["A", "B"], ["A", "B"], [["a", "b", "c"]] * 2),
|
228 |
+
(
|
229 |
+
["C", "D"],
|
230 |
+
["A", "B"],
|
231 |
+
["A", "B"],
|
232 |
+
[
|
233 |
+
[
|
234 |
+
"(a, a)",
|
235 |
+
"(b, b)",
|
236 |
+
"(c, c)",
|
237 |
+
]
|
238 |
+
]
|
239 |
+
* 2,
|
240 |
+
),
|
241 |
+
(["C"], None, ["A", "B"], [["a", "b", "c"]] * 2),
|
242 |
+
],
|
243 |
+
)
|
244 |
+
def test_box_plot_by_argument(self, by, column, titles, xticklabels, hist_df):
|
245 |
+
# GH 15079
|
246 |
+
axes = _check_plot_works(
|
247 |
+
hist_df.plot.box, default_axes=True, column=column, by=by
|
248 |
+
)
|
249 |
+
result_titles = [ax.get_title() for ax in axes]
|
250 |
+
result_xticklabels = [
|
251 |
+
[label.get_text() for label in ax.get_xticklabels()] for ax in axes
|
252 |
+
]
|
253 |
+
|
254 |
+
assert result_xticklabels == xticklabels
|
255 |
+
assert result_titles == titles
|
256 |
+
|
257 |
+
@pytest.mark.parametrize(
|
258 |
+
"by, column, titles, xticklabels",
|
259 |
+
[
|
260 |
+
(0, "A", ["A"], [["a", "b", "c"]]),
|
261 |
+
(
|
262 |
+
[0, "D"],
|
263 |
+
"A",
|
264 |
+
["A"],
|
265 |
+
[
|
266 |
+
[
|
267 |
+
"(a, a)",
|
268 |
+
"(b, b)",
|
269 |
+
"(c, c)",
|
270 |
+
]
|
271 |
+
],
|
272 |
+
),
|
273 |
+
(0, None, ["A", "B"], [["a", "b", "c"]] * 2),
|
274 |
+
],
|
275 |
+
)
|
276 |
+
def test_box_plot_by_0(self, by, column, titles, xticklabels, hist_df):
|
277 |
+
# GH 15079
|
278 |
+
df = hist_df.copy()
|
279 |
+
df = df.rename(columns={"C": 0})
|
280 |
+
|
281 |
+
axes = _check_plot_works(df.plot.box, default_axes=True, column=column, by=by)
|
282 |
+
result_titles = [ax.get_title() for ax in axes]
|
283 |
+
result_xticklabels = [
|
284 |
+
[label.get_text() for label in ax.get_xticklabels()] for ax in axes
|
285 |
+
]
|
286 |
+
|
287 |
+
assert result_xticklabels == xticklabels
|
288 |
+
assert result_titles == titles
|
289 |
+
|
290 |
+
@pytest.mark.parametrize(
|
291 |
+
"by, column",
|
292 |
+
[
|
293 |
+
([], ["A"]),
|
294 |
+
((), "A"),
|
295 |
+
([], None),
|
296 |
+
((), ["A", "B"]),
|
297 |
+
],
|
298 |
+
)
|
299 |
+
def test_box_plot_with_none_empty_list_by(self, by, column, hist_df):
|
300 |
+
# GH 15079
|
301 |
+
msg = "No group keys passed"
|
302 |
+
with pytest.raises(ValueError, match=msg):
|
303 |
+
_check_plot_works(hist_df.plot.box, default_axes=True, column=column, by=by)
|
304 |
+
|
305 |
+
@pytest.mark.slow
|
306 |
+
@pytest.mark.parametrize(
|
307 |
+
"by, column, layout, axes_num",
|
308 |
+
[
|
309 |
+
(["C"], "A", (1, 1), 1),
|
310 |
+
("C", "A", (1, 1), 1),
|
311 |
+
("C", None, (2, 1), 2),
|
312 |
+
("C", ["A", "B"], (1, 2), 2),
|
313 |
+
(["C", "D"], "A", (1, 1), 1),
|
314 |
+
(["C", "D"], None, (1, 2), 2),
|
315 |
+
],
|
316 |
+
)
|
317 |
+
def test_box_plot_layout_with_by(self, by, column, layout, axes_num, hist_df):
|
318 |
+
# GH 15079
|
319 |
+
axes = _check_plot_works(
|
320 |
+
hist_df.plot.box, default_axes=True, column=column, by=by, layout=layout
|
321 |
+
)
|
322 |
+
_check_axes_shape(axes, axes_num=axes_num, layout=layout)
|
323 |
+
|
324 |
+
@pytest.mark.parametrize(
|
325 |
+
"msg, by, layout",
|
326 |
+
[
|
327 |
+
("larger than required size", ["C", "D"], (1, 1)),
|
328 |
+
(re.escape("Layout must be a tuple of (rows, columns)"), "C", (1,)),
|
329 |
+
("At least one dimension of layout must be positive", "C", (-1, -1)),
|
330 |
+
],
|
331 |
+
)
|
332 |
+
def test_box_plot_invalid_layout_with_by_raises(self, msg, by, layout, hist_df):
|
333 |
+
# GH 15079, test if error is raised when invalid layout is given
|
334 |
+
|
335 |
+
with pytest.raises(ValueError, match=msg):
|
336 |
+
hist_df.plot.box(column=["A", "B"], by=by, layout=layout)
|
337 |
+
|
338 |
+
@pytest.mark.parametrize("figsize", [(12, 8), (20, 10)])
|
339 |
+
def test_figure_shape_hist_with_by(self, figsize, hist_df):
|
340 |
+
# GH 15079
|
341 |
+
axes = hist_df.plot.box(column="A", by="C", figsize=figsize)
|
342 |
+
_check_axes_shape(axes, axes_num=1, figsize=figsize)
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (187 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/__pycache__/test_na_scalar.cpython-310.pyc
ADDED
Binary file (7.97 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/__pycache__/test_nat.cpython-310.pyc
ADDED
Binary file (15.7 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__init__.py
ADDED
File without changes
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (196 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_arithmetic.cpython-310.pyc
ADDED
Binary file (5.75 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_constructors.cpython-310.pyc
ADDED
Binary file (2.04 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_contains.cpython-310.pyc
ADDED
Binary file (2.33 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_formats.cpython-310.pyc
ADDED
Binary file (590 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_interval.cpython-310.pyc
ADDED
Binary file (2.49 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_overlaps.cpython-310.pyc
ADDED
Binary file (2.63 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_arithmetic.py
ADDED
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import timedelta
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
from pandas import (
|
7 |
+
Interval,
|
8 |
+
Timedelta,
|
9 |
+
Timestamp,
|
10 |
+
)
|
11 |
+
import pandas._testing as tm
|
12 |
+
|
13 |
+
|
14 |
+
class TestIntervalArithmetic:
|
15 |
+
def test_interval_add(self, closed):
|
16 |
+
interval = Interval(0, 1, closed=closed)
|
17 |
+
expected = Interval(1, 2, closed=closed)
|
18 |
+
|
19 |
+
result = interval + 1
|
20 |
+
assert result == expected
|
21 |
+
|
22 |
+
result = 1 + interval
|
23 |
+
assert result == expected
|
24 |
+
|
25 |
+
result = interval
|
26 |
+
result += 1
|
27 |
+
assert result == expected
|
28 |
+
|
29 |
+
msg = r"unsupported operand type\(s\) for \+"
|
30 |
+
with pytest.raises(TypeError, match=msg):
|
31 |
+
interval + interval
|
32 |
+
|
33 |
+
with pytest.raises(TypeError, match=msg):
|
34 |
+
interval + "foo"
|
35 |
+
|
36 |
+
def test_interval_sub(self, closed):
|
37 |
+
interval = Interval(0, 1, closed=closed)
|
38 |
+
expected = Interval(-1, 0, closed=closed)
|
39 |
+
|
40 |
+
result = interval - 1
|
41 |
+
assert result == expected
|
42 |
+
|
43 |
+
result = interval
|
44 |
+
result -= 1
|
45 |
+
assert result == expected
|
46 |
+
|
47 |
+
msg = r"unsupported operand type\(s\) for -"
|
48 |
+
with pytest.raises(TypeError, match=msg):
|
49 |
+
interval - interval
|
50 |
+
|
51 |
+
with pytest.raises(TypeError, match=msg):
|
52 |
+
interval - "foo"
|
53 |
+
|
54 |
+
def test_interval_mult(self, closed):
|
55 |
+
interval = Interval(0, 1, closed=closed)
|
56 |
+
expected = Interval(0, 2, closed=closed)
|
57 |
+
|
58 |
+
result = interval * 2
|
59 |
+
assert result == expected
|
60 |
+
|
61 |
+
result = 2 * interval
|
62 |
+
assert result == expected
|
63 |
+
|
64 |
+
result = interval
|
65 |
+
result *= 2
|
66 |
+
assert result == expected
|
67 |
+
|
68 |
+
msg = r"unsupported operand type\(s\) for \*"
|
69 |
+
with pytest.raises(TypeError, match=msg):
|
70 |
+
interval * interval
|
71 |
+
|
72 |
+
msg = r"can\'t multiply sequence by non-int"
|
73 |
+
with pytest.raises(TypeError, match=msg):
|
74 |
+
interval * "foo"
|
75 |
+
|
76 |
+
def test_interval_div(self, closed):
|
77 |
+
interval = Interval(0, 1, closed=closed)
|
78 |
+
expected = Interval(0, 0.5, closed=closed)
|
79 |
+
|
80 |
+
result = interval / 2.0
|
81 |
+
assert result == expected
|
82 |
+
|
83 |
+
result = interval
|
84 |
+
result /= 2.0
|
85 |
+
assert result == expected
|
86 |
+
|
87 |
+
msg = r"unsupported operand type\(s\) for /"
|
88 |
+
with pytest.raises(TypeError, match=msg):
|
89 |
+
interval / interval
|
90 |
+
|
91 |
+
with pytest.raises(TypeError, match=msg):
|
92 |
+
interval / "foo"
|
93 |
+
|
94 |
+
def test_interval_floordiv(self, closed):
|
95 |
+
interval = Interval(1, 2, closed=closed)
|
96 |
+
expected = Interval(0, 1, closed=closed)
|
97 |
+
|
98 |
+
result = interval // 2
|
99 |
+
assert result == expected
|
100 |
+
|
101 |
+
result = interval
|
102 |
+
result //= 2
|
103 |
+
assert result == expected
|
104 |
+
|
105 |
+
msg = r"unsupported operand type\(s\) for //"
|
106 |
+
with pytest.raises(TypeError, match=msg):
|
107 |
+
interval // interval
|
108 |
+
|
109 |
+
with pytest.raises(TypeError, match=msg):
|
110 |
+
interval // "foo"
|
111 |
+
|
112 |
+
@pytest.mark.parametrize("method", ["__add__", "__sub__"])
|
113 |
+
@pytest.mark.parametrize(
|
114 |
+
"interval",
|
115 |
+
[
|
116 |
+
Interval(
|
117 |
+
Timestamp("2017-01-01 00:00:00"), Timestamp("2018-01-01 00:00:00")
|
118 |
+
),
|
119 |
+
Interval(Timedelta(days=7), Timedelta(days=14)),
|
120 |
+
],
|
121 |
+
)
|
122 |
+
@pytest.mark.parametrize(
|
123 |
+
"delta", [Timedelta(days=7), timedelta(7), np.timedelta64(7, "D")]
|
124 |
+
)
|
125 |
+
def test_time_interval_add_subtract_timedelta(self, interval, delta, method):
|
126 |
+
# https://github.com/pandas-dev/pandas/issues/32023
|
127 |
+
result = getattr(interval, method)(delta)
|
128 |
+
left = getattr(interval.left, method)(delta)
|
129 |
+
right = getattr(interval.right, method)(delta)
|
130 |
+
expected = Interval(left, right)
|
131 |
+
|
132 |
+
assert result == expected
|
133 |
+
|
134 |
+
@pytest.mark.parametrize("interval", [Interval(1, 2), Interval(1.0, 2.0)])
|
135 |
+
@pytest.mark.parametrize(
|
136 |
+
"delta", [Timedelta(days=7), timedelta(7), np.timedelta64(7, "D")]
|
137 |
+
)
|
138 |
+
def test_numeric_interval_add_timedelta_raises(self, interval, delta):
|
139 |
+
# https://github.com/pandas-dev/pandas/issues/32023
|
140 |
+
msg = "|".join(
|
141 |
+
[
|
142 |
+
"unsupported operand",
|
143 |
+
"cannot use operands",
|
144 |
+
"Only numeric, Timestamp and Timedelta endpoints are allowed",
|
145 |
+
]
|
146 |
+
)
|
147 |
+
with pytest.raises((TypeError, ValueError), match=msg):
|
148 |
+
interval + delta
|
149 |
+
|
150 |
+
with pytest.raises((TypeError, ValueError), match=msg):
|
151 |
+
delta + interval
|
152 |
+
|
153 |
+
@pytest.mark.parametrize("klass", [timedelta, np.timedelta64, Timedelta])
|
154 |
+
def test_timedelta_add_timestamp_interval(self, klass):
|
155 |
+
delta = klass(0)
|
156 |
+
expected = Interval(Timestamp("2020-01-01"), Timestamp("2020-02-01"))
|
157 |
+
|
158 |
+
result = delta + expected
|
159 |
+
assert result == expected
|
160 |
+
|
161 |
+
result = expected + delta
|
162 |
+
assert result == expected
|
163 |
+
|
164 |
+
|
165 |
+
class TestIntervalComparisons:
|
166 |
+
def test_interval_equal(self):
|
167 |
+
assert Interval(0, 1) == Interval(0, 1, closed="right")
|
168 |
+
assert Interval(0, 1) != Interval(0, 1, closed="left")
|
169 |
+
assert Interval(0, 1) != 0
|
170 |
+
|
171 |
+
def test_interval_comparison(self):
|
172 |
+
msg = (
|
173 |
+
"'<' not supported between instances of "
|
174 |
+
"'pandas._libs.interval.Interval' and 'int'"
|
175 |
+
)
|
176 |
+
with pytest.raises(TypeError, match=msg):
|
177 |
+
Interval(0, 1) < 2
|
178 |
+
|
179 |
+
assert Interval(0, 1) < Interval(1, 2)
|
180 |
+
assert Interval(0, 1) < Interval(0, 2)
|
181 |
+
assert Interval(0, 1) < Interval(0.5, 1.5)
|
182 |
+
assert Interval(0, 1) <= Interval(0, 1)
|
183 |
+
assert Interval(0, 1) > Interval(-1, 2)
|
184 |
+
assert Interval(0, 1) >= Interval(0, 1)
|
185 |
+
|
186 |
+
def test_equality_comparison_broadcasts_over_array(self):
|
187 |
+
# https://github.com/pandas-dev/pandas/issues/35931
|
188 |
+
interval = Interval(0, 1)
|
189 |
+
arr = np.array([interval, interval])
|
190 |
+
result = interval == arr
|
191 |
+
expected = np.array([True, True])
|
192 |
+
tm.assert_numpy_array_equal(result, expected)
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_constructors.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas import (
|
4 |
+
Interval,
|
5 |
+
Period,
|
6 |
+
Timestamp,
|
7 |
+
)
|
8 |
+
|
9 |
+
|
10 |
+
class TestIntervalConstructors:
|
11 |
+
@pytest.mark.parametrize(
|
12 |
+
"left, right",
|
13 |
+
[
|
14 |
+
("a", "z"),
|
15 |
+
(("a", "b"), ("c", "d")),
|
16 |
+
(list("AB"), list("ab")),
|
17 |
+
(Interval(0, 1), Interval(1, 2)),
|
18 |
+
(Period("2018Q1", freq="Q"), Period("2018Q1", freq="Q")),
|
19 |
+
],
|
20 |
+
)
|
21 |
+
def test_construct_errors(self, left, right):
|
22 |
+
# GH#23013
|
23 |
+
msg = "Only numeric, Timestamp and Timedelta endpoints are allowed"
|
24 |
+
with pytest.raises(ValueError, match=msg):
|
25 |
+
Interval(left, right)
|
26 |
+
|
27 |
+
def test_constructor_errors(self):
|
28 |
+
msg = "invalid option for 'closed': foo"
|
29 |
+
with pytest.raises(ValueError, match=msg):
|
30 |
+
Interval(0, 1, closed="foo")
|
31 |
+
|
32 |
+
msg = "left side of interval must be <= right side"
|
33 |
+
with pytest.raises(ValueError, match=msg):
|
34 |
+
Interval(1, 0)
|
35 |
+
|
36 |
+
@pytest.mark.parametrize(
|
37 |
+
"tz_left, tz_right", [(None, "UTC"), ("UTC", None), ("UTC", "US/Eastern")]
|
38 |
+
)
|
39 |
+
def test_constructor_errors_tz(self, tz_left, tz_right):
|
40 |
+
# GH#18538
|
41 |
+
left = Timestamp("2017-01-01", tz=tz_left)
|
42 |
+
right = Timestamp("2017-01-02", tz=tz_right)
|
43 |
+
|
44 |
+
if tz_left is None or tz_right is None:
|
45 |
+
error = TypeError
|
46 |
+
msg = "Cannot compare tz-naive and tz-aware timestamps"
|
47 |
+
else:
|
48 |
+
error = ValueError
|
49 |
+
msg = "left and right must have the same time zone"
|
50 |
+
with pytest.raises(error, match=msg):
|
51 |
+
Interval(left, right)
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_contains.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas import (
|
4 |
+
Interval,
|
5 |
+
Timedelta,
|
6 |
+
Timestamp,
|
7 |
+
)
|
8 |
+
|
9 |
+
|
10 |
+
class TestContains:
|
11 |
+
def test_contains(self):
|
12 |
+
interval = Interval(0, 1)
|
13 |
+
assert 0.5 in interval
|
14 |
+
assert 1 in interval
|
15 |
+
assert 0 not in interval
|
16 |
+
|
17 |
+
interval_both = Interval(0, 1, "both")
|
18 |
+
assert 0 in interval_both
|
19 |
+
assert 1 in interval_both
|
20 |
+
|
21 |
+
interval_neither = Interval(0, 1, closed="neither")
|
22 |
+
assert 0 not in interval_neither
|
23 |
+
assert 0.5 in interval_neither
|
24 |
+
assert 1 not in interval_neither
|
25 |
+
|
26 |
+
def test_contains_interval(self, inclusive_endpoints_fixture):
|
27 |
+
interval1 = Interval(0, 1, "both")
|
28 |
+
interval2 = Interval(0, 1, inclusive_endpoints_fixture)
|
29 |
+
assert interval1 in interval1
|
30 |
+
assert interval2 in interval2
|
31 |
+
assert interval2 in interval1
|
32 |
+
assert interval1 not in interval2 or inclusive_endpoints_fixture == "both"
|
33 |
+
|
34 |
+
def test_contains_infinite_length(self):
|
35 |
+
interval1 = Interval(0, 1, "both")
|
36 |
+
interval2 = Interval(float("-inf"), float("inf"), "neither")
|
37 |
+
assert interval1 in interval2
|
38 |
+
assert interval2 not in interval1
|
39 |
+
|
40 |
+
def test_contains_zero_length(self):
|
41 |
+
interval1 = Interval(0, 1, "both")
|
42 |
+
interval2 = Interval(-1, -1, "both")
|
43 |
+
interval3 = Interval(0.5, 0.5, "both")
|
44 |
+
assert interval2 not in interval1
|
45 |
+
assert interval3 in interval1
|
46 |
+
assert interval2 not in interval3 and interval3 not in interval2
|
47 |
+
assert interval1 not in interval2 and interval1 not in interval3
|
48 |
+
|
49 |
+
@pytest.mark.parametrize(
|
50 |
+
"type1",
|
51 |
+
[
|
52 |
+
(0, 1),
|
53 |
+
(Timestamp(2000, 1, 1, 0), Timestamp(2000, 1, 1, 1)),
|
54 |
+
(Timedelta("0h"), Timedelta("1h")),
|
55 |
+
],
|
56 |
+
)
|
57 |
+
@pytest.mark.parametrize(
|
58 |
+
"type2",
|
59 |
+
[
|
60 |
+
(0, 1),
|
61 |
+
(Timestamp(2000, 1, 1, 0), Timestamp(2000, 1, 1, 1)),
|
62 |
+
(Timedelta("0h"), Timedelta("1h")),
|
63 |
+
],
|
64 |
+
)
|
65 |
+
def test_contains_mixed_types(self, type1, type2):
|
66 |
+
interval1 = Interval(*type1)
|
67 |
+
interval2 = Interval(*type2)
|
68 |
+
if type1 == type2:
|
69 |
+
assert interval1 in interval2
|
70 |
+
else:
|
71 |
+
msg = "^'<=' not supported between instances of"
|
72 |
+
with pytest.raises(TypeError, match=msg):
|
73 |
+
interval1 in interval2
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_formats.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pandas import Interval
|
2 |
+
|
3 |
+
|
4 |
+
def test_interval_repr():
|
5 |
+
interval = Interval(0, 1)
|
6 |
+
assert repr(interval) == "Interval(0, 1, closed='right')"
|
7 |
+
assert str(interval) == "(0, 1]"
|
8 |
+
|
9 |
+
interval_left = Interval(0, 1, closed="left")
|
10 |
+
assert repr(interval_left) == "Interval(0, 1, closed='left')"
|
11 |
+
assert str(interval_left) == "[0, 1)"
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_interval.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
Interval,
|
6 |
+
Timedelta,
|
7 |
+
Timestamp,
|
8 |
+
)
|
9 |
+
|
10 |
+
|
11 |
+
@pytest.fixture
|
12 |
+
def interval():
|
13 |
+
return Interval(0, 1)
|
14 |
+
|
15 |
+
|
16 |
+
class TestInterval:
|
17 |
+
def test_properties(self, interval):
|
18 |
+
assert interval.closed == "right"
|
19 |
+
assert interval.left == 0
|
20 |
+
assert interval.right == 1
|
21 |
+
assert interval.mid == 0.5
|
22 |
+
|
23 |
+
def test_hash(self, interval):
|
24 |
+
# should not raise
|
25 |
+
hash(interval)
|
26 |
+
|
27 |
+
@pytest.mark.parametrize(
|
28 |
+
"left, right, expected",
|
29 |
+
[
|
30 |
+
(0, 5, 5),
|
31 |
+
(-2, 5.5, 7.5),
|
32 |
+
(10, 10, 0),
|
33 |
+
(10, np.inf, np.inf),
|
34 |
+
(-np.inf, -5, np.inf),
|
35 |
+
(-np.inf, np.inf, np.inf),
|
36 |
+
(Timedelta("0 days"), Timedelta("5 days"), Timedelta("5 days")),
|
37 |
+
(Timedelta("10 days"), Timedelta("10 days"), Timedelta("0 days")),
|
38 |
+
(Timedelta("1h10min"), Timedelta("5h5min"), Timedelta("3h55min")),
|
39 |
+
(Timedelta("5s"), Timedelta("1h"), Timedelta("59min55s")),
|
40 |
+
],
|
41 |
+
)
|
42 |
+
def test_length(self, left, right, expected):
|
43 |
+
# GH 18789
|
44 |
+
iv = Interval(left, right)
|
45 |
+
result = iv.length
|
46 |
+
assert result == expected
|
47 |
+
|
48 |
+
@pytest.mark.parametrize(
|
49 |
+
"left, right, expected",
|
50 |
+
[
|
51 |
+
("2017-01-01", "2017-01-06", "5 days"),
|
52 |
+
("2017-01-01", "2017-01-01 12:00:00", "12 hours"),
|
53 |
+
("2017-01-01 12:00", "2017-01-01 12:00:00", "0 days"),
|
54 |
+
("2017-01-01 12:01", "2017-01-05 17:31:00", "4 days 5 hours 30 min"),
|
55 |
+
],
|
56 |
+
)
|
57 |
+
@pytest.mark.parametrize("tz", (None, "UTC", "CET", "US/Eastern"))
|
58 |
+
def test_length_timestamp(self, tz, left, right, expected):
|
59 |
+
# GH 18789
|
60 |
+
iv = Interval(Timestamp(left, tz=tz), Timestamp(right, tz=tz))
|
61 |
+
result = iv.length
|
62 |
+
expected = Timedelta(expected)
|
63 |
+
assert result == expected
|
64 |
+
|
65 |
+
@pytest.mark.parametrize(
|
66 |
+
"left, right",
|
67 |
+
[
|
68 |
+
(0, 1),
|
69 |
+
(Timedelta("0 days"), Timedelta("1 day")),
|
70 |
+
(Timestamp("2018-01-01"), Timestamp("2018-01-02")),
|
71 |
+
(
|
72 |
+
Timestamp("2018-01-01", tz="US/Eastern"),
|
73 |
+
Timestamp("2018-01-02", tz="US/Eastern"),
|
74 |
+
),
|
75 |
+
],
|
76 |
+
)
|
77 |
+
def test_is_empty(self, left, right, closed):
|
78 |
+
# GH27219
|
79 |
+
# non-empty always return False
|
80 |
+
iv = Interval(left, right, closed)
|
81 |
+
assert iv.is_empty is False
|
82 |
+
|
83 |
+
# same endpoint is empty except when closed='both' (contains one point)
|
84 |
+
iv = Interval(left, left, closed)
|
85 |
+
result = iv.is_empty
|
86 |
+
expected = closed != "both"
|
87 |
+
assert result is expected
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_overlaps.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas import (
|
4 |
+
Interval,
|
5 |
+
Timedelta,
|
6 |
+
Timestamp,
|
7 |
+
)
|
8 |
+
|
9 |
+
|
10 |
+
@pytest.fixture(
|
11 |
+
params=[
|
12 |
+
(Timedelta("0 days"), Timedelta("1 day")),
|
13 |
+
(Timestamp("2018-01-01"), Timedelta("1 day")),
|
14 |
+
(0, 1),
|
15 |
+
],
|
16 |
+
ids=lambda x: type(x[0]).__name__,
|
17 |
+
)
|
18 |
+
def start_shift(request):
|
19 |
+
"""
|
20 |
+
Fixture for generating intervals of types from a start value and a shift
|
21 |
+
value that can be added to start to generate an endpoint
|
22 |
+
"""
|
23 |
+
return request.param
|
24 |
+
|
25 |
+
|
26 |
+
class TestOverlaps:
|
27 |
+
def test_overlaps_self(self, start_shift, closed):
|
28 |
+
start, shift = start_shift
|
29 |
+
interval = Interval(start, start + shift, closed)
|
30 |
+
assert interval.overlaps(interval)
|
31 |
+
|
32 |
+
def test_overlaps_nested(self, start_shift, closed, other_closed):
|
33 |
+
start, shift = start_shift
|
34 |
+
interval1 = Interval(start, start + 3 * shift, other_closed)
|
35 |
+
interval2 = Interval(start + shift, start + 2 * shift, closed)
|
36 |
+
|
37 |
+
# nested intervals should always overlap
|
38 |
+
assert interval1.overlaps(interval2)
|
39 |
+
|
40 |
+
def test_overlaps_disjoint(self, start_shift, closed, other_closed):
|
41 |
+
start, shift = start_shift
|
42 |
+
interval1 = Interval(start, start + shift, other_closed)
|
43 |
+
interval2 = Interval(start + 2 * shift, start + 3 * shift, closed)
|
44 |
+
|
45 |
+
# disjoint intervals should never overlap
|
46 |
+
assert not interval1.overlaps(interval2)
|
47 |
+
|
48 |
+
def test_overlaps_endpoint(self, start_shift, closed, other_closed):
|
49 |
+
start, shift = start_shift
|
50 |
+
interval1 = Interval(start, start + shift, other_closed)
|
51 |
+
interval2 = Interval(start + shift, start + 2 * shift, closed)
|
52 |
+
|
53 |
+
# overlap if shared endpoint is closed for both (overlap at a point)
|
54 |
+
result = interval1.overlaps(interval2)
|
55 |
+
expected = interval1.closed_right and interval2.closed_left
|
56 |
+
assert result == expected
|
57 |
+
|
58 |
+
@pytest.mark.parametrize(
|
59 |
+
"other",
|
60 |
+
[10, True, "foo", Timedelta("1 day"), Timestamp("2018-01-01")],
|
61 |
+
ids=lambda x: type(x).__name__,
|
62 |
+
)
|
63 |
+
def test_overlaps_invalid_type(self, other):
|
64 |
+
interval = Interval(0, 1)
|
65 |
+
msg = f"`other` must be an Interval, got {type(other).__name__}"
|
66 |
+
with pytest.raises(TypeError, match=msg):
|
67 |
+
interval.overlaps(other)
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__init__.py
ADDED
File without changes
|
venv/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (197 Bytes). View file
|
|