Skip to content

Commit

Permalink
Move offsets downcasting to MultiIndex._engine
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaFicarelli committed Apr 25, 2024
1 parent 3ab8601 commit c512c51
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 1 addition & 2 deletions pandas/_libs/index.pyx
Expand Up @@ -712,8 +712,7 @@ cdef class BaseMultiIndexCodesEngine:
Pre-calculated offsets, one for each level of the index.
"""
self.levels = levels
# Downcast the type if possible, to prevent upcasting when shifting codes:
self.offsets = offsets.astype(np.min_scalar_type(offsets[0]), copy=False)
self.offsets = offsets

# Transform labels in a single array, and add 2 so that we are working
# with positive integers (-1 for NaN becomes 1). This enables us to
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/multi.py
Expand Up @@ -1201,7 +1201,9 @@ def _engine(self):
# equivalent to sorting lexicographically the codes themselves. Notice
# that each level needs to be shifted by the number of bits needed to
# represent the _previous_ ones:
offsets = np.concatenate([lev_bits[1:], [0]]).astype("uint64")
offsets = np.concatenate([lev_bits[1:], [0]])
# Downcast the type if possible, to prevent upcasting when shifting codes:
offsets = offsets.astype(np.min_scalar_type(int(offsets[0])))

# Check the total number of bits needed for our representation:
if lev_bits[0] > 64:
Expand Down

0 comments on commit c512c51

Please sign in to comment.