Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from pydata:main #502

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion asv_bench/benchmarks/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setup(self, dtype, subset):
[
list("abcdefhijk"),
list("abcdefhijk"),
pd.date_range(start="2000-01-01", periods=1000, freq="B"),
pd.date_range(start="2000-01-01", periods=1000, freq="D"),
]
)
series = pd.Series(data, index)
Expand Down
9 changes: 8 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ New Features
different collections of coordinates prior to assign them to a Dataset or
DataArray (:pull:`8102`) at once.
By `Benoît Bovy <https://github.com/benbovy>`_.
- Provide `preferred_chunks` for data read from netcdf files (:issue:`1440`, :pull:`7948`)
- Provide `preferred_chunks` for data read from netcdf files (:issue:`1440`, :pull:`7948`).
By `Martin Raspaud <https://github.com/mraspaud>`_.
- Improved static typing of reduction methods (:pull:`6746`).
By `Richard Kleijn <https://github.com/rhkleijn>`_.

Breaking changes
~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -63,6 +66,10 @@ Bug fixes
- Fix bug where :py:class:`DataArray` instances on the right-hand side
of :py:meth:`DataArray.__setitem__` lose dimension names.
(:issue:`7030`, :pull:`8067`) By `Darsh Ranjan <https://github.com/dranjan>`_.
- Return ``float64`` in presence of ``NaT`` in :py:class:`~core.accessor_dt.DatetimeAccessor` and
special case ``NaT`` handling in :py:meth:`~core.accessor_dt.DatetimeAccessor.isocalendar()`
(:issue:`7928`, :pull:`8084`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
58 changes: 29 additions & 29 deletions xarray/core/_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from xarray.core import duck_array_ops
from xarray.core.options import OPTIONS
from xarray.core.types import Dims
from xarray.core.types import Dims, Self
from xarray.core.utils import contains_only_chunked_or_numpy, module_available

if TYPE_CHECKING:
Expand All @@ -30,7 +30,7 @@ def reduce(
keep_attrs: bool | None = None,
keepdims: bool = False,
**kwargs: Any,
) -> Dataset:
) -> Self:
raise NotImplementedError()

def count(
Expand All @@ -39,7 +39,7 @@ def count(
*,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``count`` along some dimension(s).

Expand Down Expand Up @@ -111,7 +111,7 @@ def all(
*,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``all`` along some dimension(s).

Expand Down Expand Up @@ -183,7 +183,7 @@ def any(
*,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``any`` along some dimension(s).

Expand Down Expand Up @@ -256,7 +256,7 @@ def max(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``max`` along some dimension(s).

Expand Down Expand Up @@ -343,7 +343,7 @@ def min(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``min`` along some dimension(s).

Expand Down Expand Up @@ -430,7 +430,7 @@ def mean(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``mean`` along some dimension(s).

Expand Down Expand Up @@ -522,7 +522,7 @@ def prod(
min_count: int | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``prod`` along some dimension(s).

Expand Down Expand Up @@ -629,7 +629,7 @@ def sum(
min_count: int | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``sum`` along some dimension(s).

Expand Down Expand Up @@ -736,7 +736,7 @@ def std(
ddof: int = 0,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``std`` along some dimension(s).

Expand Down Expand Up @@ -840,7 +840,7 @@ def var(
ddof: int = 0,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``var`` along some dimension(s).

Expand Down Expand Up @@ -943,7 +943,7 @@ def median(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``median`` along some dimension(s).

Expand Down Expand Up @@ -1034,7 +1034,7 @@ def cumsum(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``cumsum`` along some dimension(s).

Expand Down Expand Up @@ -1127,7 +1127,7 @@ def cumprod(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``cumprod`` along some dimension(s).

Expand Down Expand Up @@ -1226,7 +1226,7 @@ def reduce(
keep_attrs: bool | None = None,
keepdims: bool = False,
**kwargs: Any,
) -> DataArray:
) -> Self:
raise NotImplementedError()

def count(
Expand All @@ -1235,7 +1235,7 @@ def count(
*,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``count`` along some dimension(s).

Expand Down Expand Up @@ -1301,7 +1301,7 @@ def all(
*,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``all`` along some dimension(s).

Expand Down Expand Up @@ -1367,7 +1367,7 @@ def any(
*,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``any`` along some dimension(s).

Expand Down Expand Up @@ -1434,7 +1434,7 @@ def max(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``max`` along some dimension(s).

Expand Down Expand Up @@ -1513,7 +1513,7 @@ def min(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``min`` along some dimension(s).

Expand Down Expand Up @@ -1592,7 +1592,7 @@ def mean(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``mean`` along some dimension(s).

Expand Down Expand Up @@ -1676,7 +1676,7 @@ def prod(
min_count: int | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``prod`` along some dimension(s).

Expand Down Expand Up @@ -1773,7 +1773,7 @@ def sum(
min_count: int | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``sum`` along some dimension(s).

Expand Down Expand Up @@ -1870,7 +1870,7 @@ def std(
ddof: int = 0,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``std`` along some dimension(s).

Expand Down Expand Up @@ -1964,7 +1964,7 @@ def var(
ddof: int = 0,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``var`` along some dimension(s).

Expand Down Expand Up @@ -2057,7 +2057,7 @@ def median(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``median`` along some dimension(s).

Expand Down Expand Up @@ -2140,7 +2140,7 @@ def cumsum(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``cumsum`` along some dimension(s).

Expand Down Expand Up @@ -2229,7 +2229,7 @@ def cumprod(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``cumprod`` along some dimension(s).

Expand Down
25 changes: 22 additions & 3 deletions xarray/core/accessor_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,25 @@ def _access_through_series(values, name):
months = values_as_series.dt.month.values
field_values = _season_from_months(months)
elif name == "isocalendar":
# special NaT-handling can be removed when
# https://github.com/pandas-dev/pandas/issues/54657 is resolved
field_values = values_as_series.dt.isocalendar()
# test for <NA> and apply needed dtype
hasna = any(field_values.year.isnull())
if hasna:
field_values = np.dstack(
[
getattr(field_values, name).astype(np.float64, copy=False).values
for name in ["year", "week", "day"]
]
)
else:
field_values = np.array(field_values, dtype=np.int64)
# isocalendar returns iso- year, week, and weekday -> reshape
field_values = np.array(values_as_series.dt.isocalendar(), dtype=np.int64)
return field_values.T.reshape(3, *values.shape)
else:
field_values = getattr(values_as_series.dt, name).values

return field_values.reshape(values.shape)


Expand Down Expand Up @@ -110,7 +124,7 @@ def _get_date_field(values, name, dtype):
from dask.array import map_blocks

new_axis = chunks = None
# isocalendar adds adds an axis
# isocalendar adds an axis
if name == "isocalendar":
chunks = (3,) + values.chunksize
new_axis = 0
Expand All @@ -119,7 +133,12 @@ def _get_date_field(values, name, dtype):
access_method, values, name, dtype=dtype, new_axis=new_axis, chunks=chunks
)
else:
return access_method(values, name).astype(dtype, copy=False)
out = access_method(values, name)
# cast only for integer types to keep float64 in presence of NaT
# see https://github.com/pydata/xarray/issues/7928
if np.issubdtype(out.dtype, np.integer):
out = out.astype(dtype, copy=False)
return out


def _round_through_series_or_index(values, name, freq):
Expand Down
4 changes: 2 additions & 2 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,14 +1402,14 @@ def _cov_corr(
) / (valid_count)

if method == "cov":
return cov # type: ignore[return-value]
return cov

else:
# compute std + corr
da_a_std = da_a.std(dim=dim)
da_b_std = da_b.std(dim=dim)
corr = cov / (da_a_std * da_b_std)
return corr # type: ignore[return-value]
return corr


def cross(
Expand Down
8 changes: 4 additions & 4 deletions xarray/core/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,8 @@ def reduce(
obj, rolling_dim, keep_attrs=keep_attrs, fill_value=fillna
)

result = windows.reduce(
func, dim=list(rolling_dim.values()), keep_attrs=keep_attrs, **kwargs
)
dim = list(rolling_dim.values())
result = windows.reduce(func, dim=dim, keep_attrs=keep_attrs, **kwargs)

# Find valid windows based on count.
counts = self._counts(keep_attrs=False)
Expand All @@ -494,14 +493,15 @@ def _counts(self, keep_attrs: bool | None) -> DataArray:
# array is faster to be reduced than object array.
# The use of skipna==False is also faster since it does not need to
# copy the strided array.
dim = list(rolling_dim.values())
counts = (
self.obj.notnull(keep_attrs=keep_attrs)
.rolling(
{d: w for d, w in zip(self.dim, self.window)},
center={d: self.center[i] for i, d in enumerate(self.dim)},
)
.construct(rolling_dim, fill_value=False, keep_attrs=keep_attrs)
.sum(dim=list(rolling_dim.values()), skipna=False, keep_attrs=keep_attrs)
.sum(dim=dim, skipna=False, keep_attrs=keep_attrs)
)
return counts

Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def test_repr_multiindex(self) -> None:

def test_repr_period_index(self) -> None:
data = create_test_data(seed=456)
data.coords["time"] = pd.period_range("2000-01-01", periods=20, freq="B")
data.coords["time"] = pd.period_range("2000-01-01", periods=20, freq="D")

# check that creating the repr doesn't raise an error #GH645
repr(data)
Expand Down
Loading