MCPcopy
hub / github.com/pandas-dev/pandas / __init__

Method __init__

pandas/core/reshape/reshape.py:121–176  ·  view source on GitHub ↗
(
        self, index: MultiIndex, level: Level, constructor, sort: bool = True
    )

Source from the content-addressed store, hash-verified

119 """
120
121 def __init__(
122 self, index: MultiIndex, level: Level, constructor, sort: bool = True
123 ) -> None:
124 self.constructor = constructor
125 self.sort = sort
126
127 self.index = index.remove_unused_levels()
128
129 self.level = self.index._get_level_number(level)
130
131 # `nan` values have code `-1`, when sorting, we lift to assign them
132 # at index 0
133 self.has_nan = -1 in self.index.codes[self.level]
134 should_lift = self.has_nan and self.sort
135 self.lift = 1 if should_lift else 0
136
137 # Note: the "pop" below alters these in-place.
138 self.new_index_levels = list(self.index.levels)
139 self.new_index_names = list(self.index.names)
140
141 self.removed_name = self.new_index_names.pop(self.level)
142 self.removed_level = self.new_index_levels.pop(self.level)
143 self.removed_level_full = index.levels[self.level]
144 self.unique_nan_index: int = -1
145 if not self.sort:
146 unique_codes: np.ndarray = unique(self.index.codes[self.level])
147 if self.has_nan:
148 # drop nan codes, because they are not represented in level
149 nan_mask = unique_codes == -1
150
151 unique_codes = unique_codes[~nan_mask]
152 self.unique_nan_index = np.flatnonzero(nan_mask)[0]
153
154 self.removed_level = self.removed_level.take(unique_codes)
155 self.removed_level_full = self.removed_level_full.take(unique_codes)
156
157 if get_option("performance_warnings"):
158 # Bug fix GH 20601
159 # If the data frame is too big, the number of unique index combination
160 # will cause int32 overflow on windows environments.
161 # We want to check and raise a warning before this happens
162 num_rows = max(index_level.size for index_level in self.new_index_levels)
163 num_columns = self.removed_level.size
164
165 # GH20601: This forces an overflow if the number of cells is too high.
166 # GH 26314: Previous ValueError raised was too restrictive for many users.
167 num_cells = num_rows * num_columns
168 if num_cells > np.iinfo(np.int32).max:
169 warnings.warn(
170 f"The following operation may generate {num_cells} cells "
171 f"in the resulting pandas object.",
172 PerformanceWarning,
173 stacklevel=find_stack_level(),
174 )
175
176 self._make_selectors()
177
178 @cache_readonly

Callers

nothing calls this directly

Calls 9

_make_selectorsMethod · 0.95
uniqueFunction · 0.90
get_optionFunction · 0.90
find_stack_levelFunction · 0.90
maxFunction · 0.85
remove_unused_levelsMethod · 0.80
_get_level_numberMethod · 0.45
popMethod · 0.45
takeMethod · 0.45

Tested by

no test coverage detected