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(?): Improved structured array creation support #10910
Comments
If you want the You might consider this a little verbose, but it ensures that only one copy is made. |
A discussion on stack overflow provides some extra examples and insight . I prefer working with numpy rather than pandas for mixed data type arrays for a variety of reasons. The use of a list comprehension to change a list of lists to a list of tuples seems to be the easiest to remember before applying the desired dtype. |
@Dan-Patterson: Converting to tuples and back is O( |
@eric-wieser: Thanks, that will also do. However, I would like to know if the current result from |
@eric-wieser your edited expression using .astype is much clearer now. |
We actually discussed introducing a function http://numpy-discussion.10968.n7.nabble.com/Setting-custom-dtypes-and-1-14-tp45156p45207.html You can see the docstrings and implementations here: Further suggestions welcome. I think we are on hold actually merging it because we were waiting to decide how to implement |
@attoblarg the current result from In other words, if you are sure your starting array is contiguous in memory with no padding bytes or strides, and has exactly 64 bit entries, then doing |
Closing. BTW, |
Creating a structured array from already existing data (to enable named-field access) seems to be more difficult than I expect.
What I expected was I could pass my existing 2D ndarray to numpy.array with a new dtype:
This does give the expected dtype (
[('a', '<f8'), ('b', '<i4')]
), but the resulting array has repeated entries and more dimensions:and the field access
b['a']
gives:Using
np.array
,np.asarray
, orndarray.astype
all do this.Using direct assignment of
ndarray.dtype
or usingndarray.view
(withdtype=[('a', float), ('b', float)]
) gives something a little different (still the wrong number of dimensions):and the field access
b['a']
gives:What does give the expected result:
numpy.array
is passed a list of tuples ([(0,1), (2,3)]
), then it behaves as expected (not list of lists, list of 1D ndarrays, 2D ndarray, etc.). But copying large arrays into tuples first not great.numpy.rec.fromarrays
, passing the transpose ofa
. A record array isn't necessary, but this is an easy solution.So there is a currently available solution, but it still seems that some of the seemingly straight-forward ways of doing this don't give the expected result.
The text was updated successfully, but these errors were encountered: