From 2580e0a70f1bebac051a6f85c6f4069dcf314a2a Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 1 Nov 2024 13:38:44 -0700 Subject: [PATCH 1/3] Replace while loops --- pandas/core/strings/accessor.py | 6 +++--- pandas/plotting/_matplotlib/tools.py | 10 ++++++---- pandas/tseries/holiday.py | 10 ++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index 05e1a36877e06..5ec4de4ee792b 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -423,7 +423,7 @@ def cons_row(x): result.name = name return result - def _get_series_list(self, others): + def _get_series_list(self, others) -> list[Series]: """ Auxiliary function for :meth:`str.cat`. Turn potentially mixed input into a list of Series (elements without an index must match the length @@ -474,8 +474,8 @@ def _get_series_list(self, others): for x in others ): los: list[Series] = [] - while others: # iterate through list and append each element - los = los + self._get_series_list(others.pop(0)) + for other in others: + los.extend(self._get_series_list(other)) return los # ... or just strings elif all(not is_list_like(x) for x in others): diff --git a/pandas/plotting/_matplotlib/tools.py b/pandas/plotting/_matplotlib/tools.py index d5624aecd1215..5ebbdf886eb32 100644 --- a/pandas/plotting/_matplotlib/tools.py +++ b/pandas/plotting/_matplotlib/tools.py @@ -1,7 +1,11 @@ # being a bit too dynamic from __future__ import annotations -from math import ceil +from math import ( + ceil, + floor, + log2, +) from typing import TYPE_CHECKING import warnings @@ -126,9 +130,7 @@ def _get_layout( try: return layouts[nplots] except KeyError: - k = 1 - while k**2 < nplots: - k += 1 + k = floor(log2(nplots)) if (k - 1) * k >= nplots: return k, (k - 1) diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py index bf4ec2e551f01..b9ef557cb1d15 100644 --- a/pandas/tseries/holiday.py +++ b/pandas/tseries/holiday.py @@ -116,9 +116,8 @@ def next_workday(dt: datetime) -> datetime: returns next workday used for observances """ dt += timedelta(days=1) - while dt.weekday() > 4: - # Mon-Fri are 0-4 - dt += timedelta(days=1) + # Mon-Fri are 0-4 + dt += timedelta(days=max(dt.weekday() - 4, 0)) return dt @@ -127,9 +126,8 @@ def previous_workday(dt: datetime) -> datetime: returns previous workday used for observances """ dt -= timedelta(days=1) - while dt.weekday() > 4: - # Mon-Fri are 0-4 - dt -= timedelta(days=1) + # Mon-Fri are 0-4 + dt -= timedelta(days=max(dt.weekday() - 4, 0)) return dt From e3013a7320a5fa5c2c579800ed5c51a3889ff5fa Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 3 Feb 2025 11:10:14 -0800 Subject: [PATCH 2/3] +1 to _get_layout --- pandas/plotting/_matplotlib/tools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/tools.py b/pandas/plotting/_matplotlib/tools.py index 6c315c4dce184..d152735842f45 100644 --- a/pandas/plotting/_matplotlib/tools.py +++ b/pandas/plotting/_matplotlib/tools.py @@ -130,7 +130,7 @@ def _get_layout( try: return layouts[nplots] except KeyError: - k = floor(log2(nplots)) + k = floor(log2(nplots)) + 1 if (k - 1) * k >= nplots: return k, (k - 1) From 4d4fb4ecc3603d46eb87ee5793fe7d25d92bdd61 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 3 Feb 2025 11:16:20 -0800 Subject: [PATCH 3/3] Undo holiday changes --- pandas/tseries/holiday.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py index c95b9e96b1d44..2d195fbbc4e84 100644 --- a/pandas/tseries/holiday.py +++ b/pandas/tseries/holiday.py @@ -116,8 +116,9 @@ def next_workday(dt: datetime) -> datetime: returns next workday used for observances """ dt += timedelta(days=1) - # Mon-Fri are 0-4 - dt += timedelta(days=max(dt.weekday() - 4, 0)) + while dt.weekday() > 4: + # Mon-Fri are 0-4 + dt += timedelta(days=1) return dt @@ -126,8 +127,9 @@ def previous_workday(dt: datetime) -> datetime: returns previous workday used for observances """ dt -= timedelta(days=1) - # Mon-Fri are 0-4 - dt -= timedelta(days=max(dt.weekday() - 4, 0)) + while dt.weekday() > 4: + # Mon-Fri are 0-4 + dt -= timedelta(days=1) return dt