agrifoodpy.array_accessor

Classes

XarrayAccessorBase

Common logic for for both data structures (DataArray and Dataset).

Module Contents

class agrifoodpy.array_accessor.XarrayAccessorBase(xarray_obj)

Bases: object

Common logic for for both data structures (DataArray and Dataset).

http://xarray.pydata.org/en/stable/internals.html#extending-xarray

add_items(items, copy_from=None)

Extends the item list of an input xarray object according to the defined input item list

Parameters:
  • items (list, int, string) – list of item names to be added to the data

  • copy_from (list, int, string, optional) – If provided, this is the list of items already on the array to copy data from.

Returns:

out – Xarray object with new items added.

Return type:

xarray.Dataset, xarray.DataArray

add_regions(regions, copy_from=None)

Extends the region list of an input xarray object according to the defined input region list

Parameters:
  • regions (list, int, string) – list of region names to be added to the data

  • copy_from (list, int, string) – If provided, this is the list of regions already on the object to copy data from.

  • labels (dict) – Dictionary containing the new label for the regions matched to its corresponding label coordinate.

Returns:

out – Xarray object with new regions added.

Return type:

xarray.Dataset, xarray.DataArray

add_years(years, projection='empty')

Extends the year range of an xarray object according to the defined maximum year

Parameters:
  • years (list, int) – list of years to be added to the data

  • projection (string or array_like) – Projection mode. If “constant”, the last year of the input array is copied to every new year. If “empty”, values are initialized and set to zero. If a float array is given, these are used to populate the new year using a scaling of the last year of the array

Returns:

out – Xarray object with new years added.

Return type:

xarray.Dataset, xarray.DataArray

group_sum(coordinate, new_name=None)

Sums quantities over items of a equal group labels and, optionally, renames the groups label coordinate.

Parameters:
  • fbs (xarray.Dataset) – Input xarray object

  • coordinate (string) – Coordinate name to group elements and sum over

  • new_name (string, optional) – New name for the collapsed coordinate

Returns:

fbs – Xarray object with new coordinate base.

Return type:

xarray.Dataset, xarray.DataArray