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

Method _wrap_result

pandas/core/strings/accessor.py:260–402  ·  view source on GitHub ↗
(
        self,
        result,
        name=None,
        expand: bool | None = None,
        fill_value=np.nan,
        returns_string: bool = True,
        dtype=None,
    )

Source from the content-addressed store, hash-verified

258 raise TypeError(f"'{type(self).__name__}' object is not iterable")
259
260 def _wrap_result(
261 self,
262 result,
263 name=None,
264 expand: bool | None = None,
265 fill_value=np.nan,
266 returns_string: bool = True,
267 dtype=None,
268 ):
269 from pandas import (
270 Index,
271 MultiIndex,
272 )
273
274 if not hasattr(result, "ndim") or not hasattr(result, "dtype"):
275 if isinstance(result, ABCDataFrame):
276 result = result.__finalize__(self._orig, name="str")
277 return result
278 assert result.ndim < 3
279
280 # We can be wrapping a string / object / categorical result, in which
281 # case we'll want to return the same dtype as the input.
282 # Or we can be wrapping a numeric output, in which case we don't want
283 # to return a StringArray.
284 # Ideally the array method returns the right array type.
285 if expand is None:
286 # infer from ndim if expand is not specified
287 expand = result.ndim != 1
288 elif expand is True and not isinstance(self._orig, ABCIndex):
289 # required when expand=True is explicitly specified
290 # not needed when inferred
291 if isinstance(result.dtype, ArrowDtype):
292 import pyarrow as pa
293
294 from pandas.core.arrays.arrow.array import ArrowExtensionArray
295
296 value_lengths = pa.compute.list_value_length(result._pa_array)
297 max_len = pa.compute.max(value_lengths).as_py()
298 min_len = pa.compute.min(value_lengths).as_py()
299 if result._hasna:
300 # ArrowExtensionArray.fillna doesn't work for list scalars
301 result = ArrowExtensionArray(
302 result._pa_array.fill_null([None] * max_len)
303 )
304 if min_len < max_len:
305 # append nulls to each scalar list element up to max_len
306 result = ArrowExtensionArray(
307 pa.compute.list_slice(
308 result._pa_array,
309 start=0,
310 stop=max_len,
311 return_fixed_size_list=True,
312 )
313 )
314 if name is None:
315 name = range(max_len)
316 result = (
317 pa.compute.list_flatten(result._pa_array)

Callers 15

__getitem__Method · 0.95
splitMethod · 0.95
rsplitMethod · 0.95
partitionMethod · 0.95
rpartitionMethod · 0.95
getMethod · 0.95
joinMethod · 0.95
containsMethod · 0.95
matchMethod · 0.95
fullmatchMethod · 0.95
replaceMethod · 0.95
repeatMethod · 0.95

Calls 13

ArrowExtensionArrayClass · 0.90
is_object_dtypeFunction · 0.90
is_bool_dtypeFunction · 0.90
IndexClass · 0.90
maxFunction · 0.85
__finalize__Method · 0.80
get_level_valuesMethod · 0.80
maxMethod · 0.45
minMethod · 0.45
reshapeMethod · 0.45
to_numpyMethod · 0.45
arrayMethod · 0.45

Tested by

no test coverage detected