(
self,
result,
name=None,
expand: bool | None = None,
fill_value=np.nan,
returns_string: bool = True,
dtype=None,
)
| 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) |
no test coverage detected