Convert the data from this selection to the appropriate pandas type.
(
self, values: np.ndarray, nan_rep, encoding: str, errors: str
)
| 2238 | return getattr(self.table.cols, self.cname).is_indexed |
| 2239 | |
| 2240 | def convert( |
| 2241 | self, values: np.ndarray, nan_rep, encoding: str, errors: str |
| 2242 | ) -> tuple[np.ndarray, np.ndarray] | tuple[Index, Index]: |
| 2243 | """ |
| 2244 | Convert the data from this selection to the appropriate pandas type. |
| 2245 | """ |
| 2246 | assert isinstance(values, np.ndarray), type(values) |
| 2247 | |
| 2248 | # values is a recarray |
| 2249 | if values.dtype.fields is not None: |
| 2250 | # Copy, otherwise values will be a view |
| 2251 | # preventing the original recarry from being free'ed |
| 2252 | values = values[self.cname].copy() |
| 2253 | |
| 2254 | val_kind = self.kind |
| 2255 | values = _maybe_convert(values, val_kind, encoding, errors) |
| 2256 | kwargs = {} |
| 2257 | kwargs["name"] = self.index_name |
| 2258 | |
| 2259 | if self.freq is not None: |
| 2260 | kwargs["freq"] = self.freq |
| 2261 | |
| 2262 | factory: type[Index | DatetimeIndex] = Index |
| 2263 | if lib.is_np_dtype(values.dtype, "M") or isinstance( |
| 2264 | values.dtype, DatetimeTZDtype |
| 2265 | ): |
| 2266 | factory = DatetimeIndex |
| 2267 | elif values.dtype == "i8" and "freq" in kwargs: |
| 2268 | # PeriodIndex data is stored as i8 |
| 2269 | # error: Incompatible types in assignment (expression has type |
| 2270 | # "Callable[[Any, KwArg(Any)], PeriodIndex]", variable has type |
| 2271 | # "Union[Type[Index], Type[DatetimeIndex]]") |
| 2272 | factory = lambda x, **kwds: PeriodIndex.from_ordinals( # type: ignore[assignment] |
| 2273 | x, freq=kwds.get("freq", None) |
| 2274 | )._rename(kwds["name"]) |
| 2275 | |
| 2276 | # making an Index instance could throw a number of different errors |
| 2277 | try: |
| 2278 | new_pd_index = factory(values, **kwargs) |
| 2279 | except UnicodeEncodeError as err: |
| 2280 | if ( |
| 2281 | errors == "surrogatepass" |
| 2282 | and using_string_dtype() |
| 2283 | and str(err).endswith("surrogates not allowed") |
| 2284 | and HAS_PYARROW |
| 2285 | ): |
| 2286 | new_pd_index = factory( |
| 2287 | values, |
| 2288 | dtype=StringDtype(storage="python", na_value=np.nan), |
| 2289 | **kwargs, |
| 2290 | ) |
| 2291 | else: |
| 2292 | raise |
| 2293 | except ValueError: |
| 2294 | # if the output freq is different that what we recorded, |
| 2295 | # it should be None (see also 'doc example part 2') |
| 2296 | if "freq" in kwargs: |
| 2297 | kwargs["freq"] = None |
nothing calls this directly
no test coverage detected