68
68
Manager ,
69
69
NaPosition ,
70
70
NDFrameT ,
71
- NDFrameTb ,
72
71
RandomState ,
73
72
Renamer ,
74
73
Scalar ,
@@ -9445,7 +9444,7 @@ def align(
9445
9444
{c : self for c in other .columns }, ** other ._construct_axes_dict ()
9446
9445
)
9447
9446
# error: Incompatible return value type (got "Tuple[DataFrame,
9448
- # DataFrame]", expected "Tuple[NDFrameT, NDFrameTb ]")
9447
+ # DataFrame]", expected "Tuple[Self, NDFrameT ]")
9449
9448
return df ._align_frame ( # type: ignore[return-value]
9450
9449
other , # type: ignore[arg-type]
9451
9450
join = join ,
@@ -9456,7 +9455,7 @@ def align(
9456
9455
method = method ,
9457
9456
limit = limit ,
9458
9457
fill_axis = fill_axis ,
9459
- )
9458
+ )[: 2 ]
9460
9459
elif isinstance (other , ABCSeries ):
9461
9460
# this means self is a DataFrame, and we need to broadcast
9462
9461
# other
@@ -9465,7 +9464,7 @@ def align(
9465
9464
{c : other for c in self .columns }, ** self ._construct_axes_dict ()
9466
9465
)
9467
9466
# error: Incompatible return value type (got "Tuple[NDFrameT,
9468
- # DataFrame]", expected "Tuple[NDFrameT, NDFrameTb ]")
9467
+ # DataFrame]", expected "Tuple[Self, NDFrameT ]")
9469
9468
return self ._align_frame ( # type: ignore[return-value]
9470
9469
df ,
9471
9470
join = join ,
@@ -9476,14 +9475,13 @@ def align(
9476
9475
method = method ,
9477
9476
limit = limit ,
9478
9477
fill_axis = fill_axis ,
9479
- )
9478
+ )[: 2 ]
9480
9479
9480
+ _right : DataFrame | Series
9481
9481
if axis is not None :
9482
9482
axis = self ._get_axis_number (axis )
9483
9483
if isinstance (other , ABCDataFrame ):
9484
- # error: Incompatible return value type (got "Tuple[NDFrameT, DataFrame]",
9485
- # expected "Tuple[NDFrameT, NDFrameTb]")
9486
- return self ._align_frame ( # type: ignore[return-value]
9484
+ left , _right , join_index = self ._align_frame (
9487
9485
other ,
9488
9486
join = join ,
9489
9487
axis = axis ,
@@ -9494,10 +9492,9 @@ def align(
9494
9492
limit = limit ,
9495
9493
fill_axis = fill_axis ,
9496
9494
)
9495
+
9497
9496
elif isinstance (other , ABCSeries ):
9498
- # error: Incompatible return value type (got "Tuple[NDFrameT, Series]",
9499
- # expected "Tuple[NDFrameT, NDFrameTb]")
9500
- return self ._align_series ( # type: ignore[return-value]
9497
+ left , _right , join_index = self ._align_series (
9501
9498
other ,
9502
9499
join = join ,
9503
9500
axis = axis ,
@@ -9511,9 +9508,27 @@ def align(
9511
9508
else : # pragma: no cover
9512
9509
raise TypeError (f"unsupported type: { type (other )} " )
9513
9510
9511
+ right = cast (NDFrameT , _right )
9512
+ if self .ndim == 1 or axis == 0 :
9513
+ # If we are aligning timezone-aware DatetimeIndexes and the timezones
9514
+ # do not match, convert both to UTC.
9515
+ if is_datetime64tz_dtype (left .index .dtype ):
9516
+ if left .index .tz != right .index .tz :
9517
+ if join_index is not None :
9518
+ # GH#33671 copy to ensure we don't change the index on
9519
+ # our original Series
9520
+ left = left .copy (deep = False )
9521
+ right = right .copy (deep = False )
9522
+ left .index = join_index
9523
+ right .index = join_index
9524
+
9525
+ left = left .__finalize__ (self )
9526
+ right = right .__finalize__ (other )
9527
+ return left , right
9528
+
9514
9529
@final
9515
9530
def _align_frame (
9516
- self : NDFrameT ,
9531
+ self ,
9517
9532
other : DataFrame ,
9518
9533
join : AlignJoin = "outer" ,
9519
9534
axis : Axis | None = None ,
@@ -9523,7 +9538,7 @@ def _align_frame(
9523
9538
method = None ,
9524
9539
limit = None ,
9525
9540
fill_axis : Axis = 0 ,
9526
- ) -> tuple [NDFrameT , DataFrame ]:
9541
+ ) -> tuple [Self , DataFrame , Index | None ]:
9527
9542
# defaults
9528
9543
join_index , join_columns = None , None
9529
9544
ilidx , iridx = None , None
@@ -9562,22 +9577,14 @@ def _align_frame(
9562
9577
)
9563
9578
9564
9579
if method is not None :
9565
- _left = left .fillna (method = method , axis = fill_axis , limit = limit )
9566
- assert _left is not None # needed for mypy
9567
- left = _left
9580
+ left = left .fillna (method = method , axis = fill_axis , limit = limit )
9568
9581
right = right .fillna (method = method , axis = fill_axis , limit = limit )
9569
9582
9570
- # if DatetimeIndex have different tz, convert to UTC
9571
- left , right = _align_as_utc (left , right , join_index )
9572
-
9573
- return (
9574
- left .__finalize__ (self ),
9575
- right .__finalize__ (other ),
9576
- )
9583
+ return left , right , join_index
9577
9584
9578
9585
@final
9579
9586
def _align_series (
9580
- self : NDFrameT ,
9587
+ self ,
9581
9588
other : Series ,
9582
9589
join : AlignJoin = "outer" ,
9583
9590
axis : Axis | None = None ,
@@ -9587,7 +9594,7 @@ def _align_series(
9587
9594
method = None ,
9588
9595
limit = None ,
9589
9596
fill_axis : Axis = 0 ,
9590
- ) -> tuple [NDFrameT , Series ]:
9597
+ ) -> tuple [Self , Series , Index | None ]:
9591
9598
is_series = isinstance (self , ABCSeries )
9592
9599
if copy and using_copy_on_write ():
9593
9600
copy = False
@@ -9649,14 +9656,7 @@ def _align_series(
9649
9656
left = left .fillna (fill_value , method = method , limit = limit , axis = fill_axis )
9650
9657
right = right .fillna (fill_value , method = method , limit = limit )
9651
9658
9652
- # if DatetimeIndex have different tz, convert to UTC
9653
- if is_series or (not is_series and axis == 0 ):
9654
- left , right = _align_as_utc (left , right , join_index )
9655
-
9656
- return (
9657
- left .__finalize__ (self ),
9658
- right .__finalize__ (other ),
9659
- )
9659
+ return left , right , join_index
9660
9660
9661
9661
@final
9662
9662
def _where (
@@ -12819,23 +12819,3 @@ def _doc_params(cls):
12819
12819
The required number of valid values to perform the operation. If fewer than
12820
12820
``min_count`` non-NA values are present the result will be NA.
12821
12821
"""
12822
-
12823
-
12824
- def _align_as_utc (
12825
- left : NDFrameT , right : NDFrameTb , join_index : Index | None
12826
- ) -> tuple [NDFrameT , NDFrameTb ]:
12827
- """
12828
- If we are aligning timezone-aware DatetimeIndexes and the timezones
12829
- do not match, convert both to UTC.
12830
- """
12831
- if is_datetime64tz_dtype (left .index .dtype ):
12832
- if left .index .tz != right .index .tz :
12833
- if join_index is not None :
12834
- # GH#33671 ensure we don't change the index on
12835
- # our original Series (NB: by default deep=False)
12836
- left = left .copy ()
12837
- right = right .copy ()
12838
- left .index = join_index
12839
- right .index = join_index
12840
-
12841
- return left , right
0 commit comments