Skip to content

Commit

Permalink
Fix typos, undo empty spaces, remove temporarily introduced arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ockenfuss committed Aug 23, 2024
1 parent 1ac5e9c commit 97b00a4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 82 deletions.
91 changes: 40 additions & 51 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@
from xarray.backends import ZarrStore
from xarray.backends.api import T_NetcdfEngine, T_NetcdfTypes
from xarray.core.groupby import DataArrayGroupBy
from xarray.core.missing import GapMask
from xarray.core.resample import DataArrayResample
from xarray.core.rolling import DataArrayCoarsen, DataArrayRolling
from xarray.core.missing import GapMask
from xarray.core.types import (
CoarsenBoundaryOptions,
DatetimeLike,
Expand Down Expand Up @@ -3477,21 +3477,12 @@ def fillna(self, value: Any) -> Self:
out = ops.fillna(self, value)
return out


def interpolate_na(
self,
dim: Hashable,
method: InterpOptions = "linear",
limit: int | None = None,
use_coordinate: bool | Hashable = True,
limit: (
None
| int
| float
| str
| pd.Timedelta
| np.timedelta64
| datetime.timedelta
) = None,
max_gap: (
None
| int
Expand All @@ -3515,25 +3506,25 @@ def interpolate_na(
String indicating which method to use for interpolation:
- 'linear': linear interpolation. Additional keyword
arguments are passed to :py:func:`numpy.interp`
arguments are passed to :py:func:`numpy.interp`
- 'nearest', 'zero', 'slinear', 'quadratic', 'cubic', 'polynomial':
are passed to :py:func:`scipy.interpolate.interp1d`. If
``method='polynomial'``, the ``order`` keyword argument must also be
provided.
are passed to :py:func:`scipy.interpolate.interp1d`. If
``method='polynomial'``, the ``order`` keyword argument must also be
provided.
- 'barycentric', 'krogh', 'pchip', 'spline', 'akima': use their
respective :py:class:`scipy.interpolate` classes.
respective :py:class:`scipy.interpolate` classes.
limit : int or None, default: None
Maximum number of consecutive NaNs to fill. Must be greater than 0
or None for no limit. This filling is done regardless of the size of
the gap in the data. To only interpolate over gaps less than a given length,
see ``max_gap``.
use_coordinate : bool or str, default: True
Specifies which index to use as the x values in the interpolation
formulated as `y = f(x)`. If False, values are treated as if
equally-spaced along ``dim``. If True, the IndexVariable `dim` is
used. If ``use_coordinate`` is a string, it specifies the name of a
coordinate variable to use as the index.
limit : int or None, default: None
Maximum number of consecutive NaNs to fill. Must be greater than 0
or None for no limit. This filling is done regardless of the size of
the gap in the data. To only interpolate over gaps less than a given length,
see ``max_gap``.
max_gap : int, float, str, pandas.Timedelta, numpy.timedelta64, datetime.timedelta, default: None
Maximum size of gap, a continuous sequence of NaNs, that will be filled.
Use None for no limit. When interpolating along a datetime64 dimension
Expand All @@ -3554,7 +3545,7 @@ def interpolate_na(
<xarray.DataArray (x: 9)>
array([nan, nan, nan, 1., nan, nan, 4., nan, nan])
Coordinates:
* x (x) int64 0 1 2 3 4 5 6 7 8
* x (x) int64 0 1 2 3 4 5 6 7 8
The gap lengths are 3-0 = 3; 6-3 = 3; and 8-6 = 2 respectively
keep_attrs : bool or None, default: None
Expand Down Expand Up @@ -3583,33 +3574,32 @@ def interpolate_na(
<xarray.DataArray (x: 5)> Size: 40B
array([nan, 2., 3., nan, 0.])
Coordinates:
* x (x) int64 40B 0 1 2 3 4
* x (x) int64 40B 0 1 2 3 4
>>> da.interpolate_na(dim="x", method="linear")
<xarray.DataArray (x: 5)> Size: 40B
array([nan, 2. , 3. , 1.5, 0. ])
Coordinates:
* x (x) int64 40B 0 1 2 3 4
* x (x) int64 40B 0 1 2 3 4
>>> da.interpolate_na(dim="x", method="linear", fill_value="extrapolate")
<xarray.DataArray (x: 5)> Size: 40B
array([1. , 2. , 3. , 1.5, 0. ])
Coordinates:
* x (x) int64 40B 0 1 2 3 4
* x (x) int64 40B 0 1 2 3 4
"""
from xarray.core.missing import interp_na

return interp_na(
self,
dim=dim,
method=method,
limit=limit,
use_coordinate=use_coordinate,
max_gap=max_gap,
keep_attrs=keep_attrs,
**kwargs,
)

self,
dim=dim,
method=method,
limit=limit,
use_coordinate=use_coordinate,
max_gap=max_gap,
keep_attrs=keep_attrs,
**kwargs,
)

def ffill(self, dim: Hashable, limit: int | None = None) -> Self:
"""Fill NaN values by propagating values forward
Expand Down Expand Up @@ -3778,7 +3768,7 @@ def bfill(self, dim: Hashable, limit: int | None = None) -> Self:
from xarray.core.missing import bfill

return bfill(self, dim, limit=limit)

def fill_gaps(
self,
dim: Hashable,
Expand Down Expand Up @@ -3873,10 +3863,6 @@ def fill_gaps(
* x (x) int64 0 1 2 3 4 5 6 7 8
The gap lengths are 3-0 = 3; 6-3 = 3; and 8-6 = 2 respectively
keep_attrs : bool or None, default: None
If True, the dataarray's attributes (`attrs`) will be copied from
the original object to the new one. If False, the new
object will be returned without attributes.
Returns
-------
Expand Down Expand Up @@ -3907,32 +3893,35 @@ def fill_gaps(
array([nan, 2., nan, nan, 5., nan, 0.])
Coordinates:
* x (x) int64 56B 0 1 2 3 4 5 6
>>> da.fill_gaps(
... dim="x", limit=1, limit_direction="forward"
... ).interpolate_na(dim="x")
>>> da.fill_gaps(dim="x", limit=1, limit_direction="forward").interpolate_na(
... dim="x"
... )
<xarray.DataArray (x: 7)> Size: 56B
array([nan, 2. , 3. , nan, 5. , 2.5, 0. ])
Coordinates:
* x (x) int64 56B 0 1 2 3 4 5 6
>>> da.fill_gaps(
... dim="x", max_gap=2, limit_direction="forward"
... ).ffill(dim="x")
>>> da.fill_gaps(dim="x", max_gap=2, limit_direction="forward").ffill(dim="x")
<xarray.DataArray (x: 7)> Size: 56B
array([nan, 2., nan, nan, 5., 5., 0.])
Coordinates:
* x (x) int64 56B 0 1 2 3 4 5 6
>>> da.fill_gaps(
... dim="x", limit_area="inside"
... ).fillna(9)
>>> da.fill_gaps(dim="x", limit_area="inside").fillna(9)
<xarray.DataArray (x: 7)> Size: 56B
array([nan, 2., 9., 9., 5., 9., 0.])
Coordinates:
* x (x) int64 56B 0 1 2 3 4 5 6
"""
from xarray.core.missing import mask_gaps

return mask_gaps(self, dim, use_coordinate=use_coordinate, limit=limit, limit_direction=limit_direction, limit_area=limit_area, max_gap=max_gap)

return mask_gaps(
self,
dim,
use_coordinate=use_coordinate,
limit=limit,
limit_direction=limit_direction,
limit_area=limit_area,
max_gap=max_gap,
)

def combine_first(self, other: Self) -> Self:
"""Combine two DataArray objects, with union of coordinates.
Expand Down
60 changes: 30 additions & 30 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
from xarray.core.dataarray import DataArray
from xarray.core.groupby import DatasetGroupBy
from xarray.core.merge import CoercibleMapping, CoercibleValue, _MergeResult
from xarray.core.missing import GapMask
from xarray.core.resample import DatasetResample
from xarray.core.rolling import DatasetCoarsen, DatasetRolling
from xarray.core.types import (
Expand Down Expand Up @@ -6594,19 +6595,8 @@ def interpolate_na(
self,
dim: Hashable,
method: InterpOptions = "linear",
limit: int | None = None,
use_coordinate: bool | Hashable = True,
limit: (
None
| int
| float
| str
| pd.Timedelta
| np.timedelta64
| datetime.timedelta
) = None,
limit_direction: LimitDirectionOptions = "forward",
limit_area: LimitAreaOptions | None = None,
limit_use_coordinate: bool | Hashable = False,
max_gap: (
int
| float
Expand Down Expand Up @@ -6673,6 +6663,10 @@ def interpolate_na(
* x (x) int64 0 1 2 3 4 5 6 7 8
The gap lengths are 3-0 = 3; 6-3 = 3; and 8-6 = 2 respectively
keep_attrs : bool or None, default: None
If True, the dataarray's attributes (`attrs`) will be copied from
the original object to the new one. If False, the new
object will be returned without attributes.
**kwargs : dict, optional
parameters passed verbatim to the underlying interpolation function
Expand Down Expand Up @@ -6737,6 +6731,9 @@ def interpolate_na(
"""
from xarray.core.missing import _apply_over_vars_with_dim, interp_na

if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)

new = _apply_over_vars_with_dim(
interp_na,
self,
Expand All @@ -6745,8 +6742,10 @@ def interpolate_na(
limit=limit,
use_coordinate=use_coordinate,
max_gap=max_gap,
keep_attrs=keep_attrs,
**kwargs,
)
new.attrs = self.attrs if keep_attrs else None
return new

def ffill(self, dim: Hashable, limit: int | None = None) -> Self:
Expand Down Expand Up @@ -6972,10 +6971,6 @@ def fill_gaps(
* x (x) int64 0 1 2 3 4 5 6 7 8
The gap lengths are 3-0 = 3; 6-3 = 3; and 8-6 = 2 respectively
keep_attrs : bool or None, default: None
If True, the dataarray's attributes (`attrs`) will be copied from
the original object to the new one. If False, the new
object will be returned without attributes.
Returns
-------
Expand All @@ -6984,10 +6979,10 @@ def fill_gaps(
See Also
--------
DataArray.fillna
DataArray.ffill
DataArray.bfill
DataArray.interpolate_na
Dataset.fillna
Dataset.ffill
Dataset.bfill
Dataset.interpolate_na
pandas.DataFrame.interpolate
Notes
Expand All @@ -7011,29 +7006,25 @@ def fill_gaps(
Data variables:
A (x) float64 56B nan 2.0 nan nan 5.0 nan 0.0
B (x) float64 56B nan 2.0 nan nan 5.0 6.0 nan
>>> ds.fill_gaps(
... dim="x", limit=1, limit_direction="forward"
... ).interpolate_na(dim="x")
>>> ds.fill_gaps(dim="x", limit=1, limit_direction="forward").interpolate_na(
... dim="x"
... )
<xarray.Dataset> Size: 168B
Dimensions: (x: 7)
Coordinates:
* x (x) int64 56B 0 1 2 3 4 5 6
Data variables:
A (x) float64 56B nan 2.0 3.0 nan 5.0 2.5 0.0
B (x) float64 56B nan 2.0 3.0 nan 5.0 6.0 nan
>>> ds.fill_gaps(
... dim="x", max_gap=2, limit_direction="forward"
... ).ffill(dim="x")
>>> ds.fill_gaps(dim="x", max_gap=2, limit_direction="forward").ffill(dim="x")
<xarray.Dataset> Size: 168B
Dimensions: (x: 7)
Coordinates:
* x (x) int64 56B 0 1 2 3 4 5 6
Data variables:
A (x) float64 56B nan 2.0 nan nan 5.0 5.0 0.0
B (x) float64 56B nan 2.0 nan nan 5.0 6.0 6.0
>>> ds.fill_gaps(
... dim="x", limit_area="inside"
... ).fillna(9)
>>> ds.fill_gaps(dim="x", limit_area="inside").fillna(9)
<xarray.Dataset> Size: 168B
Dimensions: (x: 7)
Coordinates:
Expand All @@ -7044,7 +7035,16 @@ def fill_gaps(
"""
from xarray.core.missing import mask_gaps

return mask_gaps(self, dim, use_coordinate=use_coordinate, limit=limit, limit_direction=limit_direction, limit_area=limit_area, max_gap=max_gap)
return mask_gaps(
self,
dim,
use_coordinate=use_coordinate,
limit=limit,
limit_direction=limit_direction,
limit_area=limit_area,
max_gap=max_gap,
)

def combine_first(self, other: Self) -> Self:
"""Combine two Datasets, default to data_vars of self.
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def get_clean_interp_index(
from xarray.coding.cftimeindex import CFTimeIndex

index = _get_raw_interp_index(arr, dim, use_coordinate)
# index.name is None for multiindexes
# TODO: index.name is None for multiindexes
# set name for nice error messages below
if isinstance(index, pd.MultiIndex):
index.name = dim
Expand Down

0 comments on commit 97b00a4

Please sign in to comment.