Skip to content
forked from pydata/xarray

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into sparse-fixes
Browse files Browse the repository at this point in the history
* upstream/master:
  Initialize empty or full DataArray (pydata#3159)
  Raise on inplace=True (pydata#3260)
  added support for rasterio geotiff tags (pydata#3249)
  Remove sel_points (pydata#3261)
  Fix sparse ops that were calling bottleneck (pydata#3254)
  New feature of filter_by_attrs added (pydata#3259)
  Update filter_by_attrs to use 'variables' instead of 'data_vars' (pydata#3247)
  • Loading branch information
dcherian committed Aug 26, 2019
2 parents 5722517 + 3c020e5 commit e114cad
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 578 deletions.
8 changes: 0 additions & 8 deletions doc/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,6 @@ These methods may also be applied to ``Dataset`` objects
You may find increased performance by loading your data into memory first,
e.g., with :py:meth:`~xarray.Dataset.load`.

.. note::

Vectorized indexing is a new feature in v0.10.
In older versions of xarray, dimensions of indexers are ignored.
Dedicated methods for some advanced indexing use cases,
``isel_points`` and ``sel_points`` are now deprecated.
See :ref:`more_advanced_indexing` for their alternative.

.. note::

If an indexer is a :py:meth:`~xarray.DataArray`, its coordinates should not
Expand Down
18 changes: 18 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ v0.13.0 (unreleased)
This release increases the minimum required Python version from 3.5.0 to 3.5.3
(:issue:`3089`). By `Guido Imperiale <https://github.com/crusaderky>`_.

Breaking changes
~~~~~~~~~~~~~~~~

- The ``isel_points`` and ``sel_points`` methods are removed, having been deprecated
since v0.10.0. These are redundant with the ``isel`` / ``sel`` methods.
See :ref:`vectorized_indexing` for the details
By `Maximilian Roos <https://github.com/max-sixty>`_
- The ``inplace`` kwarg for public methods now raises an error, having been deprecated
since v0.11.0.
By `Maximilian Roos <https://github.com/max-sixty>`_

New functions/methods
~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -64,6 +75,11 @@ Enhancements
- In :py:meth:`~xarray.Dataset.to_zarr`, passing ``mode`` is not mandatory if
``append_dim`` is set, as it will automatically be set to ``'a'`` internally.
By `David Brochart <https://github.com/davidbrochart>`_.

- Added the ability to initialize an empty or full DataArray
with a single value. (:issue:`277`)
By `Gerardo Rivera <http://github.com/dangomelon>`_.

- :py:func:`~xarray.Dataset.to_netcdf()` now supports the ``invalid_netcdf`` kwarg when used
with ``engine="h5netcdf"``. It is passed to :py:func:`h5netcdf.File`.
By `Ulrich Herter <https://github.com/ulijh>`_.
Expand All @@ -78,6 +94,8 @@ Enhancements
:py:meth:`DataArray.set_index`, as well are more specific error messages
when the user passes invalid arguments (:issue:`3176`).
By `Gregory Gundersen <https://github.com/gwgundersen>`_.

- :py:func:`filter_by_attrs` now filters the coordinates as well as the variables. By `Spencer Jones <https://github.com/cspencerjones>`_.

Bug fixes
~~~~~~~~~
Expand Down
7 changes: 5 additions & 2 deletions xarray/backends/rasterio_.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,14 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, loc
attrs["units"] = riods.units

# Parse extra metadata from tags, if supported
parsers = {"ENVI": _parse_envi}
parsers = {"ENVI": _parse_envi, "GTiff": lambda m: m}

driver = riods.driver
if driver in parsers:
meta = parsers[driver](riods.tags(ns=driver))
if driver == "GTiff":
meta = parsers[driver](riods.tags())
else:
meta = parsers[driver](riods.tags(ns=driver))

for k, v in meta.items():
# Add values as coordinates if they match the band count,
Expand Down
106 changes: 32 additions & 74 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,24 @@ def _infer_coords_and_dims(
return new_coords, dims


def _check_data_shape(data, coords, dims):
if data is dtypes.NA:
data = np.nan
if coords is not None and utils.is_scalar(data, include_0d=False):
if utils.is_dict_like(coords):
if dims is None:
return data
else:
data_shape = tuple(
as_variable(coords[k], k).size if k in coords.keys() else 1
for k in dims
)
else:
data_shape = tuple(as_variable(coord, "foo").size for coord in coords)
data = np.full(data_shape, data)
return data


class _LocIndexer:
def __init__(self, data_array: "DataArray"):
self.data_array = data_array
Expand Down Expand Up @@ -234,7 +252,7 @@ class DataArray(AbstractArray, DataWithCoords):

def __init__(
self,
data: Any,
data: Any = dtypes.NA,
coords: Union[Sequence[Tuple], Mapping[Hashable, Any], None] = None,
dims: Union[Hashable, Sequence[Hashable], None] = None,
name: Hashable = None,
Expand Down Expand Up @@ -323,6 +341,7 @@ def __init__(
if encoding is None:
encoding = getattr(data, "encoding", None)

data = _check_data_shape(data, coords, dims)
data = as_compatible_data(data)
coords, dims = _infer_coords_and_dims(data.shape, coords, dims)
variable = Variable(dims, data, attrs, encoding, fastpath=True)
Expand Down Expand Up @@ -700,30 +719,17 @@ def reset_coords(
drop : bool, optional
If True, remove coordinates instead of converting them into
variables.
inplace : bool, optional
If True, modify this object in place. Otherwise, create a new
object.
Returns
-------
Dataset, or DataArray if ``drop == True``, or None if
``inplace == True``
Dataset, or DataArray if ``drop == True``
"""
inplace = _check_inplace(inplace)
if inplace and not drop:
raise ValueError(
"cannot reset coordinates in-place on a "
"DataArray without ``drop == True``"
)
_check_inplace(inplace)
if names is None:
names = set(self.coords) - set(self.dims)
dataset = self.coords.to_dataset().reset_coords(names, drop)
if drop:
if inplace:
self._coords = dataset._variables
return None
else:
return self._replace(coords=dataset._variables)
return self._replace(coords=dataset._variables)
else:
if self.name is None:
raise ValueError(
Expand Down Expand Up @@ -1026,32 +1032,6 @@ def sel(
)
return self._from_temp_dataset(ds)

def isel_points(self, dim="points", **indexers) -> "DataArray":
"""Return a new DataArray whose data is given by pointwise integer
indexing along the specified dimension(s).
See Also
--------
Dataset.isel_points
"""
ds = self._to_temp_dataset().isel_points(dim=dim, **indexers)
return self._from_temp_dataset(ds)

def sel_points(
self, dim="points", method=None, tolerance=None, **indexers
) -> "DataArray":
"""Return a new DataArray whose dataset is given by pointwise selection
of index labels along the specified dimension(s).
See Also
--------
Dataset.sel_points
"""
ds = self._to_temp_dataset().sel_points(
dim=dim, method=method, tolerance=tolerance, **indexers
)
return self._from_temp_dataset(ds)

def broadcast_like(
self, other: Union["DataArray", Dataset], exclude: Iterable[Hashable] = None
) -> "DataArray":
Expand Down Expand Up @@ -1511,9 +1491,6 @@ def set_index(
append : bool, optional
If True, append the supplied index(es) to the existing index(es).
Otherwise replace the existing index(es) (default).
inplace : bool, optional
If True, set new index(es) in-place. Otherwise, return a new
DataArray object.
**indexes_kwargs: optional
The keyword arguments form of ``indexes``.
One of indexes or indexes_kwargs must be provided.
Expand All @@ -1522,7 +1499,6 @@ def set_index(
-------
obj : DataArray
Another DataArray, with this data but replaced coordinates.
Return None if inplace=True.
Example
-------
Expand Down Expand Up @@ -1552,14 +1528,10 @@ def set_index(
--------
DataArray.reset_index
"""
inplace = _check_inplace(inplace)
_check_inplace(inplace)
indexes = either_dict_or_kwargs(indexes, indexes_kwargs, "set_index")
coords, _ = merge_indexes(indexes, self._coords, set(), append=append)
if inplace:
self._coords = coords
return None
else:
return self._replace(coords=coords)
return self._replace(coords=coords)

def reset_index(
self,
Expand All @@ -1577,36 +1549,29 @@ def reset_index(
drop : bool, optional
If True, remove the specified indexes and/or multi-index levels
instead of extracting them as new coordinates (default: False).
inplace : bool, optional
If True, modify the dataarray in-place. Otherwise, return a new
DataArray object.
Returns
-------
obj : DataArray
Another dataarray, with this dataarray's data but replaced
coordinates. If ``inplace == True``, return None.
coordinates.
See Also
--------
DataArray.set_index
"""
inplace = _check_inplace(inplace)
_check_inplace(inplace)
coords, _ = split_indexes(
dims_or_levels, self._coords, set(), self._level_coords, drop=drop
)
if inplace:
self._coords = coords
return None
else:
return self._replace(coords=coords)
return self._replace(coords=coords)

def reorder_levels(
self,
dim_order: Mapping[Hashable, Sequence[int]] = None,
inplace: bool = None,
**dim_order_kwargs: Sequence[int]
) -> Optional["DataArray"]:
) -> "DataArray":
"""Rearrange index levels using input order.
Parameters
Expand All @@ -1615,9 +1580,6 @@ def reorder_levels(
Mapping from names matching dimensions and values given
by lists representing new level orders. Every given dimension
must have a multi-index.
inplace : bool, optional
If True, modify the dataarray in-place. Otherwise, return a new
DataArray object.
**dim_order_kwargs: optional
The keyword arguments form of ``dim_order``.
One of dim_order or dim_order_kwargs must be provided.
Expand All @@ -1626,9 +1588,9 @@ def reorder_levels(
-------
obj : DataArray
Another dataarray, with this dataarray's data but replaced
coordinates. If ``inplace == True``, return None.
coordinates.
"""
inplace = _check_inplace(inplace)
_check_inplace(inplace)
dim_order = either_dict_or_kwargs(dim_order, dim_order_kwargs, "reorder_levels")
replace_coords = {}
for dim, order in dim_order.items():
Expand All @@ -1639,11 +1601,7 @@ def reorder_levels(
replace_coords[dim] = IndexVariable(coord.dims, index.reorder_levels(order))
coords = self._coords.copy()
coords.update(replace_coords)
if inplace:
self._coords = coords
return None
else:
return self._replace(coords=coords)
return self._replace(coords=coords)

def stack(
self,
Expand Down
Loading

0 comments on commit e114cad

Please sign in to comment.