Skip to content

Commit b1b5eef

Browse files
jbrockmendelfeefladder
authored andcommitted
PERF: setting PeriodDtype column (pandas-dev#43308)
1 parent f95ec17 commit b1b5eef

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

pandas/core/dtypes/dtypes.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,18 @@ def __eq__(self, other: Any) -> bool:
936936
if isinstance(other, str):
937937
return other in [self.name, self.name.title()]
938938

939-
return isinstance(other, PeriodDtype) and self.freq == other.freq
939+
elif isinstance(other, PeriodDtype):
940+
941+
# For freqs that can be held by a PeriodDtype, this check is
942+
# equivalent to (and much faster than) self.freq == other.freq
943+
sfreq = self.freq
944+
ofreq = other.freq
945+
return (
946+
sfreq.n == ofreq.n
947+
and sfreq._period_dtype_code == ofreq._period_dtype_code
948+
)
949+
950+
return False
940951

941952
def __ne__(self, other: Any) -> bool:
942953
return not self.__eq__(other)

pandas/core/internals/blocks.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,11 @@ def should_store(self, value: ArrayLike) -> bool:
624624
-------
625625
bool
626626
"""
627-
return is_dtype_equal(value.dtype, self.dtype)
627+
# faster equivalent to is_dtype_equal(value.dtype, self.dtype)
628+
try:
629+
return value.dtype == self.dtype
630+
except TypeError:
631+
return False
628632

629633
@final
630634
def to_native_types(self, na_rep="nan", quoting=None, **kwargs):

pandas/core/internals/managers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ def iset(self, loc: int | slice | np.ndarray, value: ArrayLike):
10111011
Set new item in-place. Does not consolidate. Adds new Block if not
10121012
contained in the current set of items
10131013
"""
1014-
value = extract_array(value, extract_numpy=True)
1014+
10151015
# FIXME: refactor, clearly separate broadcasting & zip-like assignment
10161016
# can prob also fix the various if tests for sparse/categorical
10171017
if self._blklocs is None and self.ndim > 1:

0 commit comments

Comments
 (0)