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

ENH: adding casting option to numpy.stack. #21627

Merged
merged 18 commits into from Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
ea75651
ENH: adding casting option to numpy.stack.
jhonatancunha May 17, 2022
5a252a9
ENH: adding dtype option to numpy.stack.
JessePires May 30, 2022
2c921e0
ENH: adding dtype option to numpy.stack.
JessePires May 31, 2022
c6ec33e
REV: removing auto-generated file loops_modulo.dispatch.c
jhonatancunha May 31, 2022
a05c709
Merge branch 'add_casting_to_stack' of github.com:jhonatancunha/numpy…
jhonatancunha May 31, 2022
48101bc
REV: removing auto-generated file loops_modulo.dispatch.c
jhonatancunha May 31, 2022
c0b3b2e
REV: removing inserted newlines
jhonatancunha Jun 2, 2022
ec714a3
DOC: inserting versionadded info in dtype and casting parameters.
jhonatancunha Jun 2, 2022
653aa1a
TST: writing tests to stack method with dtype and casting options
jhonatancunha Jun 2, 2022
9b8d4cd
DOC: adding upcoming_change file for new options casting and dtype in…
jhonatancunha Jun 3, 2022
df2c73f
REV: reverting lint errors.
jhonatancunha Jun 3, 2022
da11545
DOC: inserting hstack and vstack methods in upcoming changes
jhonatancunha Jun 4, 2022
8296c43
ENH: adding dtype and casting keyword arguments to numpy.vstack and n…
jhonatancunha Jun 4, 2022
7bf6781
TST: writing tests to vstack and hstack methods with dtype and castin…
jhonatancunha Jun 4, 2022
ce0ae35
REV: reverting the 'out' option type in stack method.
jhonatancunha Jun 4, 2022
1e94f43
REV: Reverting out type changes in overload of shape_base.pyi file.
JessePires Jun 6, 2022
43eadef
DOC: correcting some english erros in upcoming_changes file.
jhonatancunha Jun 8, 2022
94eb6c1
Merge branch 'add_casting_to_stack' of github.com:jhonatancunha/numpy…
jhonatancunha Jun 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions numpy/core/shape_base.py
Expand Up @@ -345,7 +345,7 @@ def hstack(tup):
return _nx.concatenate(arrs, 1)


def _stack_dispatcher(arrays, axis=None, out=None):
def _stack_dispatcher(arrays, axis=None, out=None, casting=None):
jhonatancunha marked this conversation as resolved.
Show resolved Hide resolved
arrays = _arrays_for_stack_dispatcher(arrays, stacklevel=6)
if out is not None:
# optimize for the typical case where only arrays is provided
Expand All @@ -355,7 +355,7 @@ def _stack_dispatcher(arrays, axis=None, out=None):


@array_function_dispatch(_stack_dispatcher)
def stack(arrays, axis=0, out=None):
def stack(arrays, axis=0, out=None, casting='same_kind'):
"""
Join a sequence of arrays along a new axis.

Expand All @@ -378,6 +378,10 @@ def stack(arrays, axis=0, out=None):
correct, matching that of what stack would have returned if no
out argument were specified.

casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
Controls what kind of data casting may occur. Defaults to 'same_kind'.


Returns
-------
stacked : ndarray
Expand Down Expand Up @@ -430,15 +434,17 @@ def stack(arrays, axis=0, out=None):

sl = (slice(None),) * axis + (_nx.newaxis,)
expanded_arrays = [arr[sl] for arr in arrays]
return _nx.concatenate(expanded_arrays, axis=axis, out=out)
return _nx.concatenate(expanded_arrays, axis=axis, out=out,
casting=casting)


# Internal functions to eliminate the overhead of repeated dispatch in one of
# the two possible paths inside np.block.
# Use getattr to protect against __array_function__ being disabled.
_size = getattr(_from_nx.size, '__wrapped__', _from_nx.size)
_ndim = getattr(_from_nx.ndim, '__wrapped__', _from_nx.ndim)
_concatenate = getattr(_from_nx.concatenate, '__wrapped__', _from_nx.concatenate)
_concatenate = getattr(_from_nx.concatenate,
'__wrapped__', _from_nx.concatenate)


def _block_format_index(index):
Expand Down Expand Up @@ -539,7 +545,7 @@ def _concatenate_shapes(shapes, axis):
"""Given array shapes, return the resulting shape and slices prefixes.

These help in nested concatenation.

Returns
-------
shape: tuple of int
Expand Down
16 changes: 15 additions & 1 deletion numpy/core/shape_base.pyi
Expand Up @@ -2,63 +2,77 @@ from collections.abc import Sequence
from typing import TypeVar, overload, Any, SupportsIndex

from numpy import generic
from numpy._typing import ArrayLike, NDArray, _ArrayLike
from numpy._typing import ArrayLike, NDArray, _ArrayLike, _CastingKind
BvB93 marked this conversation as resolved.
Show resolved Hide resolved

_SCT = TypeVar("_SCT", bound=generic)
_ArrayType = TypeVar("_ArrayType", bound=NDArray[Any])

__all__: list[str]


jhonatancunha marked this conversation as resolved.
Show resolved Hide resolved
@overload
def atleast_1d(arys: _ArrayLike[_SCT], /) -> NDArray[_SCT]: ...
@overload
def atleast_1d(arys: ArrayLike, /) -> NDArray[Any]: ...
@overload
def atleast_1d(*arys: ArrayLike) -> list[NDArray[Any]]: ...


@overload
def atleast_2d(arys: _ArrayLike[_SCT], /) -> NDArray[_SCT]: ...
@overload
def atleast_2d(arys: ArrayLike, /) -> NDArray[Any]: ...
@overload
def atleast_2d(*arys: ArrayLike) -> list[NDArray[Any]]: ...


@overload
def atleast_3d(arys: _ArrayLike[_SCT], /) -> NDArray[_SCT]: ...
@overload
def atleast_3d(arys: ArrayLike, /) -> NDArray[Any]: ...
@overload
def atleast_3d(*arys: ArrayLike) -> list[NDArray[Any]]: ...


@overload
def vstack(tup: Sequence[_ArrayLike[_SCT]]) -> NDArray[_SCT]: ...
@overload
def vstack(tup: Sequence[ArrayLike]) -> NDArray[Any]: ...


@overload
def hstack(tup: Sequence[_ArrayLike[_SCT]]) -> NDArray[_SCT]: ...
@overload
def hstack(tup: Sequence[ArrayLike]) -> NDArray[Any]: ...


@overload
def stack(
arrays: Sequence[_ArrayLike[_SCT]],
axis: SupportsIndex = ...,
out: None = ...,
casting: None | _CastingKind = ...
) -> NDArray[_SCT]: ...


@overload
def stack(
arrays: Sequence[ArrayLike],
axis: SupportsIndex = ...,
out: None = ...,
casting: None | _CastingKind = ...
) -> NDArray[Any]: ...


@overload
def stack(
arrays: Sequence[ArrayLike],
axis: SupportsIndex = ...,
out: _ArrayType = ...,
casting: None | _CastingKind = ...
) -> _ArrayType: ...


@overload
def block(arrays: _ArrayLike[_SCT]) -> NDArray[_SCT]: ...
@overload
Expand Down