From 56807fee088641e25ddc3d91424c2267406eca64 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Thu, 2 Mar 2023 11:06:16 +0300 Subject: [PATCH 01/11] Removed PLR5501 --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 192950f3dfb49..73bc3a5568224 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -271,8 +271,6 @@ ignore = [ "PLR2004", # Outer loop variable overwritten by inner assignment "PLW2901", - # Consider `elif` instead of `else` then `if` to remove indendation level - "PLR5501", ] exclude = [ @@ -288,6 +286,8 @@ exclude = [ [tool.ruff.per-file-ignores] # relative imports allowed for asv_bench "asv_bench/*" = ["TID"] +# to be enabled gradually +"pandas/core/*" = ["PLR5501"] [tool.pylint.messages_control] max-line-length = 88 From c93b21a411421f3bdd45a922679b1386e3aad83b Mon Sep 17 00:00:00 2001 From: Ahmad Date: Thu, 2 Mar 2023 12:14:26 +0300 Subject: [PATCH 02/11] 7 elif changed --- pandas/io/formats/format.py | 8 +++--- pandas/io/formats/info.py | 14 +++++----- pandas/io/formats/printing.py | 8 +++--- pandas/io/parquet.py | 5 ++-- pandas/io/parsers/c_parser_wrapper.py | 38 +++++++++++++-------------- pandas/io/parsers/python_parser.py | 28 ++++++++++---------- pandas/io/pytables.py | 6 ++--- 7 files changed, 53 insertions(+), 54 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index f9b081afcd045..c2598522fb1f3 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -2103,11 +2103,11 @@ def __call__(self, num: float) -> str: if self.use_eng_prefix: prefix = self.ENG_PREFIXES[int_pow10] + + elif int_pow10 < 0: + prefix = f"E-{-int_pow10:02d}" else: - if int_pow10 < 0: - prefix = f"E-{-int_pow10:02d}" - else: - prefix = f"E+{int_pow10:02d}" + prefix = f"E+{int_pow10:02d}" mant = sign * dnum / (10**pow10) diff --git a/pandas/io/formats/info.py b/pandas/io/formats/info.py index d826c0a148ebe..ab24347a518dd 100644 --- a/pandas/io/formats/info.py +++ b/pandas/io/formats/info.py @@ -660,14 +660,14 @@ def _create_table_builder(self) -> DataFrameTableBuilder: ) elif self.verbose is False: # specifically set to False, not necessarily None return DataFrameTableBuilderNonVerbose(info=self.info) + + elif self.exceeds_info_cols: + return DataFrameTableBuilderNonVerbose(info=self.info) else: - if self.exceeds_info_cols: - return DataFrameTableBuilderNonVerbose(info=self.info) - else: - return DataFrameTableBuilderVerbose( - info=self.info, - with_counts=self.show_counts, - ) + return DataFrameTableBuilderVerbose( + info=self.info, + with_counts=self.show_counts, + ) class SeriesInfoPrinter(InfoPrinterAbstract): diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index e734304471144..d53f80a93051b 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -272,10 +272,10 @@ class TableSchemaFormatter(BaseFormatter): formatters[mimetype] = TableSchemaFormatter() # enable it if it's been disabled: formatters[mimetype].enabled = True - else: - # unregister tableschema mime-type - if mimetype in formatters: - formatters[mimetype].enabled = False + + # unregister tableschema mime-type + elif mimetype in formatters: + formatters[mimetype].enabled = False def default_pprint(thing: Any, max_seq_items: int | None = None) -> str: diff --git a/pandas/io/parquet.py b/pandas/io/parquet.py index aec31f40f8570..8440ab9aa5072 100644 --- a/pandas/io/parquet.py +++ b/pandas/io/parquet.py @@ -131,9 +131,8 @@ def validate_dataframe(df: DataFrame) -> None: each level of the MultiIndex """ ) - else: - if df.columns.inferred_type not in {"string", "empty"}: - raise ValueError("parquet must have string column names") + elif 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( diff --git a/pandas/io/parsers/c_parser_wrapper.py b/pandas/io/parsers/c_parser_wrapper.py index c10f1811751cd..3f520135be4ec 100644 --- a/pandas/io/parsers/c_parser_wrapper.py +++ b/pandas/io/parsers/c_parser_wrapper.py @@ -394,26 +394,26 @@ def _concatenate_chunks(chunks: list[dict[int, ArrayLike]]) -> dict: dtype = dtypes.pop() if is_categorical_dtype(dtype): result[name] = union_categoricals(arrs, sort_categories=False) + + elif isinstance(dtype, ExtensionDtype): + # TODO: concat_compat? + array_type = dtype.construct_array_type() + # error: Argument 1 to "_concat_same_type" of "ExtensionArray" + # has incompatible type "List[Union[ExtensionArray, ndarray]]"; + # expected "Sequence[ExtensionArray]" + result[name] = array_type._concat_same_type( + arrs # type: ignore[arg-type] + ) else: - if isinstance(dtype, ExtensionDtype): - # TODO: concat_compat? - array_type = dtype.construct_array_type() - # error: Argument 1 to "_concat_same_type" of "ExtensionArray" - # has incompatible type "List[Union[ExtensionArray, ndarray]]"; - # expected "Sequence[ExtensionArray]" - result[name] = array_type._concat_same_type( - arrs # type: ignore[arg-type] - ) - else: - # error: Argument 1 to "concatenate" has incompatible - # type "List[Union[ExtensionArray, ndarray[Any, Any]]]" - # ; expected "Union[_SupportsArray[dtype[Any]], - # Sequence[_SupportsArray[dtype[Any]]], - # Sequence[Sequence[_SupportsArray[dtype[Any]]]], - # Sequence[Sequence[Sequence[_SupportsArray[dtype[Any]]]]] - # , Sequence[Sequence[Sequence[Sequence[ - # _SupportsArray[dtype[Any]]]]]]]" - result[name] = np.concatenate(arrs) # type: ignore[arg-type] + # error: Argument 1 to "concatenate" has incompatible + # type "List[Union[ExtensionArray, ndarray[Any, Any]]]" + # ; expected "Union[_SupportsArray[dtype[Any]], + # Sequence[_SupportsArray[dtype[Any]]], + # Sequence[Sequence[_SupportsArray[dtype[Any]]]], + # Sequence[Sequence[Sequence[_SupportsArray[dtype[Any]]]]] + # , Sequence[Sequence[Sequence[Sequence[ + # _SupportsArray[dtype[Any]]]]]]]" + result[name] = np.concatenate(arrs) # type: ignore[arg-type] if warning_columns: warning_names = ",".join(warning_columns) diff --git a/pandas/io/parsers/python_parser.py b/pandas/io/parsers/python_parser.py index 263966269c0fa..5d36c82c5b2a9 100644 --- a/pandas/io/parsers/python_parser.py +++ b/pandas/io/parsers/python_parser.py @@ -555,20 +555,20 @@ def _infer_columns( columns = self._handle_usecols( columns, columns[0], num_original_columns ) - else: - if self.usecols is None or len(names) >= num_original_columns: - columns = self._handle_usecols([names], names, num_original_columns) - num_original_columns = len(names) - else: - if not callable(self.usecols) and len(names) != len(self.usecols): - raise ValueError( - "Number of passed names did not match number of " - "header fields in the file" - ) - # Ignore output but set used columns. - self._handle_usecols([names], names, ncols) - columns = [names] - num_original_columns = ncols + + elif self.usecols is None or len(names) >= num_original_columns: + columns = self._handle_usecols([names], names, num_original_columns) + num_original_columns = len(names) + + elif not callable(self.usecols) and len(names) != len(self.usecols): + raise ValueError( + "Number of passed names did not match number of " + "header fields in the file" + ) + # Ignore output but set used columns. + self._handle_usecols([names], names, ncols) + columns = [names] + num_original_columns = ncols return columns, num_original_columns, unnamed_cols diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index a813a0e285027..7d6430e6b68d8 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -2196,9 +2196,9 @@ def update_info(self, info) -> None: f"existing_value [{existing_value}] conflicts with " f"new value [{value}]" ) - else: - if value is not None or existing_value is not None: - idx[key] = value + + elif value is not None or existing_value is not None: + idx[key] = value def set_info(self, info) -> None: """set my state from the passed info""" From 14c94bffb85a313f1c6dfba45e130fa032f1c5cf Mon Sep 17 00:00:00 2001 From: Ahmad Date: Thu, 2 Mar 2023 12:28:28 +0300 Subject: [PATCH 03/11] 8 more --- pandas/io/xml.py | 65 ++++++++++----------- pandas/plotting/_matplotlib/boxplot.py | 14 ++--- pandas/plotting/_matplotlib/core.py | 60 +++++++++---------- pandas/plotting/_matplotlib/timeseries.py | 6 +- pandas/tests/frame/indexing/test_where.py | 7 +-- pandas/tests/frame/methods/test_set_axis.py | 15 +++-- pandas/tests/frame/test_constructors.py | 11 ++-- pandas/tests/groupby/test_groupby_dropna.py | 11 ++-- 8 files changed, 89 insertions(+), 100 deletions(-) diff --git a/pandas/io/xml.py b/pandas/io/xml.py index 90d67ac45d4fd..bafd8b4eb6448 100644 --- a/pandas/io/xml.py +++ b/pandas/io/xml.py @@ -240,40 +240,39 @@ def _parse_nodes(self, elems: list[Any]) -> list[dict[str, str | None]]: for el in elems ] - else: - if self.names: - dicts = [ - { - **el.attrib, - **( - {el.tag: el.text.strip()} - if el.text and not el.text.isspace() - else {} - ), - **{ - nm: ch.text.strip() if ch.text else None - for nm, ch in zip(self.names, el.findall("*")) - }, - } - for el in elems - ] + elif self.names: + dicts = [ + { + **el.attrib, + **( + {el.tag: el.text.strip()} + if el.text and not el.text.isspace() + else {} + ), + **{ + nm: ch.text.strip() if ch.text else None + for nm, ch in zip(self.names, el.findall("*")) + }, + } + for el in elems + ] - else: - dicts = [ - { - **el.attrib, - **( - {el.tag: el.text.strip()} - if el.text and not el.text.isspace() - else {} - ), - **{ - ch.tag: ch.text.strip() if ch.text else None - for ch in el.findall("*") - }, - } - for el in elems - ] + else: + dicts = [ + { + **el.attrib, + **( + {el.tag: el.text.strip()} + if el.text and not el.text.isspace() + else {} + ), + **{ + ch.tag: ch.text.strip() if ch.text else None + for ch in el.findall("*") + }, + } + for el in elems + ] dicts = [ {k.split("}")[1] if "}" in k else k: v for k, v in d.items()} for d in dicts diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py index e2f30da1b839c..ad054b6065756 100644 --- a/pandas/plotting/_matplotlib/boxplot.py +++ b/pandas/plotting/_matplotlib/boxplot.py @@ -403,11 +403,10 @@ def plot_group(keys, values, ax: Axes, **kwds): colors = _get_colors() if column is None: columns = None + elif isinstance(column, (list, tuple)): + columns = column else: - if isinstance(column, (list, tuple)): - columns = column - else: - columns = [column] + columns = [column] if by is not None: # Prefer array return type for 2-D plots to match the subplot layout @@ -523,11 +522,10 @@ def boxplot_frame_groupby( keys, frames = zip(*grouped) if grouped.axis == 0: df = pd.concat(frames, keys=keys, axis=1) + elif len(frames) > 1: + df = frames[0].join(frames[1::]) else: - if len(frames) > 1: - df = frames[0].join(frames[1::]) - else: - df = frames[0] + df = frames[0] # GH 16748, DataFrameGroupby fails when subplots=False and `column` argument # is assigned, and in this case, since `df` here becomes MI after groupby, diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 754cc94b6ded6..94e416e2a2e8a 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -164,17 +164,16 @@ def __init__( if isinstance(data, DataFrame): if column: self.columns = com.maybe_make_list(column) + elif self.by is None: + self.columns = [ + col for col in data.columns if is_numeric_dtype(data[col]) + ] else: - if self.by is None: - self.columns = [ - col for col in data.columns if is_numeric_dtype(data[col]) - ] - else: - self.columns = [ - col - for col in data.columns - if col not in self.by and is_numeric_dtype(data[col]) - ] + self.columns = [ + col + for col in data.columns + if col not in self.by and is_numeric_dtype(data[col]) + ] # For `hist` plot, need to get grouped original data before `self.data` is # updated later @@ -504,15 +503,14 @@ def _setup_subplots(self): layout=self.layout, layout_type=self._layout_type, ) + elif self.ax is None: + fig = self.plt.figure(figsize=self.figsize) + axes = fig.add_subplot(111) else: - if self.ax is None: - fig = self.plt.figure(figsize=self.figsize) - axes = fig.add_subplot(111) - else: - fig = self.ax.get_figure() - if self.figsize is not None: - fig.set_size_inches(self.figsize) - axes = self.ax + fig = self.ax.get_figure() + if self.figsize is not None: + fig.set_size_inches(self.figsize) + axes = self.ax axes = flatten_axes(axes) @@ -1225,14 +1223,13 @@ def _make_plot(self): if self.colormap is not None: cmap = mpl.colormaps.get_cmap(self.colormap) + # cmap is only used if c_values are integers, otherwise UserWarning + elif is_integer_dtype(c_values): + # pandas uses colormap, matplotlib uses cmap. + cmap = "Greys" + cmap = mpl.colormaps[cmap] else: - # cmap is only used if c_values are integers, otherwise UserWarning - if is_integer_dtype(c_values): - # pandas uses colormap, matplotlib uses cmap. - cmap = "Greys" - cmap = mpl.colormaps[cmap] - else: - cmap = None + cmap = None if color_by_categorical: from matplotlib import colors @@ -1630,14 +1627,13 @@ def __init__(self, data, **kwargs) -> None: self.lim_offset = self.bar_width / 2 else: self.lim_offset = 0 + elif kwargs["align"] == "edge": + w = self.bar_width / self.nseries + self.tickoffset = self.bar_width * (pos - 0.5) + w * 0.5 + self.lim_offset = w * 0.5 else: - if kwargs["align"] == "edge": - w = self.bar_width / self.nseries - self.tickoffset = self.bar_width * (pos - 0.5) + w * 0.5 - self.lim_offset = w * 0.5 - else: - self.tickoffset = self.bar_width * pos - self.lim_offset = 0 + self.tickoffset = self.bar_width * pos + self.lim_offset = 0 self.ax_pos = self.tick_pos - self.tickoffset diff --git a/pandas/plotting/_matplotlib/timeseries.py b/pandas/plotting/_matplotlib/timeseries.py index 8e21b2c691185..ab051ee58da70 100644 --- a/pandas/plotting/_matplotlib/timeseries.py +++ b/pandas/plotting/_matplotlib/timeseries.py @@ -216,9 +216,9 @@ def use_dynamic_x(ax: Axes, data: DataFrame | Series) -> bool: if freq is None: # convert irregular if axes has freq info freq = ax_freq - else: # do not use tsplot if irregular was plotted first - if (ax_freq is None) and (len(ax.get_lines()) > 0): - return False + # do not use tsplot if irregular was plotted first + elif (ax_freq is None) and (len(ax.get_lines()) > 0): + return False if freq is None: return False diff --git a/pandas/tests/frame/indexing/test_where.py b/pandas/tests/frame/indexing/test_where.py index f0fb0a0595cbd..0bcb766a08237 100644 --- a/pandas/tests/frame/indexing/test_where.py +++ b/pandas/tests/frame/indexing/test_where.py @@ -105,11 +105,10 @@ def _check_align(df, cond, other, check_dtypes=True): if is_scalar(other): o = other + elif isinstance(other, np.ndarray): + o = Series(other[:, i], index=result.index).values else: - if isinstance(other, np.ndarray): - o = Series(other[:, i], index=result.index).values - else: - o = other[k].values + o = other[k].values new_values = d if c.all() else np.where(c, d, o) expected = Series(new_values, index=result.index, name=k) diff --git a/pandas/tests/frame/methods/test_set_axis.py b/pandas/tests/frame/methods/test_set_axis.py index fd140e0098f2a..6ae53fff8dd28 100644 --- a/pandas/tests/frame/methods/test_set_axis.py +++ b/pandas/tests/frame/methods/test_set_axis.py @@ -66,15 +66,14 @@ def test_set_axis_copy(self, obj, using_copy_on_write): tm.shares_memory(result.iloc[:, i], obj.iloc[:, i]) for i in range(obj.shape[1]) ) + # check we DID make a copy + elif obj.ndim == 1: + assert not tm.shares_memory(result, obj) else: - # check we DID make a copy - if obj.ndim == 1: - assert not tm.shares_memory(result, obj) - else: - assert not any( - tm.shares_memory(result.iloc[:, i], obj.iloc[:, i]) - for i in range(obj.shape[1]) - ) + assert not any( + tm.shares_memory(result.iloc[:, i], obj.iloc[:, i]) + for i in range(obj.shape[1]) + ) res = obj.set_axis(new_index, copy=False) tm.assert_equal(expected, res) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 569ec613cbcb9..e627703c687c8 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2976,13 +2976,12 @@ def constructor(self, frame_or_series, box): ) else: return lambda x, **kwargs: frame_or_series({"A": x}, **extra, **kwargs) + elif frame_or_series is Series: + return lambda x, **kwargs: frame_or_series([x, x], **extra, **kwargs) else: - if frame_or_series is Series: - return lambda x, **kwargs: frame_or_series([x, x], **extra, **kwargs) - else: - return lambda x, **kwargs: frame_or_series( - {"A": [x, x]}, **extra, **kwargs - ) + return lambda x, **kwargs: frame_or_series( + {"A": [x, x]}, **extra, **kwargs + ) @pytest.mark.parametrize("dtype", ["M8[ns]", "m8[ns]"]) def test_from_nat_scalar(self, dtype, constructor): diff --git a/pandas/tests/groupby/test_groupby_dropna.py b/pandas/tests/groupby/test_groupby_dropna.py index 31a8e7a7d36ac..95184bfd770d1 100644 --- a/pandas/tests/groupby/test_groupby_dropna.py +++ b/pandas/tests/groupby/test_groupby_dropna.py @@ -552,12 +552,11 @@ def test_categorical_reducers( expected = expected.set_index(["x", "x2"]) else: expected = expected.set_index("x") - else: - if index_kind != "range" and reduction_func != "size": - # size, unlike other methods, has the desired behavior in GH#49519 - expected = expected.drop(columns="x") - if index_kind == "multi": - expected = expected.drop(columns="x2") + elif index_kind != "range" and reduction_func != "size": + # size, unlike other methods, has the desired behavior in GH#49519 + expected = expected.drop(columns="x") + if index_kind == "multi": + expected = expected.drop(columns="x2") if reduction_func in ("idxmax", "idxmin") and index_kind != "range": # expected was computed with a RangeIndex; need to translate to index values values = expected["y"].values.tolist() From 072c2f41094cfc3cfacb6416b73fc7cbf1826a3b Mon Sep 17 00:00:00 2001 From: Ahmad Date: Thu, 2 Mar 2023 12:40:28 +0300 Subject: [PATCH 04/11] more fixess --- pandas/io/formats/info.py | 3 +-- pandas/io/parsers/arrow_parser_wrapper.py | 7 +++--- pandas/io/parsers/c_parser_wrapper.py | 3 +-- pandas/io/parsers/python_parser.py | 6 ++--- pandas/tests/indexes/test_numpy_compat.py | 22 +++++++++---------- pandas/tests/indexing/test_floats.py | 5 ++--- .../tests/scalar/timedelta/test_timedelta.py | 20 ++++++++--------- .../tests/scalar/timestamp/test_unary_ops.py | 9 ++++---- pandas/util/_validators.py | 7 +++--- scripts/validate_unwanted_patterns.py | 17 +++++++------- 10 files changed, 43 insertions(+), 56 deletions(-) diff --git a/pandas/io/formats/info.py b/pandas/io/formats/info.py index ab24347a518dd..6deed3957dc78 100644 --- a/pandas/io/formats/info.py +++ b/pandas/io/formats/info.py @@ -659,8 +659,7 @@ def _create_table_builder(self) -> DataFrameTableBuilder: with_counts=self.show_counts, ) elif self.verbose is False: # specifically set to False, not necessarily None - return DataFrameTableBuilderNonVerbose(info=self.info) - + return DataFrameTableBuilderNonVerbose(info=self.info) elif self.exceeds_info_cols: return DataFrameTableBuilderNonVerbose(info=self.info) else: diff --git a/pandas/io/parsers/arrow_parser_wrapper.py b/pandas/io/parsers/arrow_parser_wrapper.py index 420b6212f857a..9c726cb246572 100644 --- a/pandas/io/parsers/arrow_parser_wrapper.py +++ b/pandas/io/parsers/arrow_parser_wrapper.py @@ -112,10 +112,9 @@ def _finalize_pandas_output(self, frame: DataFrame) -> DataFrame: for i, item in enumerate(self.index_col): if is_integer(item): self.index_col[i] = frame.columns[item] - else: - # String case - if item not in frame.columns: - raise ValueError(f"Index {item} invalid") + # String case + elif item not in frame.columns: + raise ValueError(f"Index {item} invalid") frame.set_index(self.index_col, drop=True, inplace=True) # Clear names if headerless and no name given if self.header is None and not multi_index_named: diff --git a/pandas/io/parsers/c_parser_wrapper.py b/pandas/io/parsers/c_parser_wrapper.py index 3f520135be4ec..8fabfba98861a 100644 --- a/pandas/io/parsers/c_parser_wrapper.py +++ b/pandas/io/parsers/c_parser_wrapper.py @@ -393,8 +393,7 @@ def _concatenate_chunks(chunks: list[dict[int, ArrayLike]]) -> dict: dtype = dtypes.pop() if is_categorical_dtype(dtype): - result[name] = union_categoricals(arrs, sort_categories=False) - + result[name] = union_categoricals(arrs, sort_categories=False) elif isinstance(dtype, ExtensionDtype): # TODO: concat_compat? array_type = dtype.construct_array_type() diff --git a/pandas/io/parsers/python_parser.py b/pandas/io/parsers/python_parser.py index 5d36c82c5b2a9..717047a7e9476 100644 --- a/pandas/io/parsers/python_parser.py +++ b/pandas/io/parsers/python_parser.py @@ -554,12 +554,10 @@ def _infer_columns( columns = [list(range(ncols))] columns = self._handle_usecols( columns, columns[0], num_original_columns - ) - + ) elif self.usecols is None or len(names) >= num_original_columns: columns = self._handle_usecols([names], names, num_original_columns) - num_original_columns = len(names) - + num_original_columns = len(names) elif not callable(self.usecols) and len(names) != len(self.usecols): raise ValueError( "Number of passed names did not match number of " diff --git a/pandas/tests/indexes/test_numpy_compat.py b/pandas/tests/indexes/test_numpy_compat.py index 07ebe6fa04beb..5e2e2b9bbec21 100644 --- a/pandas/tests/indexes/test_numpy_compat.py +++ b/pandas/tests/indexes/test_numpy_compat.py @@ -92,14 +92,13 @@ def test_numpy_ufuncs_basic(index, func): else: # e.g. np.exp with Int64 -> Float64 assert type(result) is Index + # raise AttributeError or TypeError + elif len(index) == 0: + pass else: - # raise AttributeError or TypeError - if len(index) == 0: - pass - else: - with tm.external_error_raised((TypeError, AttributeError)): - with np.errstate(all="ignore"): - func(index) + with tm.external_error_raised((TypeError, AttributeError)): + with np.errstate(all="ignore"): + func(index) @pytest.mark.parametrize( @@ -144,12 +143,11 @@ def test_numpy_ufuncs_other(index, func): else: tm.assert_numpy_array_equal(out, result) + elif len(index) == 0: + pass else: - if len(index) == 0: - pass - else: - with tm.external_error_raised(TypeError): - func(index) + with tm.external_error_raised(TypeError): + func(index) @pytest.mark.parametrize("func", [np.maximum, np.minimum]) diff --git a/pandas/tests/indexing/test_floats.py b/pandas/tests/indexing/test_floats.py index 9cccd2c45c9e7..cc10320bd7f1d 100644 --- a/pandas/tests/indexing/test_floats.py +++ b/pandas/tests/indexing/test_floats.py @@ -29,10 +29,9 @@ def check(self, result, original, indexer, getitem): """ if isinstance(original, Series): expected = original.iloc[indexer] + elif getitem: + expected = original.iloc[:, indexer] else: - if getitem: - expected = original.iloc[:, indexer] - else: expected = original.iloc[indexer] tm.assert_almost_equal(result, expected) diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 7046055b5c0d5..4cb77b2f1d065 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -749,17 +749,15 @@ def checker(ts, nanos, unit): with pytest.raises(err_cls, match=msg): method(ts, unit) return - else: - if mod >= diff: - if ub > cls.max._value: - with pytest.raises(err_cls, match=msg): - method(ts, unit) - return - else: - if lb < cls.min._value: - with pytest.raises(err_cls, match=msg): - method(ts, unit) - return + elif mod >= diff: + if ub > cls.max._value: + with pytest.raises(err_cls, match=msg): + method(ts, unit) + return + elif lb < cls.min._value: + with pytest.raises(err_cls, match=msg): + method(ts, unit) + return res = method(ts, unit) diff --git a/pandas/tests/scalar/timestamp/test_unary_ops.py b/pandas/tests/scalar/timestamp/test_unary_ops.py index 5a082fb91a03e..568bee376a1fd 100644 --- a/pandas/tests/scalar/timestamp/test_unary_ops.py +++ b/pandas/tests/scalar/timestamp/test_unary_ops.py @@ -351,11 +351,10 @@ def checker(ts, nanos, unit): with pytest.raises(err_cls, match=msg): method(ts, unit) return - else: - if lb < cls.min._value: - with pytest.raises(err_cls, match=msg): - method(ts, unit) - return + elif lb < cls.min._value: + with pytest.raises(err_cls, match=msg): + method(ts, unit) + return res = method(ts, unit) diff --git a/pandas/util/_validators.py b/pandas/util/_validators.py index b60169f8364da..70a243ff250e0 100644 --- a/pandas/util/_validators.py +++ b/pandas/util/_validators.py @@ -328,10 +328,9 @@ def validate_percentile(q: float | Iterable[float]) -> np.ndarray: if q_arr.ndim == 0: if not 0 <= q_arr <= 1: raise ValueError(msg.format(q_arr / 100.0)) - else: - if not all(0 <= qs <= 1 for qs in q_arr): - raise ValueError(msg.format(q_arr / 100.0)) - return q_arr + elif not all(0 <= qs <= 1 for qs in q_arr): + raise ValueError(msg.format(q_arr / 100.0)) +return q_arr @overload diff --git a/scripts/validate_unwanted_patterns.py b/scripts/validate_unwanted_patterns.py index dbccb91f809d1..5d2a989b5e150 100755 --- a/scripts/validate_unwanted_patterns.py +++ b/scripts/validate_unwanted_patterns.py @@ -139,15 +139,14 @@ def bare_pytest_raises(file_obj: IO[str]) -> Iterable[Tuple[int, str]]: "Bare pytests raise have been found. " "Please pass in the argument 'match' as well the exception.", ) - else: - # Means that there are arguments that are being passed in, - # now we validate that `match` is one of the passed in arguments - if not any(keyword.arg == "match" for keyword in node.keywords): - yield ( - node.lineno, - "Bare pytests raise have been found. " - "Please pass in the argument 'match' as well the exception.", - ) + # Means that there are arguments that are being passed in, + # now we validate that `match` is one of the passed in arguments + elif not any(keyword.arg == "match" for keyword in node.keywords): + yield ( + node.lineno, + "Bare pytests raise have been found. " + "Please pass in the argument 'match' as well the exception.", + ) PRIVATE_FUNCTIONS_ALLOWED = {"sys._getframe"} # no known alternative From 4ec12330bb0ac4ec73f80c53ff2cf470ba542862 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Thu, 2 Mar 2023 19:01:09 +0300 Subject: [PATCH 05/11] moreeeeeeeee changes --- pandas/io/formats/info.py | 2 +- pandas/io/parsers/c_parser_wrapper.py | 2 +- pandas/io/parsers/python_parser.py | 4 ++-- pandas/tests/scalar/timestamp/test_unary_ops.py | 13 ++++++------- pandas/tests/tslibs/test_conversion.py | 7 +++---- pandas/util/_validators.py | 2 +- 6 files changed, 14 insertions(+), 16 deletions(-) diff --git a/pandas/io/formats/info.py b/pandas/io/formats/info.py index 6deed3957dc78..079f5042f43bd 100644 --- a/pandas/io/formats/info.py +++ b/pandas/io/formats/info.py @@ -659,7 +659,7 @@ def _create_table_builder(self) -> DataFrameTableBuilder: with_counts=self.show_counts, ) elif self.verbose is False: # specifically set to False, not necessarily None - return DataFrameTableBuilderNonVerbose(info=self.info) + return DataFrameTableBuilderNonVerbose(info=self.info) elif self.exceeds_info_cols: return DataFrameTableBuilderNonVerbose(info=self.info) else: diff --git a/pandas/io/parsers/c_parser_wrapper.py b/pandas/io/parsers/c_parser_wrapper.py index 8fabfba98861a..52126dc63aca4 100644 --- a/pandas/io/parsers/c_parser_wrapper.py +++ b/pandas/io/parsers/c_parser_wrapper.py @@ -393,7 +393,7 @@ def _concatenate_chunks(chunks: list[dict[int, ArrayLike]]) -> dict: dtype = dtypes.pop() if is_categorical_dtype(dtype): - result[name] = union_categoricals(arrs, sort_categories=False) + result[name] = union_categoricals(arrs, sort_categories=False) elif isinstance(dtype, ExtensionDtype): # TODO: concat_compat? array_type = dtype.construct_array_type() diff --git a/pandas/io/parsers/python_parser.py b/pandas/io/parsers/python_parser.py index 717047a7e9476..ab0aa1604901d 100644 --- a/pandas/io/parsers/python_parser.py +++ b/pandas/io/parsers/python_parser.py @@ -554,10 +554,10 @@ def _infer_columns( columns = [list(range(ncols))] columns = self._handle_usecols( columns, columns[0], num_original_columns - ) + ) elif self.usecols is None or len(names) >= num_original_columns: columns = self._handle_usecols([names], names, num_original_columns) - num_original_columns = len(names) + num_original_columns = len(names) elif not callable(self.usecols) and len(names) != len(self.usecols): raise ValueError( "Number of passed names did not match number of " diff --git a/pandas/tests/scalar/timestamp/test_unary_ops.py b/pandas/tests/scalar/timestamp/test_unary_ops.py index 568bee376a1fd..2fee395886cff 100644 --- a/pandas/tests/scalar/timestamp/test_unary_ops.py +++ b/pandas/tests/scalar/timestamp/test_unary_ops.py @@ -345,16 +345,15 @@ def checker(ts, nanos, unit): with pytest.raises(err_cls, match=msg): method(ts, unit) return - else: - if mod >= diff: - if ub > cls.max._value: - with pytest.raises(err_cls, match=msg): - method(ts, unit) - return - elif lb < cls.min._value: + elif mod >= diff: + if ub > cls.max._value: with pytest.raises(err_cls, match=msg): method(ts, unit) return + elif lb < cls.min._value: + with pytest.raises(err_cls, match=msg): + method(ts, unit) + return res = method(ts, unit) diff --git a/pandas/tests/tslibs/test_conversion.py b/pandas/tests/tslibs/test_conversion.py index bf9a45c6d9d7a..42d2bc01f5e67 100644 --- a/pandas/tests/tslibs/test_conversion.py +++ b/pandas/tests/tslibs/test_conversion.py @@ -110,11 +110,10 @@ def test_length_zero_copy(dtype, copy): result = astype_overflowsafe(arr, copy=copy, dtype=np.dtype("M8[ns]")) if copy: assert not np.shares_memory(result, arr) + elif arr.dtype == result.dtype: + assert result is arr else: - if arr.dtype == result.dtype: - assert result is arr - else: - assert not np.shares_memory(result, arr) + assert not np.shares_memory(result, arr) def test_ensure_datetime64ns_bigendian(): diff --git a/pandas/util/_validators.py b/pandas/util/_validators.py index 70a243ff250e0..7b1eca695c6d6 100644 --- a/pandas/util/_validators.py +++ b/pandas/util/_validators.py @@ -330,7 +330,7 @@ def validate_percentile(q: float | Iterable[float]) -> np.ndarray: raise ValueError(msg.format(q_arr / 100.0)) elif not all(0 <= qs <= 1 for qs in q_arr): raise ValueError(msg.format(q_arr / 100.0)) -return q_arr + return q_arr @overload From 6e5f610fbbb0cbfc1e32229c67208d6dd0eb4a24 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Fri, 3 Mar 2023 13:40:08 +0300 Subject: [PATCH 06/11] fix python_parser.py (try #1) --- pandas/io/parsers/python_parser.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/io/parsers/python_parser.py b/pandas/io/parsers/python_parser.py index ab0aa1604901d..79ef55b108ce2 100644 --- a/pandas/io/parsers/python_parser.py +++ b/pandas/io/parsers/python_parser.py @@ -558,15 +558,15 @@ def _infer_columns( elif self.usecols is None or len(names) >= num_original_columns: columns = self._handle_usecols([names], names, num_original_columns) num_original_columns = len(names) - elif not callable(self.usecols) and len(names) != len(self.usecols): - raise ValueError( - "Number of passed names did not match number of " - "header fields in the file" - ) - # Ignore output but set used columns. - self._handle_usecols([names], names, ncols) - columns = [names] - num_original_columns = ncols + if not callable(self.usecols) and len(names) != len(self.usecols): + raise ValueError( + "Number of passed names did not match number of " + "header fields in the file" + ) + # Ignore output but set used columns. + self._handle_usecols([names], names, ncols) + columns = [names] + num_original_columns = ncols return columns, num_original_columns, unnamed_cols From 85aee9f2ff359d9fcc2bc85f6ac37fa29f3bbdae Mon Sep 17 00:00:00 2001 From: Ahmad Date: Fri, 3 Mar 2023 14:45:37 +0300 Subject: [PATCH 07/11] try no. 2 --- pandas/io/parsers/c_parser_wrapper.py | 4 +--- pandas/tests/frame/test_constructors.py | 4 +--- pandas/tests/indexing/test_floats.py | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/pandas/io/parsers/c_parser_wrapper.py b/pandas/io/parsers/c_parser_wrapper.py index 52126dc63aca4..9c218c4c34d1d 100644 --- a/pandas/io/parsers/c_parser_wrapper.py +++ b/pandas/io/parsers/c_parser_wrapper.py @@ -400,9 +400,7 @@ def _concatenate_chunks(chunks: list[dict[int, ArrayLike]]) -> dict: # error: Argument 1 to "_concat_same_type" of "ExtensionArray" # has incompatible type "List[Union[ExtensionArray, ndarray]]"; # expected "Sequence[ExtensionArray]" - result[name] = array_type._concat_same_type( - arrs # type: ignore[arg-type] - ) + result[name] = array_type._concat_same_type(arrs) # type: ignore[arg-type] else: # error: Argument 1 to "concatenate" has incompatible # type "List[Union[ExtensionArray, ndarray[Any, Any]]]" diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index e627703c687c8..711eb924925d6 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2979,9 +2979,7 @@ def constructor(self, frame_or_series, box): elif frame_or_series is Series: return lambda x, **kwargs: frame_or_series([x, x], **extra, **kwargs) else: - return lambda x, **kwargs: frame_or_series( - {"A": [x, x]}, **extra, **kwargs - ) + return lambda x, **kwargs: frame_or_series({"A": [x, x]}, **extra, **kwargs) @pytest.mark.parametrize("dtype", ["M8[ns]", "m8[ns]"]) def test_from_nat_scalar(self, dtype, constructor): diff --git a/pandas/tests/indexing/test_floats.py b/pandas/tests/indexing/test_floats.py index cc10320bd7f1d..3b622d7b4703a 100644 --- a/pandas/tests/indexing/test_floats.py +++ b/pandas/tests/indexing/test_floats.py @@ -32,7 +32,7 @@ def check(self, result, original, indexer, getitem): elif getitem: expected = original.iloc[:, indexer] else: - expected = original.iloc[indexer] + expected = original.iloc[indexer] tm.assert_almost_equal(result, expected) From e54d77eb5f76ab9b12358f39b430e36ec48e43ce Mon Sep 17 00:00:00 2001 From: Ahmad Date: Fri, 3 Mar 2023 16:12:30 +0300 Subject: [PATCH 08/11] fix python_parser.py (try no. 3) --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/io/parsers/python_parser.py | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 40dfe0b33d66c..c0ca5b2320338 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -92,8 +92,8 @@ Other API changes Deprecations ~~~~~~~~~~~~ -- Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) - Deprecating pinning ``group.name`` to each group in :meth:`SeriesGroupBy.aggregate` aggregations; if your operation requires utilizing the groupby keys, iterate over the groupby object instead (:issue:`41090`) +- Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) - .. --------------------------------------------------------------------------- diff --git a/pandas/io/parsers/python_parser.py b/pandas/io/parsers/python_parser.py index 79ef55b108ce2..ab0aa1604901d 100644 --- a/pandas/io/parsers/python_parser.py +++ b/pandas/io/parsers/python_parser.py @@ -558,15 +558,15 @@ def _infer_columns( elif self.usecols is None or len(names) >= num_original_columns: columns = self._handle_usecols([names], names, num_original_columns) num_original_columns = len(names) - if not callable(self.usecols) and len(names) != len(self.usecols): - raise ValueError( - "Number of passed names did not match number of " - "header fields in the file" - ) - # Ignore output but set used columns. - self._handle_usecols([names], names, ncols) - columns = [names] - num_original_columns = ncols + elif not callable(self.usecols) and len(names) != len(self.usecols): + raise ValueError( + "Number of passed names did not match number of " + "header fields in the file" + ) + # Ignore output but set used columns. + self._handle_usecols([names], names, ncols) + columns = [names] + num_original_columns = ncols return columns, num_original_columns, unnamed_cols From a5bfe54baefc23514d5287b8e56b964a949a5f54 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Fri, 3 Mar 2023 20:39:30 +0300 Subject: [PATCH 09/11] v2.1.0 (fix try 1) --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index c0ca5b2320338..40dfe0b33d66c 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -92,8 +92,8 @@ Other API changes Deprecations ~~~~~~~~~~~~ -- Deprecating pinning ``group.name`` to each group in :meth:`SeriesGroupBy.aggregate` aggregations; if your operation requires utilizing the groupby keys, iterate over the groupby object instead (:issue:`41090`) - Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) +- Deprecating pinning ``group.name`` to each group in :meth:`SeriesGroupBy.aggregate` aggregations; if your operation requires utilizing the groupby keys, iterate over the groupby object instead (:issue:`41090`) - .. --------------------------------------------------------------------------- From 932a4db7816a1d1dc5e4ebe3e9ff18a9150f7ed6 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Fri, 3 Mar 2023 23:25:10 +0300 Subject: [PATCH 10/11] try no. 2 --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/io/parsers/python_parser.py | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 40dfe0b33d66c..c0ca5b2320338 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -92,8 +92,8 @@ Other API changes Deprecations ~~~~~~~~~~~~ -- Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) - Deprecating pinning ``group.name`` to each group in :meth:`SeriesGroupBy.aggregate` aggregations; if your operation requires utilizing the groupby keys, iterate over the groupby object instead (:issue:`41090`) +- Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) - .. --------------------------------------------------------------------------- diff --git a/pandas/io/parsers/python_parser.py b/pandas/io/parsers/python_parser.py index ab0aa1604901d..90481ff2c39bb 100644 --- a/pandas/io/parsers/python_parser.py +++ b/pandas/io/parsers/python_parser.py @@ -563,10 +563,11 @@ def _infer_columns( "Number of passed names did not match number of " "header fields in the file" ) - # Ignore output but set used columns. - self._handle_usecols([names], names, ncols) - columns = [names] - num_original_columns = ncols + else: + # Ignore output but set used columns. + self._handle_usecols([names], names, ncols) + columns = [names] + num_original_columns = ncols return columns, num_original_columns, unnamed_cols From 0277ae201e668b0bbb9392944d16a679537e3089 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Tue, 7 Mar 2023 18:28:12 +0300 Subject: [PATCH 11/11] Removed extra lines --- pandas/io/formats/format.py | 1 - pandas/io/formats/printing.py | 1 - pandas/io/pytables.py | 1 - 3 files changed, 3 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index c2598522fb1f3..a9ca8e237a46f 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -2103,7 +2103,6 @@ def __call__(self, num: float) -> str: if self.use_eng_prefix: prefix = self.ENG_PREFIXES[int_pow10] - elif int_pow10 < 0: prefix = f"E-{-int_pow10:02d}" else: diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index d53f80a93051b..58c2d0a9aa574 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -272,7 +272,6 @@ class TableSchemaFormatter(BaseFormatter): formatters[mimetype] = TableSchemaFormatter() # enable it if it's been disabled: formatters[mimetype].enabled = True - # unregister tableschema mime-type elif mimetype in formatters: formatters[mimetype].enabled = False diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 7d6430e6b68d8..7bdf8b187deed 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -2196,7 +2196,6 @@ def update_info(self, info) -> None: f"existing_value [{existing_value}] conflicts with " f"new value [{value}]" ) - elif value is not None or existing_value is not None: idx[key] = value