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

Conversation

anntzer
Copy link
Contributor
@anntzer anntzer commented Mar 20, 2016

See #6366.

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 ;).

@seberg
Copy link
Member
seberg commented Mar 20, 2016

If you don't mind, could make the commit message "ENH: ..." or similar, we usually try to keep them like that. Anyway, looks good to me, test changes seem good, will merge in a bit if I don't forget.

@anntzer anntzer force-pushed the new-fill-dtype-behavior branch from 0fb7e98 to be3f71d Compare March 20, 2016 15:50
@anntzer
Copy link
Contributor Author
anntzer commented Mar 20, 2016

Fixed, and added error checking for the object array case (similar to numpy/core/src/multiarray/convert.c: PyArray_FillWithScalar).

@seberg
Copy link
Member
seberg commented Mar 20, 2016

Thanks, but I am not sure we should put it in here :(, it is again a change in behaviour that should likely have a Deprecation period. Easiest would maybe be to put this in without it now, and open a new PR to deprecate it? If you feel strongly about it, Could make it a deprecation here and we wait if someone else comments.

@anntzer anntzer force-pushed the new-fill-dtype-behavior branch from be3f71d to a073198 Compare March 20, 2016 16:02
@anntzer
Copy link
Contributor Author
anntzer commented Mar 20, 2016

Undid the previous commit. I'll leave it as it is because I guess it should really be a FutureWarning (I still think np.full(3, (1, 2, 3)) should return an object array containing three tuples but whatever, I don't really care).

@seberg
Copy link
Member
seberg commented Mar 20, 2016

Thanks a lot @anntzer. I agree, but easier to do the clear stuf first :).

seberg added a commit that referenced this pull request Mar 20, 2016
MAINT: np.full now defaults to the filling value's dtype.
@seberg seberg merged commit a52eef3 into numpy:master Mar 20, 2016
@anntzer anntzer deleted the new-fill-dtype-behavior branch March 20, 2016 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0