From 879fd3b356ba2e965ae4481485b58a1d258049b3 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sun, 21 Aug 2022 13:59:23 +0000 Subject: [PATCH] Format code with black, yapf, autopep8 and isort This commit fixes the style issues introduced in af41a49 according to the output from black, yapf, autopep8 and isort. Details: https://deepsource.io/gh/shubham11941140/pandas/transform/711c1955-a830-41a9-a9e0-7389c3fe8888/ --- pandas/__init__.py | 181 +++++---- pandas/_typing.py | 79 ++-- pandas/io/parquet.py | 115 +++--- pandas/tests/plotting/frame/test_frame.py | 432 +++++++++++++--------- 4 files changed, 436 insertions(+), 371 deletions(-) diff --git a/pandas/__init__.py b/pandas/__init__.py index 89dc49de9da26..e4605fabe6fde 100644 --- a/pandas/__init__.py +++ b/pandas/__init__.py @@ -13,16 +13,19 @@ missing_dependencies.append(f"{dependency}: {e}") if missing_dependencies: - raise ImportError( - "Unable to import required dependencies:\n" + "\n".join(missing_dependencies) - ) + raise ImportError("Unable to import required dependencies:\n" + + "\n".join(missing_dependencies)) del hard_dependencies, dependency, missing_dependencies # numpy compat from pandas.compat import is_numpy_dev as _is_numpy_dev try: - from pandas._libs import hashtable as _hashtable, lib as _lib, tslib as _tslib + from pandas._libs import ( + hashtable as _hashtable, + lib as _lib, + tslib as _tslib, + ) except ImportError as err: # pragma: no cover module = err.name raise ImportError( @@ -34,143 +37,133 @@ del _tslib, _lib, _hashtable from pandas._config import ( - get_option, - set_option, - reset_option, describe_option, + get_option, option_context, options, + reset_option, + set_option, ) -from pandas.core.api import ( - # dtype +from pandas.util._print_versions import show_versions +from pandas.util._tester import test + +from pandas import ( + api, + arrays, + errors, + io, + plotting, + testing, + tseries, +) + +# use the closest tagged version if possible +from pandas._version import get_versions +from pandas.core.api import ( # dtype; missing; indexes; tseries; conversion; misc + NA, + BooleanDtype, + Categorical, + CategoricalDtype, + CategoricalIndex, + DataFrame, + DateOffset, + DatetimeIndex, + DatetimeTZDtype, + Flags, + Float32Dtype, + Float64Dtype, + Grouper, + Index, + IndexSlice, Int8Dtype, Int16Dtype, Int32Dtype, Int64Dtype, + Interval, + IntervalDtype, + IntervalIndex, + MultiIndex, + NamedAgg, + NaT, + Period, + PeriodDtype, + PeriodIndex, + RangeIndex, + Series, + StringDtype, + Timedelta, + TimedeltaIndex, + Timestamp, UInt8Dtype, UInt16Dtype, UInt32Dtype, UInt64Dtype, - Float32Dtype, - Float64Dtype, - CategoricalDtype, - PeriodDtype, - IntervalDtype, - DatetimeTZDtype, - StringDtype, - BooleanDtype, - # missing - NA, + array, + bdate_range, + date_range, + factorize, + interval_range, isna, isnull, notna, notnull, - # indexes - Index, - CategoricalIndex, - RangeIndex, - MultiIndex, - IntervalIndex, - TimedeltaIndex, - DatetimeIndex, - PeriodIndex, - IndexSlice, - # tseries - NaT, - Period, period_range, - Timedelta, + set_eng_float_format, timedelta_range, - Timestamp, - date_range, - bdate_range, - Interval, - interval_range, - DateOffset, - # conversion - to_numeric, to_datetime, + to_numeric, to_timedelta, - # misc - Flags, - Grouper, - factorize, unique, value_counts, - NamedAgg, - array, - Categorical, - set_eng_float_format, - Series, - DataFrame, ) - from pandas.core.arrays.sparse import SparseDtype - -from pandas.tseries.api import infer_freq -from pandas.tseries import offsets - from pandas.core.computation.api import eval - from pandas.core.reshape.api import ( concat, + crosstab, + cut, + get_dummies, lreshape, melt, - wide_to_long, merge, merge_asof, merge_ordered, - crosstab, pivot, pivot_table, - get_dummies, - cut, qcut, + wide_to_long, ) -from pandas import api, arrays, errors, io, plotting, testing, tseries -from pandas.util._print_versions import show_versions - -from pandas.io.api import ( - # excel +from pandas.io.api import ( # excel; parsers; pickle; pytables; sql; misc ExcelFile, ExcelWriter, - read_excel, - # parsers - read_csv, - read_fwf, - read_table, - # pickle - read_pickle, - to_pickle, - # pytables HDFStore, - read_hdf, - # sql - read_sql, - read_sql_query, - read_sql_table, - # misc read_clipboard, - read_parquet, - read_orc, + read_csv, + read_excel, read_feather, + read_fwf, read_gbq, + read_hdf, read_html, - read_xml, read_json, - read_stata, + read_orc, + read_parquet, + read_pickle, read_sas, read_spss, + read_sql, + read_sql_query, + read_sql_table, + read_stata, + read_table, + read_xml, + to_pickle, ) - from pandas.io.json import _json_normalize as json_normalize - -from pandas.util._tester import test - -# use the closest tagged version if possible -from pandas._version import get_versions +from pandas.tseries import offsets +from pandas.tseries.api import infer_freq v = get_versions() __version__ = v.get("closest-tag", v["version"]) @@ -199,7 +192,11 @@ def __getattr__(name): FutureWarning, stacklevel=2, ) - from pandas.core.api import Float64Index, Int64Index, UInt64Index + from pandas.core.api import ( + Float64Index, + Int64Index, + UInt64Index, + ) return { "Float64Index": Float64Index, diff --git a/pandas/_typing.py b/pandas/_typing.py index 5045393ad27c6..e14ae635f062c 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -71,7 +71,6 @@ else: npt: Any = None - # array-like ArrayLike = Union["ExtensionArray", np.ndarray] @@ -82,18 +81,16 @@ PythonScalar = Union[str, int, float, bool] DatetimeLikeScalar = Union["Period", "Timestamp", "Timedelta"] PandasScalar = Union["Period", "Timestamp", "Timedelta", "Interval"] -Scalar = Union[PythonScalar, PandasScalar, np.datetime64, np.timedelta64, datetime] +Scalar = Union[PythonScalar, PandasScalar, np.datetime64, np.timedelta64, + datetime] IntStrT = TypeVar("IntStrT", int, str) - # timestamp and timedelta convertible types -TimestampConvertibleTypes = Union[ - "Timestamp", datetime, np.datetime64, int, np.int64, float, str -] -TimedeltaConvertibleTypes = Union[ - "Timedelta", timedelta, np.timedelta64, int, np.int64, float, str -] +TimestampConvertibleTypes = Union["Timestamp", datetime, np.datetime64, int, + np.int64, float, str] +TimedeltaConvertibleTypes = Union["Timedelta", timedelta, np.timedelta64, int, + np.int64, float, str] Timezone = Union[str, tzinfo] # NDFrameT is stricter and ensures that the same subclass of NDFrame always is @@ -112,16 +109,12 @@ Frequency = Union[str, "DateOffset"] Axes = Collection[Any] -RandomState = Union[ - int, - ArrayLike, - np.random.Generator, - np.random.BitGenerator, - np.random.RandomState, -] +RandomState = Union[int, ArrayLike, np.random.Generator, + np.random.BitGenerator, np.random.RandomState, ] # dtypes -NpDtype = Union[str, np.dtype, type_t[Union[str, float, int, complex, bool, object]]] +NpDtype = Union[str, np.dtype, type_t[Union[str, float, int, complex, bool, + object]]] Dtype = Union["ExtensionDtype", NpDtype] AstypeArg = Union["ExtensionDtype", "npt.DTypeLike"] # DtypeArg specifies all allowable dtypes in a functions its dtype argument @@ -132,9 +125,8 @@ ConvertersArg = Dict[Hashable, Callable[[Dtype], Dtype]] # parse_dates -ParseDatesArg = Union[ - bool, List[Hashable], List[List[Hashable]], Dict[Hashable, List[Hashable]] -] +ParseDatesArg = Union[bool, List[Hashable], List[List[Hashable]], + Dict[Hashable, List[Hashable]]] # For functions like rename that convert one label to another Renamer = Union[Mapping[Hashable, Any], Callable[[Hashable], Hashable]] @@ -155,20 +147,9 @@ # types of `func` kwarg for DataFrame.aggregate and Series.aggregate AggFuncTypeBase = Union[Callable, str] AggFuncTypeDict = Dict[Hashable, Union[AggFuncTypeBase, List[AggFuncTypeBase]]] -AggFuncType = Union[ - AggFuncTypeBase, - List[AggFuncTypeBase], - AggFuncTypeDict, -] -AggObjType = Union[ - "Series", - "DataFrame", - "GroupBy", - "SeriesGroupBy", - "DataFrameGroupBy", - "BaseWindow", - "Resampler", -] +AggFuncType = Union[AggFuncTypeBase, List[AggFuncTypeBase], AggFuncTypeDict, ] +AggObjType = Union["Series", "DataFrame", "GroupBy", "SeriesGroupBy", + "DataFrameGroupBy", "BaseWindow", "Resampler", ] PythonFuncType = Callable[[Any], Any] @@ -178,6 +159,7 @@ class BaseBuffer(Protocol): + @property def mode(self) -> str: # for _get_filepath_or_buffer @@ -202,12 +184,14 @@ def tell(self) -> int: class ReadBuffer(BaseBuffer, Protocol[AnyStr_cov]): + def read(self, __n: int | None = ...) -> AnyStr_cov: # for BytesIOWrapper, gzip.GzipFile, bz2.BZ2File ... class WriteBuffer(BaseBuffer, Protocol[AnyStr_con]): + def write(self, __b: AnyStr_con) -> Any: # for gzip.GzipFile, bz2.BZ2File ... @@ -218,16 +202,19 @@ def flush(self) -> Any: class ReadPickleBuffer(ReadBuffer[bytes], Protocol): + def readline(self) -> AnyStr_cov: ... class WriteExcelBuffer(WriteBuffer[bytes], Protocol): + def truncate(self, size: int | None = ...) -> int: ... class ReadCsvBuffer(ReadBuffer[AnyStr_cov], Protocol): + def __iter__(self) -> Iterator[AnyStr_cov]: # for engine=python ... @@ -247,30 +234,25 @@ def closed(self) -> bool: # for arbitrary kwargs passed during reading/writing files StorageOptions = Optional[Dict[str, Any]] - # compression keywords and compression CompressionDict = Dict[str, Any] -CompressionOptions = Optional[ - Union[Literal["infer", "gzip", "bz2", "zip", "xz", "zstd"], CompressionDict] -] +CompressionOptions = Optional[Union[Literal["infer", "gzip", "bz2", "zip", + "xz", "zstd"], CompressionDict]] # types in DataFrameFormatter -FormattersType = Union[ - List[Callable], Tuple[Callable, ...], Mapping[Union[str, int], Callable] -] +FormattersType = Union[List[Callable], Tuple[Callable, ...], + Mapping[Union[str, int], Callable]] ColspaceType = Mapping[Hashable, Union[str, int]] FloatFormatType = Union[str, Callable, "EngFormatter"] -ColspaceArgType = Union[ - str, int, Sequence[Union[str, int]], Mapping[Hashable, Union[str, int]] -] +ColspaceArgType = Union[str, int, Sequence[Union[str, int]], + Mapping[Hashable, Union[str, int]]] # Arguments for fillna() FillnaOptions = Literal["backfill", "bfill", "ffill", "pad"] # internals -Manager = Union[ - "ArrayManager", "SingleArrayManager", "BlockManager", "SingleBlockManager" -] +Manager = Union["ArrayManager", "SingleArrayManager", "BlockManager", + "SingleBlockManager"] SingleManager = Union["SingleArrayManager", "SingleBlockManager"] Manager2D = Union["ArrayManager", "BlockManager"] @@ -291,7 +273,8 @@ def closed(self) -> bool: PositionalIndexerTuple = Tuple[PositionalIndexer, PositionalIndexer] PositionalIndexer2D = Union[PositionalIndexer, PositionalIndexerTuple] if TYPE_CHECKING: - TakeIndexer = Union[Sequence[int], Sequence[np.integer], npt.NDArray[np.integer]] + TakeIndexer = Union[Sequence[int], Sequence[np.integer], + npt.NDArray[np.integer]] else: TakeIndexer = Any diff --git a/pandas/io/parquet.py b/pandas/io/parquet.py index 6803a39e5cfa4..5e93b536eefb4 100644 --- a/pandas/io/parquet.py +++ b/pandas/io/parquet.py @@ -56,8 +56,7 @@ def get_engine(engine: str) -> BaseImpl: "pyarrow or fastparquet is required for parquet " "support.\n" "Trying to import the above resulted in these errors:" - f"{error_msgs}" - ) + f"{error_msgs}") if engine == "pyarrow": return PyArrowImpl() @@ -73,41 +72,38 @@ def _get_path_or_handle( storage_options: StorageOptions = None, mode: str = "rb", is_dir: bool = False, -) -> tuple[ - FilePath | ReadBuffer[bytes] | WriteBuffer[bytes], IOHandles[bytes] | None, Any -]: +) -> tuple[FilePath | ReadBuffer[bytes] | WriteBuffer[bytes], IOHandles[bytes] + | None, Any]: """File handling for PyArrow.""" path_or_handle = stringify_path(path) if is_fsspec_url(path_or_handle) and fs is None: fsspec = import_optional_dependency("fsspec") - fs, path_or_handle = fsspec.core.url_to_fs( - path_or_handle, **(storage_options or {}) - ) + fs, path_or_handle = fsspec.core.url_to_fs(path_or_handle, + **(storage_options or {})) elif storage_options and (not is_url(path_or_handle) or mode != "rb"): # can't write to a remote url # without making use of fsspec at the moment - raise ValueError("storage_options passed with buffer, or non-supported URL") + raise ValueError( + "storage_options passed with buffer, or non-supported URL") handles = None - if ( - not fs - and not is_dir - and isinstance(path_or_handle, str) - and not os.path.isdir(path_or_handle) - ): + if (not fs and not is_dir and isinstance(path_or_handle, str) + and not os.path.isdir(path_or_handle)): # use get_handle only when we are very certain that it is not a directory # fsspec resources can also point to directories # this branch is used for example when reading from non-fsspec URLs - handles = get_handle( - path_or_handle, mode, is_text=False, storage_options=storage_options - ) + handles = get_handle(path_or_handle, + mode, + is_text=False, + storage_options=storage_options) fs = None path_or_handle = handles.handle return path_or_handle, handles, fs class BaseImpl: + @staticmethod def validate_dataframe(df: DataFrame) -> None: @@ -116,23 +112,20 @@ def validate_dataframe(df: DataFrame) -> None: # must have value column names for all index levels (strings only) if isinstance(df.columns, MultiIndex): - if not all( - x.inferred_type in {"string", "empty"} for x in df.columns.levels - ): - raise ValueError( - """ + if not all(x.inferred_type in {"string", "empty"} + for x in df.columns.levels): + raise ValueError(""" parquet must have string column names for all values in each level of the MultiIndex - """ - ) + """) else: if df.columns.inferred_type not in {"string", "empty"}: raise ValueError("parquet must have string column names") # index level names must be strings valid_names = all( - isinstance(name, str) for name in df.index.names if name is not None - ) + isinstance(name, str) for name in df.index.names + if name is not None) if not valid_names: raise ValueError("Index level names must be strings") @@ -144,10 +137,10 @@ def read(self, path, columns=None, **kwargs) -> DataFrame: class PyArrowImpl(BaseImpl): + def __init__(self): import_optional_dependency( - "pyarrow", extra="pyarrow is required for parquet support." - ) + "pyarrow", extra="pyarrow is required for parquet support.") # import utils to register the pyarrow extension types import pandas.core.arrays._arrow_utils # noqa:F401 @@ -166,7 +159,9 @@ def write( ) -> None: self.validate_dataframe(df) - from_pandas_kwargs: dict[str, Any] = {"schema": kwargs.pop("schema", None)} + from_pandas_kwargs: dict[str, Any] = { + "schema": kwargs.pop("schema", None) + } if index is not None: from_pandas_kwargs["preserve_index"] = index @@ -179,11 +174,9 @@ def write( mode="wb", is_dir=partition_cols is not None, ) - if ( - isinstance(path_or_handle, io.BufferedWriter) - and hasattr(path_or_handle, "name") - and isinstance(path_or_handle.name, (str, bytes)) - ): + if (isinstance(path_or_handle, io.BufferedWriter) + and hasattr(path_or_handle, "name") + and isinstance(path_or_handle.name, (str, bytes))): path_or_handle = path_or_handle.name try: @@ -198,9 +191,10 @@ def write( ) else: # write to single output file - self.api.parquet.write_table( - table, path_or_handle, compression=compression, **kwargs - ) + self.api.parquet.write_table(table, + path_or_handle, + compression=compression, + **kwargs) finally: if handles is not None: handles.close() @@ -244,8 +238,8 @@ def read( ) try: result = self.api.parquet.read_table( - path_or_handle, columns=columns, **kwargs - ).to_pandas(**to_pandas_kwargs) + path_or_handle, columns=columns, + **kwargs).to_pandas(**to_pandas_kwargs) if manager == "array": result = result._as_manager("array", copy=False) return result @@ -255,12 +249,13 @@ def read( class FastParquetImpl(BaseImpl): + def __init__(self): # since pandas is a dependency of fastparquet # we need to import on first use fastparquet = import_optional_dependency( - "fastparquet", extra="fastparquet is required for parquet support." - ) + "fastparquet", + extra="fastparquet is required for parquet support.") self.api = fastparquet def write( @@ -281,8 +276,7 @@ def write( if "partition_on" in kwargs and partition_cols is not None: raise ValueError( "Cannot use both partition_on and " - "partition_cols. Use partition_cols for partitioning data" - ) + "partition_cols. Use partition_cols for partitioning data") elif "partition_on" in kwargs: partition_cols = kwargs.pop("partition_on") @@ -296,8 +290,7 @@ def write( # if filesystem is provided by fsspec, file must be opened in 'wb' mode. kwargs["open_with"] = lambda path, _: fsspec.open( - path, "wb", **(storage_options or {}) - ).open() + path, "wb", **(storage_options or {})).open() elif storage_options: raise ValueError( "storage_options passed with file object or non-fsspec file path" @@ -313,9 +306,11 @@ def write( **kwargs, ) - def read( - self, path, columns=None, storage_options: StorageOptions = None, **kwargs - ) -> DataFrame: + def read(self, + path, + columns=None, + storage_options: StorageOptions = None, + **kwargs) -> DataFrame: parquet_kwargs: dict[str, Any] = {} use_nullable_dtypes = kwargs.pop("use_nullable_dtypes", False) if Version(self.api.__version__) >= Version("0.7.1"): @@ -324,28 +319,27 @@ def read( if use_nullable_dtypes: raise ValueError( "The 'use_nullable_dtypes' argument is not supported for the " - "fastparquet engine" - ) + "fastparquet engine") path = stringify_path(path) handles = None if is_fsspec_url(path): fsspec = import_optional_dependency("fsspec") if Version(self.api.__version__) > Version("0.6.1"): - parquet_kwargs["fs"] = fsspec.open( - path, "rb", **(storage_options or {}) - ).fs + parquet_kwargs["fs"] = fsspec.open(path, "rb", + **(storage_options + or {})).fs else: parquet_kwargs["open_with"] = lambda path, _: fsspec.open( - path, "rb", **(storage_options or {}) - ).open() + path, "rb", **(storage_options or {})).open() elif isinstance(path, str) and not os.path.isdir(path): # use get_handle only when we are very certain that it is not a directory # fsspec resources can also point to directories # this branch is used for example when reading from non-fsspec URLs - handles = get_handle( - path, "rb", is_text=False, storage_options=storage_options - ) + handles = get_handle(path, + "rb", + is_text=False, + storage_options=storage_options) path = handles.handle parquet_file = self.api.ParquetFile(path, **parquet_kwargs) @@ -421,7 +415,8 @@ def to_parquet( partition_cols = [partition_cols] impl = get_engine(engine) - path_or_buf: FilePath | WriteBuffer[bytes] = io.BytesIO() if path is None else path + path_or_buf: FilePath | WriteBuffer[bytes] = io.BytesIO( + ) if path is None else path impl.write( df, diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index 64f090a5105c6..023c10205bc3f 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -36,6 +36,7 @@ @td.skip_if_no_mpl class TestDataFramePlots(TestPlotBase): + @pytest.mark.slow def test_plot(self): df = tm.makeTimeDataFrame() @@ -67,7 +68,8 @@ def test_plot(self): with pytest.raises(AttributeError, match=msg): df.plot.line(blarg=True) - df = DataFrame(np.random.rand(10, 3), index=list(string.ascii_letters[:10])) + df = DataFrame(np.random.rand(10, 3), + index=list(string.ascii_letters[:10])) ax = _check_plot_works(df.plot, use_index=True) self._check_ticks_props(ax, xrot=0) @@ -76,7 +78,10 @@ def test_plot(self): _check_plot_works(df.plot, xticks=[1, 5, 10]) _check_plot_works(df.plot, ylim=(-100, 100), xlim=(-100, 100)) - _check_plot_works(df.plot, default_axes=True, subplots=True, title="blah") + _check_plot_works(df.plot, + default_axes=True, + subplots=True, + title="blah") # We have to redo it here because _check_plot_works does two plots, # once without an ax kwarg and once with an ax kwarg and the new sharex @@ -100,7 +105,8 @@ def test_plot(self): _check_plot_works(df.plot, title="blah") tuples = zip(string.ascii_letters[:10], range(10)) - df = DataFrame(np.random.rand(10, 3), index=MultiIndex.from_tuples(tuples)) + df = DataFrame(np.random.rand(10, 3), + index=MultiIndex.from_tuples(tuples)) ax = _check_plot_works(df.plot, use_index=True) self._check_ticks_props(ax, xrot=0) @@ -118,10 +124,12 @@ def test_plot(self): ], names=["i0", "i1"], ) - columns = MultiIndex.from_tuples( - [("bar", "\u0394"), ("bar", "\u0395")], names=["c0", "c1"] - ) - df = DataFrame(np.random.randint(0, 10, (8, 2)), columns=columns, index=index) + columns = MultiIndex.from_tuples([("bar", "\u0394"), + ("bar", "\u0395")], + names=["c0", "c1"]) + df = DataFrame(np.random.randint(0, 10, (8, 2)), + columns=columns, + index=index) _check_plot_works(df.plot, title="\u03A3") # GH 6951 @@ -156,7 +164,8 @@ def test_nullable_int_plot(self): _check_plot_works(df.plot, x="A", y="B") _check_plot_works(df[["A", "B"]].plot, x="A", y="B") - _check_plot_works(df[["C", "A"]].plot, x="C", y="A") # nullable value on x-axis + _check_plot_works(df[["C", "A"]].plot, x="C", + y="A") # nullable value on x-axis _check_plot_works(df[["A", "C"]].plot, x="A", y="C") _check_plot_works(df[["B", "C"]].plot, x="B", y="C") _check_plot_works(df[["A", "D"]].plot, x="A", y="D") @@ -217,14 +226,16 @@ def test_plot_xy(self): # figsize and title ax = df.plot(x=1, y=2, title="Test", figsize=(16, 8)) self._check_text_labels(ax.title, "Test") - self._check_axes_shape(ax, axes_num=1, layout=(1, 1), figsize=(16.0, 8.0)) + self._check_axes_shape(ax, + axes_num=1, + layout=(1, 1), + figsize=(16.0, 8.0)) # columns.inferred_type == 'mixed' # TODO add MultiIndex test - @pytest.mark.parametrize( - "input_log, expected_log", [(True, "log"), ("sym", "symlog")] - ) + @pytest.mark.parametrize("input_log, expected_log", [(True, "log"), + ("sym", "symlog")]) def test_logscales(self, input_log, expected_log): df = DataFrame({"a": np.arange(100)}, index=np.arange(100)) @@ -302,9 +313,9 @@ def test_period_compat(self): tm.close() def test_unsorted_index(self): - df = DataFrame( - {"y": np.arange(100)}, index=np.arange(99, -1, -1), dtype=np.int64 - ) + df = DataFrame({"y": np.arange(100)}, + index=np.arange(99, -1, -1), + dtype=np.int64) ax = df.plot() lines = ax.get_lines()[0] rs = lines.get_xydata() @@ -337,7 +348,10 @@ def test_unsorted_index_lims(self): assert xmin <= np.nanmin(lines[0].get_data()[0]) assert xmax >= np.nanmax(lines[0].get_data()[0]) - df = DataFrame({"y": [0.0, 1.0, 2.0, 3.0], "z": [91.0, 90.0, 93.0, 92.0]}) + df = DataFrame({ + "y": [0.0, 1.0, 2.0, 3.0], + "z": [91.0, 90.0, 93.0, 92.0] + }) ax = df.plot(x="z", y="y") xmin, xmax = ax.get_xlim() lines = ax.get_lines() @@ -369,14 +383,12 @@ def test_line_area_stacked(self, kind): df = DataFrame(np.random.rand(6, 4), columns=["w", "x", "y", "z"]) neg_df = -df # each column has either positive or negative value - sep_df = DataFrame( - { - "w": np.random.rand(6), - "x": np.random.rand(6), - "y": -np.random.rand(6), - "z": -np.random.rand(6), - } - ) + sep_df = DataFrame({ + "w": np.random.rand(6), + "x": np.random.rand(6), + "y": -np.random.rand(6), + "z": -np.random.rand(6), + }) # each column has positive-negative mixed value mixed_df = DataFrame( np.random.randn(6, 4), @@ -401,8 +413,7 @@ def test_line_area_stacked(self, kind): msg = ( "When stacked is True, each column must be either all positive or " "all negative. Column 'w' contains both positive and negative " - "values" - ) + "values") with pytest.raises(ValueError, match=msg): mixed_df.plot(stacked=True) @@ -415,7 +426,11 @@ def test_line_area_nan_df(self): values1 = [1, 2, np.nan, 3] values2 = [3, np.nan, 2, 1] df = DataFrame({"a": values1, "b": values2}) - tdf = DataFrame({"a": values1, "b": values2}, index=tm.makeDateIndex(k=4)) + tdf = DataFrame({ + "a": values1, + "b": values2 + }, + index=tm.makeDateIndex(k=4)) for d in [df, tdf]: ax = _check_plot_works(d.plot) @@ -428,23 +443,23 @@ def test_line_area_nan_df(self): exp = np.array([3, 2, 1], dtype=np.float64) tm.assert_numpy_array_equal(np.delete(masked2.data, 1), exp) - tm.assert_numpy_array_equal( - masked1.mask, np.array([False, False, True, False]) - ) - tm.assert_numpy_array_equal( - masked2.mask, np.array([False, True, False, False]) - ) + tm.assert_numpy_array_equal(masked1.mask, + np.array([False, False, True, False])) + tm.assert_numpy_array_equal(masked2.mask, + np.array([False, True, False, False])) expected1 = np.array([1, 2, 0, 3], dtype=np.float64) expected2 = np.array([3, 0, 2, 1], dtype=np.float64) ax = _check_plot_works(d.plot, stacked=True) tm.assert_numpy_array_equal(ax.lines[0].get_ydata(), expected1) - tm.assert_numpy_array_equal(ax.lines[1].get_ydata(), expected1 + expected2) + tm.assert_numpy_array_equal(ax.lines[1].get_ydata(), + expected1 + expected2) ax = _check_plot_works(d.plot.area) tm.assert_numpy_array_equal(ax.lines[0].get_ydata(), expected1) - tm.assert_numpy_array_equal(ax.lines[1].get_ydata(), expected1 + expected2) + tm.assert_numpy_array_equal(ax.lines[1].get_ydata(), + expected1 + expected2) ax = _check_plot_works(d.plot.area, stacked=False) tm.assert_numpy_array_equal(ax.lines[0].get_ydata(), expected1) @@ -594,7 +609,11 @@ def test_bar_bottom_left(self): assert result == [1] * 5 def test_bar_nan(self): - df = DataFrame({"A": [10, np.nan, 20], "B": [5, 10, 20], "C": [1, 2, 3]}) + df = DataFrame({ + "A": [10, np.nan, 20], + "B": [5, 10, 20], + "C": [1, 2, 3] + }) ax = df.plot.bar() expected = [10, 0, 20, 5, 10, 20, 1, 2, 3] result = [p.get_height() for p in ax.patches] @@ -647,10 +666,12 @@ def test_plot_scatter(self): _check_plot_works(df.plot.scatter, x="x", y="y") _check_plot_works(df.plot.scatter, x=1, y=2) - msg = re.escape("scatter() missing 1 required positional argument: 'y'") + msg = re.escape( + "scatter() missing 1 required positional argument: 'y'") with pytest.raises(TypeError, match=msg): df.plot.scatter(x="x") - msg = re.escape("scatter() missing 1 required positional argument: 'x'") + msg = re.escape( + "scatter() missing 1 required positional argument: 'x'") with pytest.raises(TypeError, match=msg): df.plot.scatter(y="y") @@ -691,7 +712,8 @@ def test_scatterplot_object_data(self): @pytest.mark.parametrize("ordered", [True, False]) @pytest.mark.parametrize( "categories", - (["setosa", "versicolor", "virginica"], ["versicolor", "virginica", "setosa"]), + (["setosa", "versicolor", "virginica" + ], ["versicolor", "virginica", "setosa"]), ) def test_scatterplot_color_by_categorical(self, ordered, categories): df = DataFrame( @@ -704,7 +726,7 @@ def test_scatterplot_color_by_categorical(self, ordered, categories): categories=categories, ) ax = df.plot.scatter(x=0, y=1, c="species") - (colorbar_collection,) = ax.collections + (colorbar_collection, ) = ax.collections colorbar = colorbar_collection.colorbar expected_ticks = np.array([0.5, 1.5, 2.5]) @@ -716,13 +738,19 @@ def test_scatterplot_color_by_categorical(self, ordered, categories): tm.assert_numpy_array_equal(result_boundaries, expected_boundaries) expected_yticklabels = categories - result_yticklabels = [i.get_text() for i in colorbar.ax.get_ymajorticklabels()] - assert all(i == j for i, j in zip(result_yticklabels, expected_yticklabels)) + result_yticklabels = [ + i.get_text() for i in colorbar.ax.get_ymajorticklabels() + ] + assert all(i == j + for i, j in zip(result_yticklabels, expected_yticklabels)) @pytest.mark.parametrize("x, y", [("x", "y"), ("y", "x"), ("y", "y")]) def test_plot_scatter_with_categorical_data(self, x, y): # after fixing GH 18755, should be able to plot categorical data - df = DataFrame({"x": [1, 2, 3, 4], "y": pd.Categorical(["a", "b", "a", "c"])}) + df = DataFrame({ + "x": [1, 2, 3, 4], + "y": pd.Categorical(["a", "b", "a", "c"]) + }) _check_plot_works(df.plot.scatter, x=x, y=y) @@ -735,7 +763,10 @@ def test_plot_scatter_with_c(self): columns=["x", "y", "z", "four"], ) - axes = [df.plot.scatter(x="x", y="y", c="z"), df.plot.scatter(x=0, y=1, c=2)] + axes = [ + df.plot.scatter(x="x", y="y", c="z"), + df.plot.scatter(x=0, y=1, c=2) + ] for ax in axes: # default to Greys assert ax.collections[0].cmap.name == "Greys" @@ -773,7 +804,8 @@ def test_plot_scatter_with_c(self): # identical to the values we supplied, normally we'd be on shaky ground # comparing floats for equality but here we expect them to be # identical. - tm.assert_numpy_array_equal(ax.collections[0].get_facecolor(), rgba_array) + tm.assert_numpy_array_equal(ax.collections[0].get_facecolor(), + rgba_array) # we don't test the colors of the faces in this next plot because they # are dependent on the spring colormap, which may change its colors # later. @@ -782,16 +814,19 @@ def test_plot_scatter_with_c(self): def test_plot_scatter_with_s(self): # this refers to GH 32904 - df = DataFrame(np.random.random((10, 3)) * 100, columns=["a", "b", "c"]) + df = DataFrame(np.random.random((10, 3)) * 100, + columns=["a", "b", "c"]) ax = df.plot.scatter(x="a", y="b", s="c") - tm.assert_numpy_array_equal(df["c"].values, right=ax.collections[0].get_sizes()) + tm.assert_numpy_array_equal(df["c"].values, + right=ax.collections[0].get_sizes()) def test_plot_scatter_with_norm(self): # added while fixing GH 45809 import matplotlib as mpl - df = DataFrame(np.random.random((10, 3)) * 100, columns=["a", "b", "c"]) + df = DataFrame(np.random.random((10, 3)) * 100, + columns=["a", "b", "c"]) norm = mpl.colors.LogNorm() ax = df.plot.scatter(x="a", y="b", c="c", norm=norm) assert ax.collections[0].norm is norm @@ -800,7 +835,8 @@ def test_plot_scatter_without_norm(self): # added while fixing GH 45809 import matplotlib as mpl - df = DataFrame(np.random.random((10, 3)) * 100, columns=["a", "b", "c"]) + df = DataFrame(np.random.random((10, 3)) * 100, + columns=["a", "b", "c"]) ax = df.plot.scatter(x="a", y="b", c="c") plot_norm = ax.collections[0].norm color_min_max = (df.c.min(), df.c.max()) @@ -849,9 +885,9 @@ def test_boxplot(self, hist_df): ax = _check_plot_works(df.plot.box) self._check_text_labels(ax.get_xticklabels(), labels) - tm.assert_numpy_array_equal( - ax.xaxis.get_ticklocs(), np.arange(1, len(numeric_cols) + 1) - ) + tm.assert_numpy_array_equal(ax.xaxis.get_ticklocs(), + np.arange(1, + len(numeric_cols) + 1)) assert len(ax.lines) == 7 * len(numeric_cols) tm.close() @@ -989,8 +1025,8 @@ def test_hist_df(self): self._check_ticks_props(axes, xrot=0, yrot=50, ylabelsize=8) @pytest.mark.parametrize( - "weights", [0.1 * np.ones(shape=(100,)), 0.1 * np.ones(shape=(100, 2))] - ) + "weights", + [0.1 * np.ones(shape=(100, )), 0.1 * np.ones(shape=(100, 2))]) def test_hist_weights(self, weights): # GH 33173 np.random.seed(0) @@ -999,13 +1035,18 @@ def test_hist_weights(self, weights): ax1 = _check_plot_works(df.plot, kind="hist", weights=weights) ax2 = _check_plot_works(df.plot, kind="hist") - patch_height_with_weights = [patch.get_height() for patch in ax1.patches] + patch_height_with_weights = [ + patch.get_height() for patch in ax1.patches + ] # original heights with no weights, and we manually multiply with example # weights, so after multiplication, they should be almost same - expected_patch_height = [0.1 * patch.get_height() for patch in ax2.patches] + expected_patch_height = [ + 0.1 * patch.get_height() for patch in ax2.patches + ] - tm.assert_almost_equal(patch_height_with_weights, expected_patch_height) + tm.assert_almost_equal(patch_height_with_weights, + expected_patch_height) def _check_box_coord( self, @@ -1022,35 +1063,49 @@ def _check_box_coord( # dtype is depending on above values, no need to check if expected_y is not None: - tm.assert_numpy_array_equal(result_y, expected_y, check_dtype=False) + tm.assert_numpy_array_equal(result_y, + expected_y, + check_dtype=False) if expected_h is not None: - tm.assert_numpy_array_equal(result_height, expected_h, check_dtype=False) + tm.assert_numpy_array_equal(result_height, + expected_h, + check_dtype=False) if expected_x is not None: - tm.assert_numpy_array_equal(result_x, expected_x, check_dtype=False) + tm.assert_numpy_array_equal(result_x, + expected_x, + check_dtype=False) if expected_w is not None: - tm.assert_numpy_array_equal(result_width, expected_w, check_dtype=False) + tm.assert_numpy_array_equal(result_width, + expected_w, + check_dtype=False) def test_hist_df_coord(self): normal_df = DataFrame( { - "A": np.repeat(np.array([1, 2, 3, 4, 5]), np.array([10, 9, 8, 7, 6])), - "B": np.repeat(np.array([1, 2, 3, 4, 5]), np.array([8, 8, 8, 8, 8])), - "C": np.repeat(np.array([1, 2, 3, 4, 5]), np.array([6, 7, 8, 9, 10])), + "A": + np.repeat(np.array([1, 2, 3, 4, 5]), np.array([10, 9, 8, 7, 6 + ])), + "B": + np.repeat(np.array([1, 2, 3, 4, 5]), np.array([8, 8, 8, 8, 8 + ])), + "C": + np.repeat(np.array([1, 2, 3, 4, 5]), np.array([6, 7, 8, 9, 10 + ])), }, columns=["A", "B", "C"], ) nan_df = DataFrame( { - "A": np.repeat( - np.array([np.nan, 1, 2, 3, 4, 5]), np.array([3, 10, 9, 8, 7, 6]) - ), - "B": np.repeat( - np.array([1, np.nan, 2, 3, 4, 5]), np.array([8, 3, 8, 8, 8, 8]) - ), - "C": np.repeat( - np.array([1, 2, 3, np.nan, 4, 5]), np.array([6, 7, 8, 3, 9, 10]) - ), + "A": + np.repeat(np.array([np.nan, 1, 2, 3, 4, 5]), + np.array([3, 10, 9, 8, 7, 6])), + "B": + np.repeat(np.array([1, np.nan, 2, 3, 4, 5]), + np.array([8, 3, 8, 8, 8, 8])), + "C": + np.repeat(np.array([1, 2, 3, np.nan, 4, 5]), + np.array([6, 7, 8, 3, 9, 10])), }, columns=["A", "B", "C"], ) @@ -1142,9 +1197,10 @@ def test_hist_df_coord(self): expected_w=np.array([6, 7, 8, 9, 10]), ) - axes = df.plot.hist( - bins=5, stacked=True, subplots=True, orientation="horizontal" - ) + axes = df.plot.hist(bins=5, + stacked=True, + subplots=True, + orientation="horizontal") self._check_box_coord( axes[0].patches, expected_x=np.array([0, 0, 0, 0, 0]), @@ -1172,15 +1228,22 @@ def test_style_by_column(self): df = DataFrame(np.random.randn(100, 3)) for markers in [ - {0: "^", 1: "+", 2: "o"}, - {0: "^", 1: "+"}, + { + 0: "^", + 1: "+", + 2: "o" + }, + { + 0: "^", + 1: "+" + }, ["^", "+", "o"], ["^", "+"], ]: fig.clf() fig.add_subplot(111) ax = df.plot(style=markers) - for idx, line in enumerate(ax.get_lines()[: len(markers)]): + for idx, line in enumerate(ax.get_lines()[:len(markers)]): assert line.get_marker() == markers[idx] def test_line_label_none(self): @@ -1211,7 +1274,9 @@ def test_specified_props_kwd_plot_box(self, props, expected): def test_unordered_ts(self): df = DataFrame( np.array([3.0, 2.0, 1.0]), - index=[date(2012, 10, 1), date(2012, 9, 1), date(2012, 8, 1)], + index=[date(2012, 10, 1), + date(2012, 9, 1), + date(2012, 8, 1)], columns=["test"], ) ax = df.plot() @@ -1304,7 +1369,8 @@ def test_y_listlike(self, x, y, lbl, colors): assert len(ax.lines) == len(y) self._check_colors(ax.get_lines(), linecolors=colors) - @pytest.mark.parametrize("x,y,colnames", [(0, 1, ["A", "B"]), (1, 0, [0, 1])]) + @pytest.mark.parametrize("x,y,colnames", [(0, 1, ["A", "B"]), + (1, 0, [0, 1])]) def test_xy_args_integer(self, x, y, colnames): # GH 20056: tests integer args for xy and checks col names df = DataFrame({"A": [1, 2], "B": [3, 4]}) @@ -1312,13 +1378,11 @@ def test_xy_args_integer(self, x, y, colnames): _check_plot_works(df.plot, x=x, y=y) def test_hexbin_basic(self): - df = DataFrame( - { - "A": np.random.uniform(size=20), - "B": np.random.uniform(size=20), - "C": np.arange(20) + np.random.uniform(size=20), - } - ) + df = DataFrame({ + "A": np.random.uniform(size=20), + "B": np.random.uniform(size=20), + "C": np.arange(20) + np.random.uniform(size=20), + }) ax = df.plot.hexbin(x="A", y="B", gridsize=10) # TODO: need better way to test. This just does existence. @@ -1333,13 +1397,11 @@ def test_hexbin_basic(self): self._check_axes_shape(axes, axes_num=1, layout=(1, 1)) def test_hexbin_with_c(self): - df = DataFrame( - { - "A": np.random.uniform(size=20), - "B": np.random.uniform(size=20), - "C": np.arange(20) + np.random.uniform(size=20), - } - ) + df = DataFrame({ + "A": np.random.uniform(size=20), + "B": np.random.uniform(size=20), + "C": np.arange(20) + np.random.uniform(size=20), + }) ax = df.plot.hexbin(x="A", y="B", C="C") assert len(ax.collections) == 1 @@ -1351,18 +1413,20 @@ def test_hexbin_with_c(self): "kwargs, expected", [ ({}, "BuGn"), # default cmap - ({"colormap": "cubehelix"}, "cubehelix"), - ({"cmap": "YlGn"}, "YlGn"), + ({ + "colormap": "cubehelix" + }, "cubehelix"), + ({ + "cmap": "YlGn" + }, "YlGn"), ], ) def test_hexbin_cmap(self, kwargs, expected): - df = DataFrame( - { - "A": np.random.uniform(size=20), - "B": np.random.uniform(size=20), - "C": np.arange(20) + np.random.uniform(size=20), - } - ) + df = DataFrame({ + "A": np.random.uniform(size=20), + "B": np.random.uniform(size=20), + "C": np.arange(20) + np.random.uniform(size=20), + }) ax = df.plot.hexbin(x="A", y="B", **kwargs) assert ax.collections[0].cmap.name == expected @@ -1435,7 +1499,7 @@ def test_pie_df_nan(self): # NaN's not included in legend with subplots # see https://github.com/pandas-dev/pandas/issues/8390 result_labels = [x.get_text() for x in ax.get_legend().get_texts()] - expected_labels = base_expected[:i] + base_expected[i + 1 :] + expected_labels = base_expected[:i] + base_expected[i + 1:] assert result_labels == expected_labels @pytest.mark.slow @@ -1455,9 +1519,11 @@ def test_errorbar_plot(self): ax = _check_plot_works(df.plot, yerr=df_err, loglog=True) self._check_has_errorbars(ax, xerr=0, yerr=2) - ax = _check_plot_works( - (df + 1).plot, yerr=df_err, xerr=df_err, kind="bar", log=True - ) + ax = _check_plot_works((df + 1).plot, + yerr=df_err, + xerr=df_err, + kind="bar", + log=True) self._check_has_errorbars(ax, xerr=2, yerr=2) # yerr is raw error values @@ -1502,7 +1568,10 @@ def test_errorbar_plot_different_kinds(self, kind): ax = _check_plot_works(df.plot, yerr=df_err, xerr=df_err, kind=kind) self._check_has_errorbars(ax, xerr=2, yerr=2) - ax = _check_plot_works(df.plot, yerr=df_err["x"], xerr=df_err["x"], kind=kind) + ax = _check_plot_works(df.plot, + yerr=df_err["x"], + xerr=df_err["x"], + kind=kind) self._check_has_errorbars(ax, xerr=2, yerr=2) ax = _check_plot_works(df.plot, xerr=0.2, yerr=0.2, kind=kind) @@ -1525,7 +1594,8 @@ def test_errorbar_plot_iterator(self): df = DataFrame(d) # yerr is iterator - ax = _check_plot_works(df.plot, yerr=itertools.repeat(0.1, len(df))) + ax = _check_plot_works(df.plot, + yerr=itertools.repeat(0.1, len(df))) self._check_has_errorbars(ax, xerr=0, yerr=2) def test_errorbar_with_integer_column_names(self): @@ -1616,7 +1686,8 @@ def test_errorbar_asymmetrical(self): tm.close() def test_table(self): - df = DataFrame(np.random.rand(10, 3), index=list(string.ascii_letters[:10])) + df = DataFrame(np.random.rand(10, 3), + index=list(string.ascii_letters[:10])) _check_plot_works(df.plot, table=True) _check_plot_works(df.plot, table=df) @@ -1628,10 +1699,12 @@ def test_table(self): assert len(ax.tables) == 1 def test_errorbar_scatter(self): - df = DataFrame(np.random.randn(5, 2), index=range(5), columns=["x", "y"]) - df_err = DataFrame( - np.random.randn(5, 2) / 5, index=range(5), columns=["x", "y"] - ) + df = DataFrame(np.random.randn(5, 2), + index=range(5), + columns=["x", "y"]) + df_err = DataFrame(np.random.randn(5, 2) / 5, + index=range(5), + columns=["x", "y"]) ax = _check_plot_works(df.plot.scatter, x="x", y="y") self._check_has_errorbars(ax, xerr=0, yerr=0) @@ -1640,24 +1713,31 @@ def test_errorbar_scatter(self): ax = _check_plot_works(df.plot.scatter, x="x", y="y", yerr=df_err) self._check_has_errorbars(ax, xerr=0, yerr=1) - ax = _check_plot_works(df.plot.scatter, x="x", y="y", xerr=df_err, yerr=df_err) + ax = _check_plot_works(df.plot.scatter, + x="x", + y="y", + xerr=df_err, + yerr=df_err) self._check_has_errorbars(ax, xerr=1, yerr=1) def _check_errorbar_color(containers, expected, has_err="has_xerr"): lines = [] - errs = [c.lines for c in ax.containers if getattr(c, has_err, False)][0] + errs = [ + c.lines for c in ax.containers if getattr(c, has_err, False) + ][0] for el in errs: if is_list_like(el): lines.extend(el) else: lines.append(el) err_lines = [x for x in lines if x in ax.collections] - self._check_colors( - err_lines, linecolors=np.array([expected] * len(err_lines)) - ) + self._check_colors(err_lines, + linecolors=np.array([expected] * + len(err_lines))) # GH 8081 - df = DataFrame(np.random.randn(10, 5), columns=["a", "b", "c", "d", "e"]) + df = DataFrame(np.random.randn(10, 5), + columns=["a", "b", "c", "d", "e"]) ax = df.plot.scatter(x="a", y="b", xerr="d", yerr="e", c="red") self._check_has_errorbars(ax, xerr=1, yerr=1) _check_errorbar_color(ax.containers, "red", has_err="has_xerr") @@ -1676,14 +1756,12 @@ def test_sharex_and_ax(self): plt.close("all") gs, axes = _generate_4_axes_via_gridspec() - df = DataFrame( - { - "a": [1, 2, 3, 4, 5, 6], - "b": [1, 2, 3, 4, 5, 6], - "c": [1, 2, 3, 4, 5, 6], - "d": [1, 2, 3, 4, 5, 6], - } - ) + df = DataFrame({ + "a": [1, 2, 3, 4, 5, 6], + "b": [1, 2, 3, 4, 5, 6], + "c": [1, 2, 3, 4, 5, 6], + "d": [1, 2, 3, 4, 5, 6], + }) def _check(axes): for ax in axes: @@ -1691,10 +1769,12 @@ def _check(axes): self._check_visible(ax.get_yticklabels(), visible=True) for ax in [axes[0], axes[2]]: self._check_visible(ax.get_xticklabels(), visible=False) - self._check_visible(ax.get_xticklabels(minor=True), visible=False) + self._check_visible(ax.get_xticklabels(minor=True), + visible=False) for ax in [axes[1], axes[3]]: self._check_visible(ax.get_xticklabels(), visible=True) - self._check_visible(ax.get_xticklabels(minor=True), visible=True) + self._check_visible(ax.get_xticklabels(minor=True), + visible=True) for ax in axes: df.plot(x="a", y="b", title="title", ax=ax, sharex=True) @@ -1729,20 +1809,19 @@ def test_sharey_and_ax(self): gs, axes = _generate_4_axes_via_gridspec() - df = DataFrame( - { - "a": [1, 2, 3, 4, 5, 6], - "b": [1, 2, 3, 4, 5, 6], - "c": [1, 2, 3, 4, 5, 6], - "d": [1, 2, 3, 4, 5, 6], - } - ) + df = DataFrame({ + "a": [1, 2, 3, 4, 5, 6], + "b": [1, 2, 3, 4, 5, 6], + "c": [1, 2, 3, 4, 5, 6], + "d": [1, 2, 3, 4, 5, 6], + }) def _check(axes): for ax in axes: assert len(ax.lines) == 1 self._check_visible(ax.get_xticklabels(), visible=True) - self._check_visible(ax.get_xticklabels(minor=True), visible=True) + self._check_visible(ax.get_xticklabels(minor=True), + visible=True) for ax in [axes[0], axes[1]]: self._check_visible(ax.get_yticklabels(), visible=True) for ax in [axes[2], axes[3]]: @@ -1785,13 +1864,11 @@ def test_memory_leak(self): args = {} if kind in ["hexbin", "scatter", "pie"]: - df = DataFrame( - { - "A": np.random.uniform(size=20), - "B": np.random.uniform(size=20), - "C": np.arange(20) + np.random.uniform(size=20), - } - ) + df = DataFrame({ + "A": np.random.uniform(size=20), + "B": np.random.uniform(size=20), + "C": np.arange(20) + np.random.uniform(size=20), + }) args = {"x": "A", "y": "B"} elif kind == "area": df = tm.makeTimeDataFrame().abs() @@ -1818,9 +1895,12 @@ def test_df_gridspec_patterns(self): import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt - ts = Series(np.random.randn(10), index=date_range("1/1/2000", periods=10)) + ts = Series(np.random.randn(10), + index=date_range("1/1/2000", periods=10)) - df = DataFrame(np.random.randn(10, 2), index=ts.index, columns=list("AB")) + df = DataFrame(np.random.randn(10, 2), + index=ts.index, + columns=list("AB")) def _get_vertical_grid(): gs = gridspec.GridSpec(3, 1) @@ -1844,7 +1924,8 @@ def _get_horizontal_grid(): for ax in [ax1, ax2]: self._check_visible(ax.get_yticklabels(), visible=True) self._check_visible(ax.get_xticklabels(), visible=True) - self._check_visible(ax.get_xticklabels(minor=True), visible=True) + self._check_visible(ax.get_xticklabels(minor=True), + visible=True) tm.close() # subplots=True @@ -1855,13 +1936,17 @@ def _get_horizontal_grid(): for ax in axes: self._check_visible(ax.get_yticklabels(), visible=True) self._check_visible(ax.get_xticklabels(), visible=True) - self._check_visible(ax.get_xticklabels(minor=True), visible=True) + self._check_visible(ax.get_xticklabels(minor=True), + visible=True) tm.close() # vertical / subplots / sharex=True / sharey=True ax1, ax2 = _get_vertical_grid() with tm.assert_produces_warning(UserWarning): - axes = df.plot(subplots=True, ax=[ax1, ax2], sharex=True, sharey=True) + axes = df.plot(subplots=True, + ax=[ax1, ax2], + sharex=True, + sharey=True) assert len(axes[0].lines) == 1 assert len(axes[1].lines) == 1 for ax in [ax1, ax2]: @@ -1877,7 +1962,10 @@ def _get_horizontal_grid(): # horizontal / subplots / sharex=True / sharey=True ax1, ax2 = _get_horizontal_grid() with tm.assert_produces_warning(UserWarning): - axes = df.plot(subplots=True, ax=[ax1, ax2], sharex=True, sharey=True) + axes = df.plot(subplots=True, + ax=[ax1, ax2], + sharex=True, + sharey=True) assert len(axes[0].lines) == 1 assert len(axes[1].lines) == 1 self._check_visible(axes[0].get_yticklabels(), visible=True) @@ -1900,7 +1988,9 @@ def _get_boxed_grid(): return ax1, ax2, ax3, ax4 axes = _get_boxed_grid() - df = DataFrame(np.random.randn(10, 4), index=ts.index, columns=list("ABCD")) + df = DataFrame(np.random.randn(10, 4), + index=ts.index, + columns=list("ABCD")) axes = df.plot(subplots=True, ax=axes) for ax in axes: assert len(ax.lines) == 1 @@ -1931,9 +2021,15 @@ def _get_boxed_grid(): def test_df_grid_settings(self): # Make sure plot defaults to rcParams['axes.grid'] setting, GH 9792 self._check_grid_settings( - DataFrame({"a": [1, 2, 3], "b": [2, 3, 4]}), + DataFrame({ + "a": [1, 2, 3], + "b": [2, 3, 4] + }), plotting.PlotAccessor._dataframe_kinds, - kws={"x": "a", "y": "b"}, + kws={ + "x": "a", + "y": "b" + }, ) def test_plain_axes(self): @@ -1970,11 +2066,8 @@ def test_plain_axes(self): @pytest.mark.parametrize("method", ["line", "barh", "bar"]) def test_secondary_axis_font_size(self, method): # GH: 12565 - df = ( - DataFrame(np.random.randn(15, 2), columns=list("AB")) - .assign(C=lambda df: df.B.cumsum()) - .assign(D=lambda df: df.C * 1.1) - ) + df = (DataFrame(np.random.randn(15, 2), columns=list("AB")).assign( + C=lambda df: df.B.cumsum()).assign(D=lambda df: df.C * 1.1)) fontsize = 20 sy = ["C", "D"] @@ -1986,13 +2079,11 @@ def test_secondary_axis_font_size(self, method): def test_x_string_values_ticks(self): # Test if string plot index have a fixed xtick position # GH: 7612, GH: 22334 - df = DataFrame( - { - "sales": [3, 2, 3], - "visits": [20, 42, 28], - "day": ["Monday", "Tuesday", "Wednesday"], - } - ) + df = DataFrame({ + "sales": [3, 2, 3], + "visits": [20, 42, 28], + "day": ["Monday", "Tuesday", "Wednesday"], + }) ax = df.plot.area(x="day") ax.set_xlim(-1, 3) xticklabels = [t.get_text() for t in ax.get_xticklabels()] @@ -2079,9 +2170,8 @@ def test_plot_no_numeric_data(self): ], ) @pytest.mark.parametrize("kind", ["line", "area", "bar"]) - def test_xlabel_ylabel_dataframe_single_plot( - self, kind, index_name, old_label, new_label - ): + def test_xlabel_ylabel_dataframe_single_plot(self, kind, index_name, + old_label, new_label): # GH 9093 df = DataFrame([[1, 2], [2, 5]], columns=["Type A", "Type B"]) df.index.name = index_name