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

Method convert

pandas/io/pytables.py:2240–2305  ·  view source on GitHub ↗

Convert the data from this selection to the appropriate pandas type.

(
        self, values: np.ndarray, nan_rep, encoding: str, errors: str
    )

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 10

using_string_dtypeFunction · 0.90
StringDtypeClass · 0.90
_maybe_convertFunction · 0.85
from_ordinalsMethod · 0.80
endswithMethod · 0.80
copyMethod · 0.45
_renameMethod · 0.45
getMethod · 0.45
tz_convertMethod · 0.45
tz_localizeMethod · 0.45

Tested by

no test coverage detected