Skip to content

Commit 024159b

Browse files
authored
CLN: de-duplicate reindex/align code (#40968)
1 parent 0a3cbd9 commit 024159b

File tree

1 file changed

+28
-33
lines changed

1 file changed

+28
-33
lines changed

pandas/core/generic.py

+28-33
Original file line numberDiff line numberDiff line change
@@ -8813,15 +8813,7 @@ def _align_frame(
88138813
right = right.fillna(method=method, axis=fill_axis, limit=limit)
88148814

88158815
# if DatetimeIndex have different tz, convert to UTC
8816-
if is_datetime64tz_dtype(left.index.dtype):
8817-
if left.index.tz != right.index.tz:
8818-
if join_index is not None:
8819-
# GH#33671 ensure we don't change the index on
8820-
# our original Series (NB: by default deep=False)
8821-
left = left.copy()
8822-
right = right.copy()
8823-
left.index = join_index
8824-
right.index = join_index
8816+
left, right = _align_as_utc(left, right, join_index)
88258817

88268818
return (
88278819
left.__finalize__(self),
@@ -8863,27 +8855,18 @@ def _align_series(
88638855
else:
88648856
# one has > 1 ndim
88658857
fdata = self._mgr
8866-
if axis == 0:
8867-
join_index = self.index
8858+
if axis in [0, 1]:
8859+
join_index = self.axes[axis]
88688860
lidx, ridx = None, None
8869-
if not self.index.equals(other.index):
8870-
join_index, lidx, ridx = self.index.join(
8861+
if not join_index.equals(other.index):
8862+
join_index, lidx, ridx = join_index.join(
88718863
other.index, how=join, level=level, return_indexers=True
88728864
)
88738865

88748866
if lidx is not None:
8875-
fdata = fdata.reindex_indexer(join_index, lidx, axis=1)
8867+
bm_axis = self._get_block_manager_axis(axis)
8868+
fdata = fdata.reindex_indexer(join_index, lidx, axis=bm_axis)
88768869

8877-
elif axis == 1:
8878-
join_index = self.columns
8879-
lidx, ridx = None, None
8880-
if not self.columns.equals(other.index):
8881-
join_index, lidx, ridx = self.columns.join(
8882-
other.index, how=join, level=level, return_indexers=True
8883-
)
8884-
8885-
if lidx is not None:
8886-
fdata = fdata.reindex_indexer(join_index, lidx, axis=0)
88878870
else:
88888871
raise ValueError("Must specify axis=0 or 1")
88898872

@@ -8905,15 +8888,7 @@ def _align_series(
89058888

89068889
# if DatetimeIndex have different tz, convert to UTC
89078890
if is_series or (not is_series and axis == 0):
8908-
if is_datetime64tz_dtype(left.index.dtype):
8909-
if left.index.tz != right.index.tz:
8910-
if join_index is not None:
8911-
# GH#33671 ensure we don't change the index on
8912-
# our original Series (NB: by default deep=False)
8913-
left = left.copy()
8914-
right = right.copy()
8915-
left.index = join_index
8916-
right.index = join_index
8891+
left, right = _align_as_utc(left, right, join_index)
89178892

89188893
return (
89198894
left.__finalize__(self),
@@ -11887,3 +11862,23 @@ def _doc_params(cls):
1188711862
The required number of valid values to perform the operation. If fewer than
1188811863
``min_count`` non-NA values are present the result will be NA.
1188911864
"""
11865+
11866+
11867+
def _align_as_utc(
11868+
left: FrameOrSeries, right: FrameOrSeries, join_index: Index | None
11869+
) -> tuple[FrameOrSeries, FrameOrSeries]:
11870+
"""
11871+
If we are aligning timezone-aware DatetimeIndexes and the timezones
11872+
do not match, convert both to UTC.
11873+
"""
11874+
if is_datetime64tz_dtype(left.index.dtype):
11875+
if left.index.tz != right.index.tz:
11876+
if join_index is not None:
11877+
# GH#33671 ensure we don't change the index on
11878+
# our original Series (NB: by default deep=False)
11879+
left = left.copy()
11880+
right = right.copy()
11881+
left.index = join_index
11882+
right.index = join_index
11883+
11884+
return left, right

0 commit comments

Comments
 (0)