From 6d05c42de83d141d0dce610bded59dfcfcd85c58 Mon Sep 17 00:00:00 2001 From: Victor Momodu Date: Thu, 16 Feb 2023 17:11:19 +0100 Subject: [PATCH 1/8] Ran pre-commit and fixed all errors --- .pre-commit-config.yaml | 1 + pandas/conftest.py | 4 +--- pandas/core/dtypes/dtypes.py | 6 +++--- pandas/core/series.py | 2 -- pandas/io/formats/css.py | 6 +++--- pandas/io/json/_json.py | 3 +-- pandas/io/pytables.py | 4 ++-- pandas/io/stata.py | 1 - pandas/tests/frame/methods/test_to_records.py | 2 +- pandas/tests/io/formats/style/test_style.py | 2 +- pandas/tseries/frequencies.py | 2 +- pyproject.toml | 2 ++ scripts/validate_exception_location.py | 3 +-- 13 files changed, 17 insertions(+), 21 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2f13ac4c73fa0..a6364d51cab41 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,6 +19,7 @@ repos: rev: v0.0.244 hooks: - id: ruff + args: [--fix, --exit-non-zero-on-fix] - repo: https://github.com/MarcoGorelli/absolufy-imports rev: v0.3.1 hooks: diff --git a/pandas/conftest.py b/pandas/conftest.py index 50951532364d1..4284a03f20d3c 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -660,9 +660,7 @@ def index_flat(request): key for key, value in indices_dict.items() if not ( - key.startswith("int") - or key.startswith("uint") - or key.startswith("float") + key.startswith(("int", "uint", "float")) or key in ["range", "empty", "repeats", "bool-dtype"] ) and not isinstance(value, MultiIndex) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 123e20d6258bf..2909b24840831 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -897,7 +897,7 @@ def freq(self): @classmethod def _parse_dtype_strict(cls, freq: str_type) -> BaseOffset: if isinstance(freq, str): # note: freq is already of type str! - if freq.startswith("period[") or freq.startswith("Period["): + if freq.startswith(("Period[", "period[")): m = cls._match.search(freq) if m is not None: freq = m.group("freq") @@ -916,7 +916,7 @@ def construct_from_string(cls, string: str_type) -> PeriodDtype: """ if ( isinstance(string, str) - and (string.startswith("period[") or string.startswith("Period[")) + and (string.startswith(("period[", "Period["))) or isinstance(string, BaseOffset) ): # do not parse string like U as period[U] @@ -980,7 +980,7 @@ def is_dtype(cls, dtype: object) -> bool: if isinstance(dtype, str): # PeriodDtype can be instantiated from freq string like "U", # but doesn't regard freq str like "U" as dtype. - if dtype.startswith("period[") or dtype.startswith("Period["): + if dtype.startswith(("period[", "Period[")): try: return cls._parse_dtype_strict(dtype) is not None except ValueError: diff --git a/pandas/core/series.py b/pandas/core/series.py index e4c7c4d3b3d73..9b40b586a6814 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -836,8 +836,6 @@ def view(self, dtype: Dtype | None = None) -> Series: # ---------------------------------------------------------------------- # NDArray Compat - _HANDLED_TYPES = (Index, ExtensionArray, np.ndarray) - def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray: """ Return the values as a NumPy array. diff --git a/pandas/io/formats/css.py b/pandas/io/formats/css.py index f2f808a6e2081..d757b0ec18a31 100644 --- a/pandas/io/formats/css.py +++ b/pandas/io/formats/css.py @@ -200,15 +200,15 @@ class CSSResolver: SIDES = ("top", "right", "bottom", "left") CSS_EXPANSIONS = { - **{ + { (f"border-{prop}" if prop else "border"): _border_expander(prop) for prop in ["", "top", "right", "bottom", "left"] }, - **{ + { f"border-{prop}": _side_expander(f"border-{{:s}}-{prop}") for prop in ["color", "style", "width"] }, - **{ + { "margin": _side_expander("margin-{:s}"), "padding": _side_expander("padding-{:s}"), }, diff --git a/pandas/io/json/_json.py b/pandas/io/json/_json.py index bdc070d04bd69..335d510666a1f 100644 --- a/pandas/io/json/_json.py +++ b/pandas/io/json/_json.py @@ -1415,8 +1415,7 @@ def is_ok(col) -> bool: col_lower = col.lower() if ( - col_lower.endswith("_at") - or col_lower.endswith("_time") + col_lower.endswith(("_at", "_time")) or col_lower == "modified" or col_lower == "date" or col_lower == "datetime" diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 4a95daafd82a9..ac464264d98d9 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -5119,13 +5119,13 @@ def _dtype_to_kind(dtype_str: str) -> str: """ dtype_str = _ensure_decoded(dtype_str) - if dtype_str.startswith("string") or dtype_str.startswith("bytes"): + if dtype_str.startswith(("string", "bytes")): kind = "string" elif dtype_str.startswith("float"): kind = "float" elif dtype_str.startswith("complex"): kind = "complex" - elif dtype_str.startswith("int") or dtype_str.startswith("uint"): + elif dtype_str.startswith(("int", "uint")): kind = "integer" elif dtype_str.startswith("datetime64"): kind = "datetime64" diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 155f989eb0634..fd6e98a6466c8 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -875,7 +875,6 @@ class StataMissingValue: float32_base = struct.pack(" bool: def _is_quarterly(rule: str) -> bool: rule = rule.upper() - return rule == "Q" or rule.startswith("Q-") or rule.startswith("BQ") + return rule == "Q" or rule.startswith(("Q-", "BQ")) def _is_monthly(rule: str) -> bool: diff --git a/pyproject.toml b/pyproject.toml index 8cd0155aca09e..725ca0a6951de 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -212,6 +212,8 @@ select = [ "Q", # pylint "PLE", "PLR", "PLW", + # misc lints + "PIE", ] ignore = [ diff --git a/scripts/validate_exception_location.py b/scripts/validate_exception_location.py index ebbe6c95a3ec9..b067527826de7 100644 --- a/scripts/validate_exception_location.py +++ b/scripts/validate_exception_location.py @@ -53,8 +53,7 @@ def visit_ClassDef(self, node) -> None: def is_an_exception_subclass(base_id: str) -> bool: return ( base_id == "Exception" - or base_id.endswith("Warning") - or base_id.endswith("Error") + or base_id.endswith(("Warning", "Error")) ) exception_classes = [] From 7bb6483fa7edd8a72fb065fcd0f692c3f6a6bef2 Mon Sep 17 00:00:00 2001 From: Victor Momodu Date: Thu, 16 Feb 2023 18:02:55 +0100 Subject: [PATCH 2/8] Formatted using black --- doc/source/user_guide/style.ipynb | 652 ++++++++++++++----------- scripts/validate_exception_location.py | 5 +- 2 files changed, 372 insertions(+), 285 deletions(-) diff --git a/doc/source/user_guide/style.ipynb b/doc/source/user_guide/style.ipynb index 90e57ad4ad90f..539885e1f6f03 100644 --- a/doc/source/user_guide/style.ipynb +++ b/doc/source/user_guide/style.ipynb @@ -47,6 +47,7 @@ "outputs": [], "source": [ "import matplotlib.pyplot\n", + "\n", "# We have this here to trigger matplotlib's font cache stuff.\n", "# This cell is hidden from the output" ] @@ -80,15 +81,12 @@ "import numpy as np\n", "import matplotlib as mpl\n", "\n", - "df = pd.DataFrame({\n", - " \"strings\": [\"Adam\", \"Mike\"],\n", - " \"ints\": [1, 3],\n", - " \"floats\": [1.123, 1000.23]\n", - "})\n", - "df.style \\\n", - " .format(precision=3, thousands=\".\", decimal=\",\") \\\n", - " .format_index(str.upper, axis=1) \\\n", - " .relabel_index([\"row 1\", \"row 2\"], axis=0)" + "df = pd.DataFrame(\n", + " {\"strings\": [\"Adam\", \"Mike\"], \"ints\": [1, 3], \"floats\": [1.123, 1000.23]}\n", + ")\n", + "df.style.format(precision=3, thousands=\".\", decimal=\",\").format_index(\n", + " str.upper, axis=1\n", + ").relabel_index([\"row 1\", \"row 2\"], axis=0)" ] }, { @@ -104,17 +102,21 @@ "metadata": {}, "outputs": [], "source": [ - "weather_df = pd.DataFrame(np.random.rand(10,2)*5, \n", - " index=pd.date_range(start=\"2021-01-01\", periods=10),\n", - " columns=[\"Tokyo\", \"Beijing\"])\n", + "weather_df = pd.DataFrame(\n", + " np.random.rand(10, 2) * 5,\n", + " index=pd.date_range(start=\"2021-01-01\", periods=10),\n", + " columns=[\"Tokyo\", \"Beijing\"],\n", + ")\n", + "\n", "\n", - "def rain_condition(v): \n", + "def rain_condition(v):\n", " if v < 1.75:\n", " return \"Dry\"\n", " elif v < 2.75:\n", " return \"Rain\"\n", " return \"Heavy Rain\"\n", "\n", + "\n", "def make_pretty(styler):\n", " styler.set_caption(\"Weather Conditions\")\n", " styler.format(rain_condition)\n", @@ -122,6 +124,7 @@ " styler.background_gradient(axis=None, vmin=1, vmax=5, cmap=\"YlGnBu\")\n", " return styler\n", "\n", + "\n", "weather_df" ] }, @@ -158,9 +161,7 @@ "outputs": [], "source": [ "df = pd.DataFrame(np.random.randn(5, 5))\n", - "df.style \\\n", - " .hide(subset=[0, 2, 4], axis=0) \\\n", - " .hide(subset=[0, 2, 4], axis=1)" + "df.style.hide(subset=[0, 2, 4], axis=0).hide(subset=[0, 2, 4], axis=1)" ] }, { @@ -177,9 +178,9 @@ "outputs": [], "source": [ "show = [0, 2, 4]\n", - "df.style \\\n", - " .hide([row for row in df.index if row not in show], axis=0) \\\n", - " .hide([col for col in df.columns if col not in show], axis=1)" + "df.style.hide([row for row in df.index if row not in show], axis=0).hide(\n", + " [col for col in df.columns if col not in show], axis=1\n", + ")" ] }, { @@ -199,9 +200,9 @@ "metadata": {}, "outputs": [], "source": [ - "summary_styler = df.agg([\"sum\", \"mean\"]).style \\\n", - " .format(precision=3) \\\n", - " .relabel_index([\"Sum\", \"Average\"])\n", + "summary_styler = (\n", + " df.agg([\"sum\", \"mean\"]).style.format(precision=3).relabel_index([\"Sum\", \"Average\"])\n", + ")\n", "df.style.format(precision=1).concat(summary_styler)" ] }, @@ -227,9 +228,16 @@ "metadata": {}, "outputs": [], "source": [ - "df = pd.DataFrame([[38.0, 2.0, 18.0, 22.0, 21, np.nan],[19, 439, 6, 452, 226,232]], \n", - " index=pd.Index(['Tumour (Positive)', 'Non-Tumour (Negative)'], name='Actual Label:'), \n", - " columns=pd.MultiIndex.from_product([['Decision Tree', 'Regression', 'Random'],['Tumour', 'Non-Tumour']], names=['Model:', 'Predicted:']))\n", + "df = pd.DataFrame(\n", + " [[38.0, 2.0, 18.0, 22.0, 21, np.nan], [19, 439, 6, 452, 226, 232]],\n", + " index=pd.Index(\n", + " [\"Tumour (Positive)\", \"Non-Tumour (Negative)\"], name=\"Actual Label:\"\n", + " ),\n", + " columns=pd.MultiIndex.from_product(\n", + " [[\"Decision Tree\", \"Regression\", \"Random\"], [\"Tumour\", \"Non-Tumour\"]],\n", + " names=[\"Model:\", \"Predicted:\"],\n", + " ),\n", + ")\n", "df.style" ] }, @@ -242,63 +250,66 @@ "outputs": [], "source": [ "# Hidden cell to just create the below example: code is covered throughout the guide.\n", - "s = df.style\\\n", - " .hide([('Random', 'Tumour'), ('Random', 'Non-Tumour')], axis='columns')\\\n", - " .format('{:.0f}')\\\n", - " .set_table_styles([{\n", - " 'selector': '',\n", - " 'props': 'border-collapse: separate;'\n", - " },{\n", - " 'selector': 'caption',\n", - " 'props': 'caption-side: bottom; font-size:1.3em;'\n", - " },{\n", - " 'selector': '.index_name',\n", - " 'props': 'font-style: italic; color: darkgrey; font-weight:normal;'\n", - " },{\n", - " 'selector': 'th:not(.index_name)',\n", - " 'props': 'background-color: #000066; color: white;'\n", - " },{\n", - " 'selector': 'th.col_heading',\n", - " 'props': 'text-align: center;'\n", - " },{\n", - " 'selector': 'th.col_heading.level0',\n", - " 'props': 'font-size: 1.5em;'\n", - " },{\n", - " 'selector': 'th.col2',\n", - " 'props': 'border-left: 1px solid white;'\n", - " },{\n", - " 'selector': '.col2',\n", - " 'props': 'border-left: 1px solid #000066;'\n", - " },{\n", - " 'selector': 'td',\n", - " 'props': 'text-align: center; font-weight:bold;'\n", - " },{\n", - " 'selector': '.true',\n", - " 'props': 'background-color: #e6ffe6;'\n", - " },{\n", - " 'selector': '.false',\n", - " 'props': 'background-color: #ffe6e6;'\n", - " },{\n", - " 'selector': '.border-red',\n", - " 'props': 'border: 2px dashed red;'\n", - " },{\n", - " 'selector': '.border-green',\n", - " 'props': 'border: 2px dashed green;'\n", - " },{\n", - " 'selector': 'td:hover',\n", - " 'props': 'background-color: #ffffb3;'\n", - " }])\\\n", - " .set_td_classes(pd.DataFrame([['true border-green', 'false', 'true', 'false border-red', '', ''],\n", - " ['false', 'true', 'false', 'true', '', '']], \n", - " index=df.index, columns=df.columns))\\\n", - " .set_caption(\"Confusion matrix for multiple cancer prediction models.\")\\\n", - " .set_tooltips(pd.DataFrame([['This model has a very strong true positive rate', '', '', \"This model's total number of false negatives is too high\", '', ''],\n", - " ['', '', '', '', '', '']], \n", - " index=df.index, columns=df.columns),\n", - " css_class='pd-tt', props=\n", - " 'visibility: hidden; position: absolute; z-index: 1; border: 1px solid #000066;'\n", - " 'background-color: white; color: #000066; font-size: 0.8em;' \n", - " 'transform: translate(0px, -24px); padding: 0.6em; border-radius: 0.5em;')\n" + "s = (\n", + " df.style.hide([(\"Random\", \"Tumour\"), (\"Random\", \"Non-Tumour\")], axis=\"columns\")\n", + " .format(\"{:.0f}\")\n", + " .set_table_styles(\n", + " [\n", + " {\"selector\": \"\", \"props\": \"border-collapse: separate;\"},\n", + " {\"selector\": \"caption\", \"props\": \"caption-side: bottom; font-size:1.3em;\"},\n", + " {\n", + " \"selector\": \".index_name\",\n", + " \"props\": \"font-style: italic; color: darkgrey; font-weight:normal;\",\n", + " },\n", + " {\n", + " \"selector\": \"th:not(.index_name)\",\n", + " \"props\": \"background-color: #000066; color: white;\",\n", + " },\n", + " {\"selector\": \"th.col_heading\", \"props\": \"text-align: center;\"},\n", + " {\"selector\": \"th.col_heading.level0\", \"props\": \"font-size: 1.5em;\"},\n", + " {\"selector\": \"th.col2\", \"props\": \"border-left: 1px solid white;\"},\n", + " {\"selector\": \".col2\", \"props\": \"border-left: 1px solid #000066;\"},\n", + " {\"selector\": \"td\", \"props\": \"text-align: center; font-weight:bold;\"},\n", + " {\"selector\": \".true\", \"props\": \"background-color: #e6ffe6;\"},\n", + " {\"selector\": \".false\", \"props\": \"background-color: #ffe6e6;\"},\n", + " {\"selector\": \".border-red\", \"props\": \"border: 2px dashed red;\"},\n", + " {\"selector\": \".border-green\", \"props\": \"border: 2px dashed green;\"},\n", + " {\"selector\": \"td:hover\", \"props\": \"background-color: #ffffb3;\"},\n", + " ]\n", + " )\n", + " .set_td_classes(\n", + " pd.DataFrame(\n", + " [\n", + " [\"true border-green\", \"false\", \"true\", \"false border-red\", \"\", \"\"],\n", + " [\"false\", \"true\", \"false\", \"true\", \"\", \"\"],\n", + " ],\n", + " index=df.index,\n", + " columns=df.columns,\n", + " )\n", + " )\n", + " .set_caption(\"Confusion matrix for multiple cancer prediction models.\")\n", + " .set_tooltips(\n", + " pd.DataFrame(\n", + " [\n", + " [\n", + " \"This model has a very strong true positive rate\",\n", + " \"\",\n", + " \"\",\n", + " \"This model's total number of false negatives is too high\",\n", + " \"\",\n", + " \"\",\n", + " ],\n", + " [\"\", \"\", \"\", \"\", \"\", \"\"],\n", + " ],\n", + " index=df.index,\n", + " columns=df.columns,\n", + " ),\n", + " css_class=\"pd-tt\",\n", + " props=\"visibility: hidden; position: absolute; z-index: 1; border: 1px solid #000066;\"\n", + " \"background-color: white; color: #000066; font-size: 0.8em;\"\n", + " \"transform: translate(0px, -24px); padding: 0.6em; border-radius: 0.5em;\",\n", + " )\n", + ")" ] }, { @@ -325,7 +336,9 @@ "metadata": {}, "outputs": [], "source": [ - "s = df.style.format('{:.0f}').hide([('Random', 'Tumour'), ('Random', 'Non-Tumour')], axis=\"columns\")\n", + "s = df.style.format(\"{:.0f}\").hide(\n", + " [(\"Random\", \"Tumour\"), (\"Random\", \"Non-Tumour\")], axis=\"columns\"\n", + ")\n", "s" ] }, @@ -337,8 +350,8 @@ }, "outputs": [], "source": [ - "# Hidden cell to avoid CSS clashes and latter code upcoding previous formatting \n", - "s.set_uuid('after_hide')" + "# Hidden cell to avoid CSS clashes and latter code upcoding previous formatting\n", + "s.set_uuid(\"after_hide\")" ] }, { @@ -395,16 +408,16 @@ "outputs": [], "source": [ "cell_hover = { # for row hover use instead of \n", - " 'selector': 'td:hover',\n", - " 'props': [('background-color', '#ffffb3')]\n", + " \"selector\": \"td:hover\",\n", + " \"props\": [(\"background-color\", \"#ffffb3\")],\n", "}\n", "index_names = {\n", - " 'selector': '.index_name',\n", - " 'props': 'font-style: italic; color: darkgrey; font-weight:normal;'\n", + " \"selector\": \".index_name\",\n", + " \"props\": \"font-style: italic; color: darkgrey; font-weight:normal;\",\n", "}\n", "headers = {\n", - " 'selector': 'th:not(.index_name)',\n", - " 'props': 'background-color: #000066; color: white;'\n", + " \"selector\": \"th:not(.index_name)\",\n", + " \"props\": \"background-color: #000066; color: white;\",\n", "}\n", "s.set_table_styles([cell_hover, index_names, headers])" ] @@ -417,8 +430,8 @@ }, "outputs": [], "source": [ - "# Hidden cell to avoid CSS clashes and latter code upcoding previous formatting \n", - "s.set_uuid('after_tab_styles1')" + "# Hidden cell to avoid CSS clashes and latter code upcoding previous formatting\n", + "s.set_uuid(\"after_tab_styles1\")" ] }, { @@ -434,11 +447,14 @@ "metadata": {}, "outputs": [], "source": [ - "s.set_table_styles([\n", - " {'selector': 'th.col_heading', 'props': 'text-align: center;'},\n", - " {'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;'},\n", - " {'selector': 'td', 'props': 'text-align: center; font-weight: bold;'},\n", - "], overwrite=False)" + "s.set_table_styles(\n", + " [\n", + " {\"selector\": \"th.col_heading\", \"props\": \"text-align: center;\"},\n", + " {\"selector\": \"th.col_heading.level0\", \"props\": \"font-size: 1.5em;\"},\n", + " {\"selector\": \"td\", \"props\": \"text-align: center; font-weight: bold;\"},\n", + " ],\n", + " overwrite=False,\n", + ")" ] }, { @@ -449,8 +465,8 @@ }, "outputs": [], "source": [ - "# Hidden cell to avoid CSS clashes and latter code upcoding previous formatting \n", - "s.set_uuid('after_tab_styles2')" + "# Hidden cell to avoid CSS clashes and latter code upcoding previous formatting\n", + "s.set_uuid(\"after_tab_styles2\")" ] }, { @@ -468,10 +484,16 @@ "metadata": {}, "outputs": [], "source": [ - "s.set_table_styles({\n", - " ('Regression', 'Tumour'): [{'selector': 'th', 'props': 'border-left: 1px solid white'},\n", - " {'selector': 'td', 'props': 'border-left: 1px solid #000066'}]\n", - "}, overwrite=False, axis=0)" + "s.set_table_styles(\n", + " {\n", + " (\"Regression\", \"Tumour\"): [\n", + " {\"selector\": \"th\", \"props\": \"border-left: 1px solid white\"},\n", + " {\"selector\": \"td\", \"props\": \"border-left: 1px solid #000066\"},\n", + " ]\n", + " },\n", + " overwrite=False,\n", + " axis=0,\n", + ")" ] }, { @@ -482,8 +504,8 @@ }, "outputs": [], "source": [ - "# Hidden cell to avoid CSS clashes and latter code upcoding previous formatting \n", - "s.set_uuid('xyz01')" + "# Hidden cell to avoid CSS clashes and latter code upcoding previous formatting\n", + "s.set_uuid(\"xyz01\")" ] }, { @@ -508,7 +530,7 @@ "outputs": [], "source": [ "out = s.set_table_attributes('class=\"my-table-cls\"').to_html()\n", - "print(out[out.find('