From 2417b45f872d3a53a02f9809269b4d20fe10c102 Mon Sep 17 00:00:00 2001 From: Marie Knapp Date: Sun, 27 Nov 2022 18:23:17 -0800 Subject: [PATCH 01/11] changed decimal module import, Decimal function call in EngFormatter class __call__ function --- pandas/io/formats/format.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index cdc21f04da43a..05d00897241c4 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -9,7 +9,7 @@ QUOTE_NONE, QUOTE_NONNUMERIC, ) -import decimal +from decimal import Decimal from functools import partial from io import StringIO import math @@ -2071,12 +2071,12 @@ def __call__(self, num: float) -> str: @return: engineering formatted string """ - dnum = decimal.Decimal(str(num)) + dnum = Decimal(str(num)) - if decimal.Decimal.is_nan(dnum): + if Decimal.is_nan(dnum): return "NaN" - if decimal.Decimal.is_infinite(dnum): + if Decimal.is_infinite(dnum): return "inf" sign = 1 @@ -2086,9 +2086,9 @@ def __call__(self, num: float) -> str: dnum = -dnum if dnum != 0: - pow10 = decimal.Decimal(int(math.floor(dnum.log10() / 3) * 3)) + pow10 = Decimal(int(math.floor(dnum.log10() / 3) * 3)) else: - pow10 = decimal.Decimal(0) + pow10 = Decimal(0) pow10 = pow10.min(max(self.ENG_PREFIXES.keys())) pow10 = pow10.max(min(self.ENG_PREFIXES.keys())) From 6e93179824e025e9d19b531674ebc2ebfd5ddd5d Mon Sep 17 00:00:00 2001 From: Marie Knapp Date: Sun, 27 Nov 2022 18:26:43 -0800 Subject: [PATCH 02/11] changed name of parameter for get_format_datetime64 to avoid redefined outer name conflict with is_dates_only function --- pandas/io/formats/format.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 05d00897241c4..9033735a7413a 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1794,12 +1794,12 @@ def _format_datetime64_dateonly( def get_format_datetime64( - is_dates_only: bool, nat_rep: str = "NaT", date_format: str | None = None + is_dates_only_result: bool, nat_rep: str = "NaT", date_format: str | None = None ) -> Callable: """Return a formatter callable taking a datetime64 as input and providing a string as output""" - if is_dates_only: + if is_dates_only_result: return lambda x: _format_datetime64_dateonly( x, nat_rep=nat_rep, date_format=date_format ) From f46e00d08d9af855c92eb7b5f42d88506ef9a053 Mon Sep 17 00:00:00 2001 From: Marie Knapp Date: Sun, 27 Nov 2022 18:27:29 -0800 Subject: [PATCH 03/11] change in argument name for call to get_format_datetime64 to match change in datetimes.py --- pandas/core/indexes/datetimes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 5709f94e2ccd5..9f168b4208007 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -384,7 +384,7 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool: def _formatter_func(self): from pandas.io.formats.format import get_format_datetime64 - formatter = get_format_datetime64(is_dates_only=self._is_dates_only) + formatter = get_format_datetime64(is_dates_only_result=self._is_dates_only) return lambda x: f"'{formatter(x)}'" # -------------------------------------------------------------------- From 868e8b402fe97c44cbc90db29c9f264965e26c7f Mon Sep 17 00:00:00 2001 From: Marie Knapp Date: Sun, 27 Nov 2022 19:09:26 -0800 Subject: [PATCH 04/11] removed import - justify function not used and creating redefined outer name conflict with justify functions defined for TextAdjustment and EastAsianTextAdjustment classes --- pandas/io/formats/format.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 9033735a7413a..6d911327c9f67 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -108,7 +108,6 @@ ) from pandas.io.formats.printing import ( adjoin, - justify, pprint_thing, ) From 0f9c688b1715fff5a981d446cbf7b13558c34e01 Mon Sep 17 00:00:00 2001 From: Marie Knapp Date: Sun, 27 Nov 2022 20:06:26 -0800 Subject: [PATCH 05/11] replaced justify import with alias to avoid redefined-outer-name conflict --- pandas/io/formats/format.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 6d911327c9f67..c3d0d7a9617f3 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -108,6 +108,7 @@ ) from pandas.io.formats.printing import ( adjoin, + justify as jst, pprint_thing, ) @@ -432,7 +433,7 @@ def len(self, text: str) -> int: return len(text) def justify(self, texts: Any, max_len: int, mode: str = "right") -> list[str]: - return justify(texts, max_len, mode=mode) + return jst(texts, max_len, mode=mode) def adjoin(self, space: int, *lists, **kwargs) -> str: return adjoin(space, *lists, strlen=self.len, justfunc=self.justify, **kwargs) From f4473fdf9b4aa36759f26772d318ba8b982a3523 Mon Sep 17 00:00:00 2001 From: Marie Knapp Date: Mon, 28 Nov 2022 08:43:10 -0800 Subject: [PATCH 06/11] renamed imports from printing module --- pandas/io/formats/format.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index c3d0d7a9617f3..7b0b5d3655324 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -106,11 +106,7 @@ check_parent_directory, stringify_path, ) -from pandas.io.formats.printing import ( - adjoin, - justify as jst, - pprint_thing, -) +from pandas.io.formats import printing if TYPE_CHECKING: from pandas import ( @@ -339,7 +335,7 @@ def _get_footer(self) -> str: if footer: footer += ", " - series_name = pprint_thing(name, escape_chars=("\t", "\r", "\n")) + series_name = printing.pprint_thing(name, escape_chars=("\t", "\r", "\n")) footer += f"Name: {series_name}" if self.length is True or ( @@ -354,7 +350,7 @@ def _get_footer(self) -> str: if dtype_name: if footer: footer += ", " - footer += f"dtype: {pprint_thing(dtype_name)}" + footer += f"dtype: {printing.pprint_thing(dtype_name)}" # level infos are added to the end and in a new line, like it is done # for Categoricals @@ -433,10 +429,12 @@ def len(self, text: str) -> int: return len(text) def justify(self, texts: Any, max_len: int, mode: str = "right") -> list[str]: - return jst(texts, max_len, mode=mode) + return printing.justify(texts, max_len, mode=mode) def adjoin(self, space: int, *lists, **kwargs) -> str: - return adjoin(space, *lists, strlen=self.len, justfunc=self.justify, **kwargs) + return printing.adjoin( + space, *lists, strlen=self.len, justfunc=self.justify, **kwargs + ) class EastAsianTextAdjustment(TextAdjustment): @@ -1375,7 +1373,7 @@ def _format_strings(self) -> list[str]: else: quote_strings = self.quoting is not None and self.quoting != QUOTE_NONE formatter = partial( - pprint_thing, + printing.pprint_thing, escape_chars=("\t", "\r", "\n"), quote_strings=quote_strings, ) From b60047327aa9cb168b724096350edec4b06d2a11 Mon Sep 17 00:00:00 2001 From: Marie Knapp Date: Tue, 29 Nov 2022 08:53:36 -0800 Subject: [PATCH 07/11] function renamed to is_dates_only_, returned argument for get_formate_datetime64 to is_dates_only --- pandas/io/formats/format.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 7b0b5d3655324..f0ed79106d100 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1742,7 +1742,7 @@ def format_percentiles( return [i + "%" for i in out] -def is_dates_only(values: np.ndarray | DatetimeArray | Index | DatetimeIndex) -> bool: +def is_dates_only_(values: np.ndarray | DatetimeArray | Index | DatetimeIndex) -> bool: # return a boolean if we are only dates (and don't have a timezone) if not isinstance(values, Index): values = values.ravel() @@ -1792,12 +1792,12 @@ def _format_datetime64_dateonly( def get_format_datetime64( - is_dates_only_result: bool, nat_rep: str = "NaT", date_format: str | None = None + is_dates_only: bool, nat_rep: str = "NaT", date_format: str | None = None ) -> Callable: """Return a formatter callable taking a datetime64 as input and providing a string as output""" - if is_dates_only_result: + if is_dates_only: return lambda x: _format_datetime64_dateonly( x, nat_rep=nat_rep, date_format=date_format ) @@ -1814,7 +1814,7 @@ def get_format_datetime64_from_values( # only accepts 1D values values = values.ravel() - ido = is_dates_only(values) + ido = is_dates_only_(values) if ido: # Only dates and no timezone: provide a default format return date_format or "%Y-%m-%d" @@ -1825,7 +1825,7 @@ class Datetime64TZFormatter(Datetime64Formatter): def _format_strings(self) -> list[str]: """we by definition have a TZ""" values = self.values.astype(object) - ido = is_dates_only(values) + ido = is_dates_only_(values) formatter = self.formatter or get_format_datetime64( ido, date_format=self.date_format ) From bbfa7bd007501e3761927a259649ba63c109fd75 Mon Sep 17 00:00:00 2001 From: Marie Knapp Date: Tue, 29 Nov 2022 09:04:47 -0800 Subject: [PATCH 08/11] changes in datetimes.py to reflect function and argument names in format.py --- pandas/core/indexes/datetimes.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 9f168b4208007..c559186612be1 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -356,12 +356,14 @@ def _is_dates_only(self) -> bool: ------- bool """ - from pandas.io.formats.format import is_dates_only + from pandas.io.formats.format import is_dates_only_ # error: Argument 1 to "is_dates_only" has incompatible type # "Union[ExtensionArray, ndarray]"; expected "Union[ndarray, # DatetimeArray, Index, DatetimeIndex]" - return self.tz is None and is_dates_only(self._values) # type: ignore[arg-type] + return self.tz is None and is_dates_only_( + self._values + ) # type: ignore[arg-type] def __reduce__(self): d = {"data": self._data, "name": self.name} @@ -384,7 +386,7 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool: def _formatter_func(self): from pandas.io.formats.format import get_format_datetime64 - formatter = get_format_datetime64(is_dates_only_result=self._is_dates_only) + formatter = get_format_datetime64(is_dates_only=self._is_dates_only) return lambda x: f"'{formatter(x)}'" # -------------------------------------------------------------------- From 6002af1a60b3445fb8100f9de3986f060e403c2f Mon Sep 17 00:00:00 2001 From: Marie Knapp Date: Tue, 29 Nov 2022 09:08:56 -0800 Subject: [PATCH 09/11] argument and function names swapped --- pandas/core/indexes/datetimes.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index c559186612be1..9f17bf6858d30 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -356,14 +356,12 @@ def _is_dates_only(self) -> bool: ------- bool """ - from pandas.io.formats.format import is_dates_only_ + from pandas.io.formats.format import is_dates_only # error: Argument 1 to "is_dates_only" has incompatible type # "Union[ExtensionArray, ndarray]"; expected "Union[ndarray, # DatetimeArray, Index, DatetimeIndex]" - return self.tz is None and is_dates_only_( - self._values - ) # type: ignore[arg-type] + return self.tz is None and is_dates_only(self._values) # type: ignore[arg-type] def __reduce__(self): d = {"data": self._data, "name": self.name} @@ -386,7 +384,7 @@ def _is_comparable_dtype(self, dtype: DtypeObj) -> bool: def _formatter_func(self): from pandas.io.formats.format import get_format_datetime64 - formatter = get_format_datetime64(is_dates_only=self._is_dates_only) + formatter = get_format_datetime64(is_dates_only_=self._is_dates_only) return lambda x: f"'{formatter(x)}'" # -------------------------------------------------------------------- From 851f0b8fab62fe5c253cea2f13b56d74f212cddb Mon Sep 17 00:00:00 2001 From: Marie Knapp Date: Tue, 29 Nov 2022 09:10:39 -0800 Subject: [PATCH 10/11] function name is is_dates_only, argument for get_format_datetime64 is is_dates_only_ --- pandas/io/formats/format.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index f0ed79106d100..61c12f5011886 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1742,7 +1742,7 @@ def format_percentiles( return [i + "%" for i in out] -def is_dates_only_(values: np.ndarray | DatetimeArray | Index | DatetimeIndex) -> bool: +def is_dates_only(values: np.ndarray | DatetimeArray | Index | DatetimeIndex) -> bool: # return a boolean if we are only dates (and don't have a timezone) if not isinstance(values, Index): values = values.ravel() @@ -1792,12 +1792,12 @@ def _format_datetime64_dateonly( def get_format_datetime64( - is_dates_only: bool, nat_rep: str = "NaT", date_format: str | None = None + is_dates_only_: bool, nat_rep: str = "NaT", date_format: str | None = None ) -> Callable: """Return a formatter callable taking a datetime64 as input and providing a string as output""" - if is_dates_only: + if is_dates_only_: return lambda x: _format_datetime64_dateonly( x, nat_rep=nat_rep, date_format=date_format ) @@ -1814,7 +1814,7 @@ def get_format_datetime64_from_values( # only accepts 1D values values = values.ravel() - ido = is_dates_only_(values) + ido = is_dates_only(values) if ido: # Only dates and no timezone: provide a default format return date_format or "%Y-%m-%d" @@ -1825,7 +1825,7 @@ class Datetime64TZFormatter(Datetime64Formatter): def _format_strings(self) -> list[str]: """we by definition have a TZ""" values = self.values.astype(object) - ido = is_dates_only_(values) + ido = is_dates_only(values) formatter = self.formatter or get_format_datetime64( ido, date_format=self.date_format ) From 9c897c4f57ba455c2e4912441d00fe4982569e46 Mon Sep 17 00:00:00 2001 From: Marie Knapp Date: Tue, 29 Nov 2022 09:34:13 -0800 Subject: [PATCH 11/11] Update .pre-commit-config.yaml removed format and datetimes files --- .pre-commit-config.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cc6875589c691..97a9748de8513 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -79,8 +79,6 @@ repos: |^pandas/util/_test_decorators\.py # keep excluded |^pandas/_version\.py # keep excluded |^pandas/conftest\.py # keep excluded - |^pandas/core/tools/datetimes\.py - |^pandas/io/formats/format\.py |^pandas/core/generic\.py args: [--disable=all, --enable=redefined-outer-name] stages: [manual]