Skip to content

Commit 49ff1a5

Browse files
follow contributing guidelines
1 parent 444deaa commit 49ff1a5

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

pandas/core/frame.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9029,31 +9029,31 @@ def combine(
90299029
# GH#60128 Integers n where |n| > 2**53 would lose precision after align
90309030
# upcasts them to float. Avoid lossy conversion by preemptively promoting
90319031
# int64 and uint64 to their nullable ExtensionDtypes, Int64 and UInt64.
9032-
def _promote_wide_ints(df: DataFrame) -> DataFrame:
9033-
"""Promotes int64/uint64 columns to their nullable ExtensionDtypes."""
9032+
def _ensure_nullable_int64_dtypes(df: DataFrame) -> DataFrame:
9033+
"""Promote int64/uint64 DataFrame columns to Int64/UInt64."""
90349034
cast_map: dict[str, str] = {}
90359035
for col, dt in df.dtypes.items():
9036-
if dt == np.dtype("int64"):
9036+
if dt == np.int64:
90379037
cast_map[col] = "Int64"
9038-
elif dt == np.dtype("uint64"):
9038+
elif dt == np.uint64:
90399039
cast_map[col] = "UInt64"
90409040

90419041
if cast_map:
90429042
df = df.astype(cast_map)
90439043
return df
90449044

9045-
# To maintain backwards compatibility, this function can restore
9046-
# int64/uint64 columns from float64 when possible. But we should
9047-
# really consider just embracing nullable ExtensionDtypes instead.
9048-
def _restore_wide_ints(
9049-
self_original: DataFrame, other_original: DataFrame, combined_df: DataFrame
9045+
# To maintain backwards compatibility, downcast the pre-promoted int64
9046+
# columns of the combined DataFrame back to how they would have resolved.
9047+
# Consider just embracing nullable ExtensionDtypes instead, though.
9048+
def _revert_int64_dtype_promotion(
9049+
self_orig: DataFrame, other_orig: DataFrame, combined_df: DataFrame
90509050
) -> DataFrame:
9051-
"""Restores original dtypes by re-casting the promoted int columns."""
9051+
"""Resolve the combined dtypes according to the original dtypes."""
90529052
cast_map: dict[str, str] = {}
90539053
for col in combined_df.columns:
90549054
ser = combined_df[col]
9055-
orig_dt_self = self_original.dtypes.get(col)
9056-
orig_dt_other = other_original.dtypes.get(col)
9055+
orig_dt_self = self_orig.dtypes.get(col)
9056+
orig_dt_other = other_orig.dtypes.get(col)
90579057

90589058
was_promoted = (orig_dt_self in [np.int64, np.uint64]) or (
90599059
orig_dt_other in [np.int64, np.uint64]
@@ -9080,11 +9080,11 @@ def _restore_wide_ints(
90809080
combined_df = combined_df.astype(cast_map)
90819081
return combined_df
90829082

9083-
# store originals and promote wide ints before align
9084-
self_original = self
9085-
other_original = other
9086-
self = _promote_wide_ints(self)
9087-
other = _promote_wide_ints(other)
9083+
# store originals and prepare for align
9084+
self_orig = self
9085+
other_orig = other
9086+
self = _ensure_nullable_int64_dtypes(self)
9087+
other = _ensure_nullable_int64_dtypes(other)
90889088

90899089
other_idxlen = len(other.index) # save for compare
90909090
other_columns = other.columns
@@ -9153,7 +9153,9 @@ def _restore_wide_ints(
91539153

91549154
# convert_objects just in case
91559155
frame_result = self._constructor(result, index=new_index, columns=new_columns)
9156-
frame_result = _restore_wide_ints(self_original, other_original, frame_result)
9156+
frame_result = _revert_int64_dtype_promotion(
9157+
self_orig, other_orig, frame_result
9158+
)
91579159
return frame_result.__finalize__(self, method="combine")
91589160

91599161
def combine_first(self, other: DataFrame) -> DataFrame:

0 commit comments

Comments
 (0)