Skip to content

Commit 301d85f

Browse files
always use nullable Int for 64-bit ints
1 parent ea7d2ba commit 301d85f

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

pandas/core/frame.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9155,30 +9155,23 @@ def combiner(x: Series, y: Series):
91559155
)
91569156
combined = combined.astype(other.dtypes)
91579157
else:
9158-
# GH#60128 Avoid precision loss from int64/uint64 -> float64 round-trip.
9159-
# Promote NumPy int64/uint64 to nullable Int64/UInt64 only when values
9160-
# exceed float64's exact range (|x| >= 2**53). This keeps alignment that
9161-
# introduces <NA> from forcing a lossy cast.
9162-
def _cast_large_numpy_ints_to_nullable(df: DataFrame) -> DataFrame:
9163-
BOUND = 2**53 # first non-exact integer for float64
9158+
# GH#60128 Avoid precision loss from int64/uint64 <-> float64 round-trip.
9159+
def _cast_64_bit_ints_to_nullable(df: DataFrame) -> DataFrame:
91649160
cast_map: dict[str, str] = {}
91659161

91669162
for col, dt in df.dtypes.items():
9167-
ser = df[col]
91689163
if dt == np.dtype("uint64"):
9169-
if ser.size and ser.max() >= BOUND:
9170-
cast_map[col] = "UInt64"
9164+
cast_map[col] = "UInt64"
91719165
elif dt == np.dtype("int64"):
9172-
if ser.size and (ser.max() >= BOUND or ser.min() <= -BOUND):
9173-
cast_map[col] = "Int64"
9166+
cast_map[col] = "Int64"
91749167

91759168
return df.astype(cast_map) if cast_map else df
91769169

91779170
# Only need to cast sides that gain rows on outer align (introduces <NA>).
91789171
if len(other.index.difference(self.index, sort=False)):
9179-
self = _cast_large_numpy_ints_to_nullable(self)
9172+
self = _cast_64_bit_ints_to_nullable(self)
91809173
if len(self.index.difference(other.index, sort=False)):
9181-
other = _cast_large_numpy_ints_to_nullable(other)
9174+
other = _cast_64_bit_ints_to_nullable(other)
91829175

91839176
combined = self.combine(other, combiner, overwrite=False)
91849177

0 commit comments

Comments
 (0)