8000 np.full now defaults to the filling value's dtype. by anntzer · Pull Request #7437 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

np.full now defaults to the filling value's dtype. #7437

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

Merged
merged 1 commit into from
Mar 20, 2016
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: 2 additions & 0 deletions doc/release/1.12.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ more explanation.
FutureWarning to changed behavior
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* ``np.full`` now returns an array of the fill-value's dtype if no dtype is
given, instead of defaulting to float.

C API
~~~~~
Expand Down
13 changes: 5 additions & 8 deletions numpy/core/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,8 @@ def full(shape, fill_value, dtype=None, order='C'):
fill_value : scalar
Fill value.
dtype : data-type, optional
The desired data-type for the array, e.g., `np.int8`. Default
is `float`, but will change to `np.array(fill_value).dtype` in a
future release.
The desired data-type for the array The default, `None`, means
`np.array(fill_value).dtype`.
order : {'C', 'F'}, optional
Whether to store multidimensional data in C- or Fortran-contiguous
(row- or column-wise) order in memory.
Expand All @@ -289,16 +288,14 @@ def full(shape, fill_value, dtype=None, order='C'):
>>> np.full((2, 2), np.inf)
array([[ inf, inf],
[ inf, inf]])
>>> np.full((2, 2), 10, dtype=np.int)
>>> np.full((2, 2), 10)
array([[10, 10],
[10, 10]])

"""
if dtype is None:
dtype = array(fill_value).dtype
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder about stupid objects, but I guess we just shouldn't care, anyone who wants to do np.full(3, (1, 2)) should be prepared to give dtype=object. (could make it copy=False just for the kicks)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, np.full(3, (1, 2)) (with or without passing dtype=object) didn't work and still doesn't work. The real fun happens when someone write np.full(3, (1, 2, 3)) though (it used to, and still, returns array([1, 2, 3])).

Is there a function equivalent to array(fill_value).dtype, but that would actually return object when a non-scalar is passed in? I thought np.obj2sctype(..., default=object) would work, but it returns the dtype of a ndarray when a ndarray is passed in (so np.full(3, np.array([1, 2, 3])) would still "fail"). (By the way I find obj2sctype's "return None if everything fails" somewhat unpythonic.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forget about this, I guess.... object dtype is funny, and you just have to do it manually. Even noticed we had talked about it before, heh. I guess you are right about obj2sctype, it might be trying to recreate the corresponding C-function, no idea....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should we consider that np.full(3, (1, 2, 3)) ==> np.array([1, 2, 3]) (the old behavior, kept in this patch) is not a stopper? I feel like there should be a way to at least error out there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there should be an error. arr.fill kind of tries this, not sure about all the logic it uses.

In any case, this is an orthogonal issue, so I think we should ignore it here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really, the only reason I didn't merge it yet, was that I liked our special tag, like ENH:, or actually I guess here MAINT: is better, in the commit message ;).

a = empty(shape, dtype, order)
if dtype is None and array(fill_value).dtype != a.dtype:
warnings.warn(
"in the future, full({0}, {1!r}) will return an array of {2!r}".
format(shape, fill_value, array(fill_value).dtype), FutureWarning)
multiarray.copyto(a, fill_value, casting='unsafe')
return a

Expand Down
11 changes: 0 additions & 11 deletions numpy/core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,17 +485,6 @@ def test_simple(self):
arr.__getitem__, (slice(None), index))


class TestFullDefaultDtype(object):
"""np.full defaults to float when dtype is not set. In the future, it will
use the fill value's dtype.
"""

def test_full_default_dtype(self):
assert_warns(FutureWarning, np.full, 1, 1)
assert_warns(FutureWarning, np.full, 1, None)
assert_no_warnings(np.full, 1, 1, float)


class TestDatetime64Timezone(_DeprecationTestCase):
"""Parsing of datetime64 with timezones deprecated in 1.11.0, because
datetime64 is now timezone naive rather than UTC only.
Expand Down
55 changes: 27 additions & 28 deletions numpy/core/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1847,49 +1847,48 @@ def test_scalars(self):


class TestCreationFuncs(TestCase):
# Test ones, zeros, empty and filled
# Test ones, zeros, empty and full.

def setUp(self):
self.dtypes = ('b', 'i', 'u', 'f', 'c', 'S', 'a', 'U', 'V')
dtypes = {np.dtype(tp) for tp in itertools.chain(*np.sctypes.values())}
# void, bytes, str
variable_sized = {tp for tp in dtypes if tp.str.endswith('0')}
self.dtypes = sorted(dtypes - variable_sized |
{np.dtype(tp.str.replace("0", str(i)))
for tp in variable_sized for i in range(1, 10)},
key=lambda dtype: dtype.str)
self.orders = {'C': 'c_contiguous', 'F': 'f_contiguous'}
self.ndims = 10

def check_function(self, func, fill_value=None):
par = (
(0, 1, 2),
range(self.ndims),
self.orders,
self.dtypes,
2**np.arange(9)
)
par = ((0, 1, 2),
range(self.ndims),
self.orders,
self.dtypes)
fill_kwarg = {}
if fill_value is not None:
fill_kwarg = {'fill_value': fill_value}
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
for size, ndims, order, type, bytes in itertools.product(*par):
for size, ndims, order, dtype in itertools.product(*par):
shape = ndims * [size]
try:
dtype = np.dtype('{0}{1}'.format(type, bytes))
except TypeError: # dtype combination does not exist

# do not fill void type
if fill_kwarg and dtype.str.startswith('|V'):
continue
else:
# do not fill void type
if fill_value is not None and type in 'V':
continue

arr = func(shape, order=order, dtype=dtype,
**fill_kwarg)
arr = func(shape, order=order, dtype=dtype,
**fill_kwarg)

assert_(arr.dtype == dtype)
assert_(getattr(arr.flags, self.orders[order]))
assert_equal(arr.dtype, dtype)
assert_(getattr(arr.flags, self.orders[order]))

if fill_value is not None:
if dtype.str.startswith('|S'):
val = str(fill_value)
else:
val = fill_value
assert_equal(arr, dtype.type(val))
if fill_value is not None:
if dtype.str.startswith('|S'):
val = str(fill_value)
else:
val = fill_value
assert_equal(arr, dtype.type(val))

def test_zeros(self):
self.check_function(np.zeros)
Expand All @@ -1900,7 +1899,7 @@ def test_ones(self):
def test_empty(self):
self.check_function(np.empty)

def test_filled(self):
def test_full(self):
self.check_function(np.full, 0)
self.check_function(np.full, 1)

Expand Down
0