MCPcopy
hub / github.com/pydata/xarray / PandasExtensionArray

Class PandasExtensionArray

xarray/core/extension_array.py:238–327  ·  view source on GitHub ↗

NEP-18 compliant wrapper for pandas extension arrays. Parameters ---------- array : T_ExtensionArray The array to be wrapped upon e.g,. :py:class:`xarray.Variable` creation. ```

Source from the content-addressed store, hash-verified

236
237@dataclass(frozen=True)
238class PandasExtensionArray(NDArrayMixin, Generic[T_ExtensionArray]):
239 """NEP-18 compliant wrapper for pandas extension arrays.
240
241 Parameters
242 ----------
243 array : T_ExtensionArray
244 The array to be wrapped upon e.g,. :py:class:`xarray.Variable` creation.
245 ```
246 """
247
248 array: T_ExtensionArray
249
250 def __post_init__(self):
251 if not isinstance(self.array, pd.api.extensions.ExtensionArray):
252 raise TypeError(f"{self.array} is not a pandas ExtensionArray.")
253 # This does not use the UNSUPPORTED_EXTENSION_ARRAY_TYPES whitelist because
254 # we do support extension arrays from datetime, for example, that need
255 # duck array support internally via this class. These can appear from `DatetimeIndex`
256 # wrapped by `PandasIndex` internally, for example.
257 if not is_allowed_extension_array(self.array):
258 raise TypeError(
259 f"{self.array.dtype!r} should be converted to a numpy array in `xarray` internally."
260 )
261
262 def __array_function__(self, func, types, args, kwargs):
263 if func not in HANDLED_EXTENSION_ARRAY_FUNCTIONS:
264 raise KeyError("Function not registered for pandas extension arrays.")
265 args = replace_duck_with_extension_array(args)
266 res = HANDLED_EXTENSION_ARRAY_FUNCTIONS[func](*args, **kwargs)
267 if isinstance(res, ExtensionArray):
268 return PandasExtensionArray(res)
269 return res
270
271 def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
272 return ufunc(*inputs, **kwargs)
273
274 def __getitem__(self, key) -> PandasExtensionArray[T_ExtensionArray]:
275 if (
276 isinstance(key, tuple) and len(key) == 1
277 ): # pyarrow type arrays can't handle single-length tuples
278 (key,) = key
279 item = self.array[key]
280 if is_allowed_extension_array(item):
281 return PandasExtensionArray(item)
282 if is_scalar(item) or isinstance(key, int):
283 return PandasExtensionArray(type(self.array)._from_sequence([item])) # type: ignore[call-arg,attr-defined,unused-ignore]
284 return PandasExtensionArray(item)
285
286 def __setitem__(self, key, val):
287 self.array[key] = val
288
289 def __len__(self):
290 return len(self.array)
291
292 def __eq__(self, other):
293 if isinstance(other, PandasExtensionArray):
294 return self.array == other.array
295 return self.array == other

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…