From 9a5ffd2c228fc06deef0fd6869a84662fa8b354d Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 26 Jan 2024 15:07:56 -0800 Subject: [PATCH 1/6] Use str.captialize instead of helper --- pandas/core/dtypes/dtypes.py | 4 +--- pandas/util/__init__.py | 4 ---- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 5b51bc9debb33..f137028976e53 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -62,8 +62,6 @@ is_list_like, ) -from pandas.util import capitalize_first_letter - if not pa_version_under10p1: import pyarrow as pa @@ -1087,7 +1085,7 @@ def na_value(self) -> NaTType: def __eq__(self, other: object) -> bool: if isinstance(other, str): - return other in [self.name, capitalize_first_letter(self.name)] + return other in {self.name, self.name.capitalize()} return super().__eq__(other) diff --git a/pandas/util/__init__.py b/pandas/util/__init__.py index 91282fde8b11d..8fe928ed6c5cf 100644 --- a/pandas/util/__init__.py +++ b/pandas/util/__init__.py @@ -23,7 +23,3 @@ def __getattr__(key: str): return cache_readonly raise AttributeError(f"module 'pandas.util' has no attribute '{key}'") - - -def capitalize_first_letter(s: str) -> str: - return s[:1].upper() + s[1:] From 00f09921c2238cac4d80b495fef896fff617bea1 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 26 Jan 2024 16:01:54 -0800 Subject: [PATCH 2/6] Use title instead --- pandas/core/dtypes/dtypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index f137028976e53..83365eb552c90 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -1085,7 +1085,7 @@ def na_value(self) -> NaTType: def __eq__(self, other: object) -> bool: if isinstance(other, str): - return other in {self.name, self.name.capitalize()} + return other in {self.name, self.name.title()} return super().__eq__(other) From b775e2161e6762460f8bd2d9af567c5b441687d3 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:06:53 -0800 Subject: [PATCH 3/6] Use string capwords --- pandas/core/dtypes/dtypes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 83365eb552c90..2e78434109d28 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -11,6 +11,7 @@ ) from decimal import Decimal import re +import string from typing import ( TYPE_CHECKING, Any, @@ -1085,7 +1086,7 @@ def na_value(self) -> NaTType: def __eq__(self, other: object) -> bool: if isinstance(other, str): - return other in {self.name, self.name.title()} + return string.capwords(other) == string.capwords(self.name) return super().__eq__(other) From be1df815be9f85f3b9ee4cc04318554c2d1b8efe Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Tue, 30 Jan 2024 17:07:49 -0800 Subject: [PATCH 4/6] Pylint --- pandas/core/dtypes/dtypes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 2e78434109d28..01a609fb5487f 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -11,7 +11,7 @@ ) from decimal import Decimal import re -import string +import string as string_stdlib from typing import ( TYPE_CHECKING, Any, @@ -1086,7 +1086,7 @@ def na_value(self) -> NaTType: def __eq__(self, other: object) -> bool: if isinstance(other, str): - return string.capwords(other) == string.capwords(self.name) + return string_stdlib.capwords(other) == string_stdlib.capwords(self.name) return super().__eq__(other) From 19fa57572406889eb94b741d7fdc5bb19cf3e00d Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 31 Jan 2024 11:38:28 -0800 Subject: [PATCH 5/6] Just use helper function --- pandas/core/dtypes/dtypes.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 01a609fb5487f..a9f979d421472 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -11,7 +11,6 @@ ) from decimal import Decimal import re -import string as string_stdlib from typing import ( TYPE_CHECKING, Any, @@ -103,6 +102,11 @@ str_type = str +def capitalize_first_letter(s: str) -> str: + # For PeriodDtype.__eq__ + return s[:1].upper() + s[1:] + + class PandasExtensionDtype(ExtensionDtype): """ A np.dtype duck-typed class, suitable for holding a custom dtype. @@ -1086,7 +1090,7 @@ def na_value(self) -> NaTType: def __eq__(self, other: object) -> bool: if isinstance(other, str): - return string_stdlib.capwords(other) == string_stdlib.capwords(self.name) + return other in {self.name, capitalize_first_letter(self.name)} return super().__eq__(other) From 8ce54ce62333bf1bc210daafd6f181f351661452 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 1 Feb 2024 11:04:57 -0800 Subject: [PATCH 6/6] Remove helper function --- pandas/core/dtypes/dtypes.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index a9f979d421472..a6a5f142faf1c 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -102,11 +102,6 @@ str_type = str -def capitalize_first_letter(s: str) -> str: - # For PeriodDtype.__eq__ - return s[:1].upper() + s[1:] - - class PandasExtensionDtype(ExtensionDtype): """ A np.dtype duck-typed class, suitable for holding a custom dtype. @@ -1090,7 +1085,7 @@ def na_value(self) -> NaTType: def __eq__(self, other: object) -> bool: if isinstance(other, str): - return other in {self.name, capitalize_first_letter(self.name)} + return other[:1].lower() + other[1:] == self.name return super().__eq__(other)