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

Conversation

madphysicist
Copy link
Contributor

@madphysicist madphysicist commented Nov 23, 2019

  1. Added copy parameter which defaults to False.
  2. Added None option to the dtype parameter.

Item #1 is inspired by situations like the one in Stack Overflow question https://stackoverflow.com/q/58998475/2988730. Sometimes, you just need to ensure a copy, and it's nice not to have to check things like if asfarray(x) is x: x = x.copy().

Item #2 solves the problem of trying to do asfarray(x, dtype=x.dtype) for x that don't have a dtype attribute, like lists or tuples.

API Change, so mailing list: https://mail.python.org/pipermail/numpy-discussion/2019-November/080251.html

@madphysicist madphysicist force-pushed the asfarray-features branch 4 times, most recently from 69301dc to fb09cbf Compare November 23, 2019 06:41
- Added `copy=False` parameter
- Added `dtype=None` option
@seberg
Copy link
Member

seberg commented Nov 24, 2019

To be honest, I am not too convinced right now. Another thing, you mentioned .astype(np.inexact), that is not well defined in a sense. It just ends up being .astype(np.float64) (also copy=True) is already the default. My main squirm is that np.farray is a very uncommon function, and I am not sure that adding these things improves the situation much with respect to the original question.

@madphysicist
Copy link
Contributor Author

@seberg. The use-case I have in my actual project (which does use asfarray pretty extensively) is asfarray(x, dtype=None, copy=True). I need to be able to pass through any inexact point type from any array-like, and only convert the integers. Right now, asfarray only operates on a fixed type, and so requires you to determine the type manually.

I don't see this being an uncommon function being a problem. If the the limited interface is the issue, then this will make more common. If not, then this change does not affect that many people.

@seberg
Copy link
Member

seberg commented Nov 24, 2019

I am just not sure I like the whole dance of first creating an array. The main issue though is that I have never used this function, and the whole way it works (replacing the input dtype if it is not floating, but ignoring the arrays dtype) is strange. So I guess my main concern is that I do not like this funciton/approach to begin with...

@madphysicist
Copy link
Contributor Author

@seberg. I don't like that particular dance either. If there was a reasonable way to assess the dtype without creating an array, I'd be happy to use that instead.

I've used this function quite a bit in certain situations where I need to do operations on floating point numbers. astype is nice, but it only works if you already have an array. And creating a copy is also a very nice feature to have.

@eric-wieser
Copy link
Member

in certain situations where I need to do operations on floating point numbers.

Can you give an example of when you care about the numbers being floating point?

@madphysicist
Copy link
Contributor Author

The specific example I have is a small scikit I'm working on. I do a whole bunch of in-place floating-point math on the inputs (division, fractional exponents, etc.), so it's very convenient to be able to create a copy as I convert the input to float.

@seberg
Copy link
Member

seberg commented Dec 3, 2019

I would personally (at this time at least) probably opt for a pattern such as:

float_dtype = np.result_type(arr.dtype, 0.)
arr = arr.astype(float_dtype, copy=False)

Not sure if that is what you are looking for.

@seberg seberg added the 57 - Close? Issues which may be closable unless discussion continued label Dec 3, 2019
@madphysicist
Copy link
Contributor Author

madphysicist commented Dec 3, 2019

@seberg That doesn't allow passing in arbitrary array-like objects that may not have a dtype or astype. That's the main allure of this function to begin with, and why I want it to have a complete set of options.

@seberg
Copy link
Member

seberg commented Apr 10, 2020

I am sorry, but I think I will close this for now (for now, happy to reopen). I am still not convinced and it has been a while. This just does not feel like a neat API where extending it actually helps the general user. I am sure it can be useful to have a helper for it for libraries, but I doubt this is it to be honest. I am more likely to accept deprecating it with a message: This is how you might spell a better function...

@seberg seberg closed this Apr 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 - Enhancement 57 - Close? Issues which may be closable unless discussion continued component: numpy.lib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants