<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 30, 2018 at 3:21 PM, Allan Haldane <span dir="ltr"><<a href="mailto:allanhaldane@gmail.com" target="_blank">allanhaldane@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 01/30/2018 01:33 PM, <a href="mailto:josef.pktd@gmail.com">josef.pktd@gmail.com</a> wrote:<br>
> AFAICS, one problem is that the padded view didn't come with the<br>
> matching down stream usage support, the pack function as mentioned, an<br>
> alternative way to convert to a standard ndarray, copy doesn't get rid<br>
> of the padding and so on.<br>
><br>
> eg. another mailing list thread I just found with the same problem<br>
> <a href="http://numpy-discussion.10968.n7.nabble.com/view-of-recarray-issue-td32001.html" rel="noreferrer" target="_blank">http://numpy-discussion.10968.<wbr>n7.nabble.com/view-of-<wbr>recarray-issue-td32001.html</a><br>
><br>
> quoting Ralf:<br>
> Question: is that really the recommended way to get an (N, 2) size float<br>
> array from two columns of a larger record array? If so, why isn't there<br>
> a better way? If you'd want to write to that (N, 2) array you have to<br>
> append a copy, making it even uglier. Also, then there really should be<br>
> tests for views in test_records.py.<br>
><br>
><br>
> This "better way" never showed up, AFAIK. And it looks like we came back<br>
> to this problem every few years.<br>
><br>
> Josef<br>
<br>
</span>Since we are at least pushing off this change to a later release<br>
(1.15?), we have some time to prepare/catch up.<br>
<br>
What can we add to numpy.lib.recfunctions to make the multi-field<br>
copy->view change smoother? We have discussed at least two functions:<br>
<br>
 * repack_fields - rearrange the memory layout of a structured array to<br>
add/remove padding between fields<br>
<br>
 * structured_to_unstructured - turns a n-D structured array into an<br>
(n+1)-D unstructured ndarray, whose dtype is the highest common type of<br>
all the fields. May want the inverse function too.<br></blockquote><div><br></div><div>The only sticky point with statsmodels is to have an equivalent of</div><div>

<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">a[['b', 'c']].view(('f8', 2)).</span>

<br></div><div><br></div><div>Highest common dtype might be object, the main usecase for this is to select some elements of a specific dtype and then use them as standard,homogeneous ndarray. In our case and other cases that I have seen it is mainly to select a subset of the floating point numbers.</div><div>Another case of this might be to combine two strings into one 

<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">a[['b', 'c']].view(('S8'))    if b is s5 and c is S3, but I don't think I used this in serious code.</span></div><div><br></div><div>for inverse function: I guess it is still possible to view any standard homogenous ndarray with a structured dtype as long as the itemsize matches.</div><div><br></div><div>Browsing through old mailing list threads, I saw that adding multiple fields or concatenating two arrays with structured dtypes into an array with a single combined dtype was missing and I guess still is. (IIRC this is the usecase where we go now the pandas detour in statsmodels.)</div><div><br></div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
We might also consider<br>
<br>
 * apply_along_fields(arr, method) - applies the method along the<br>
"field" axis, equivalent to something like<br>
method(struct_to_unstructured(<wbr>arr), axis=-1)<br></blockquote><div><br></div><div>If this works on a padded view of an existing array, then this would be an improvement over the current version of having to extract and copy the relevant fields of an existing structured dtype or loop over different numeric dtypes, ints, floats.</div><div><br></div><div>In general there will need to be a way to apply `method` only to selected columns, or columns of a matching dtype. (e.g. We don't want the sum or mean of a string.)</div><div>(e.g. we use ptp() on numeric fields to check if there is already a constant column in the array or dataframe)</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
I think these are pretty minimal and shouldn't be too hard to implement.<br></blockquote><div><br></div><div>AFAICS, it would cover the statsmodels usage.</div><div><br></div><div><br></div><div>Josef</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class="HOEnZb"><font color="#888888"><br>
Allan<br>
</font></span><div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/numpy-<wbr>discussion</a><br>
</div></div></blockquote></div><br></div></div>