Skip to content

ENH: Additions to np.farray API #14966

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions doc/release/upcoming_changes/14966.new_feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
`numpy.asfarray` accepts `copy` argument
----------------------------------------
It is now possible to force `asfarray` to make a copy using `copy=True`.
`dtype` also accepts an argument of `None`, to attempt to preserve the type of
any array-like.
45 changes: 40 additions & 5 deletions numpy/lib/tests/test_type_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,49 @@ def test_basic(self):
assert_all(isrealobj(b))


class TestArrayConversion(object):
class TestAsfarray(object):

def test_asfarray(self):
a = asfarray(np.array([1, 2, 3]))
def test_basic(self):
a = asfarray([1, 2, 3])
assert_equal(a.__class__, np.ndarray)
assert_(np.issubdtype(a.dtype, np.floating))

b = asfarray(np.array([1, 2, 3]))
assert_equal(b.__class__, np.ndarray)
assert_(np.issubdtype(b.dtype, np.floating))

def test_dtype(self):
a = asfarray(np.array([1, 2, 3]), dtype=np.int32)
assert_(np.issubdtype(a.dtype, np.floating))

a = asfarray(np.array([1, 2, 3]), dtype=np.float16)
assert_(np.issubdtype(a.dtype, np.floating))

a = asfarray(np.array([1, 2, 3]), dtype=np.complex128)
assert_(np.issubdtype(a.dtype, np.complexfloating))

def test_dtype_none(self):
a = asfarray(np.array([1, 2, 3]), dtype=None)
assert_(np.issubdtype(a.dtype, np.floating))

b = asfarray(np.array([1.0, 2.0, 3.0]), dtype=None)
assert_(np.issubdtype(b.dtype, np.inexact))

c = asfarray(np.array([1.0+1j, 2.0+2j, 3.0+3j]), dtype=None)
assert_(np.issubdtype(c.dtype, np.complexfloating))

def test_dtype_array(self):
# previously this would infer dtypes from arrays, unlike every single
# other numpy function
assert_raises(TypeError,
asfarray, np.array([1, 2, 3]), dtype=np.array(1.0))
assert_raises(TypeError, asfarray,
np.array([1, 2, 3]), dtype=np.array(1.0))

def test_copy(self):
a = np.array([1, 2, 3])
assert_(asfarray(a, copy=False) is not a)
assert_(asfarray(a, copy=True) is not a)

b = np.array([1.0, 2.0, 3.0])
assert_(asfarray(b, copy=False) is b)
assert_(asfarray(b, copy=True) is not b)

23 changes: 18 additions & 5 deletions numpy/lib/type_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'typename', 'asfarray', 'mintypecode', 'asscalar',
'common_type']

from numpy.core.multiarray import array
import numpy.core.numeric as _nx
from numpy.core.numeric import asarray, asanyarray, isnan, zeros
from numpy.core.overrides import set_module
Expand Down Expand Up @@ -80,22 +81,26 @@ def mintypecode(typechars, typeset='GDFgdf', default='d'):
return l[0][1]


def _asfarray_dispatcher(a, dtype=None):
def _asfarray_dispatcher(a, dtype=None, copy=None):
return (a,)


@array_function_dispatch(_asfarray_dispatcher)
def asfarray(a, dtype=_nx.float_):
def asfarray(a, dtype=_nx.float_, copy=False):
"""
Return an array converted to a float type.

Parameters
----------
a : array_like
The input array.
dtype : str or dtype object, optional
dtype : str or dtype object or None, optional
Float type code to coerce input array `a`. If `dtype` is one of the
'int' dtypes, it is replaced with float64.
'int' dtypes, it is replaced with float64. If `None`, use the dtype of
the array itself in the same way.
copy : bool
If `True`, a copy will be returned even if the input array meets the
type criteria.

Returns
-------
Expand All @@ -112,9 +117,17 @@ def asfarray(a, dtype=_nx.float_):
array([2., 3.])

"""
if dtype is None:
orig = a
a = array(orig, copy=False, subok=False, order=None)
dtype = a.dtype
if a is not orig:
copy = False

if not _nx.issubdtype(dtype, _nx.inexact):
dtype = _nx.float_
return asarray(a, dtype=dtype)

return array(a, dtype=dtype, copy=copy, subok=False, order=None)


def _real_dispatcher(val):
Expand Down