Repository navigation
BUG: Fix structured array format functions - #8200
Conversation
|
@ahaldane Just a heads up. |
|
We need to get this finished. |
|
Reading the document of |
|
Great! @ahaldane Heads up. |
|
I'll take a look soon (today or next few days). |
| is_array_field = 1 < field_values.ndim | ||
| format_function = _get_format_function( | ||
| ravel(field_values), precision, suppress_small, formatter) | ||
| if is_array_field: |
There was a problem hiding this comment.
I would prefer if dtype_[field_name].shape != ():, so we don't need is_array_field.
| format_function = _get_format_function( | ||
| ravel(field_values), precision, suppress_small, formatter) | ||
| if is_array_field: | ||
| format_function = SubArrayFormat(format_function) |
There was a problem hiding this comment.
Just a note: SubarrayFormat here does not take into account precision, suppress_small, formatter. Maybe it should, but if so let's leave that for a future PR. Current numpy master doesn't take them into account either.
| x = _convert_arrays(x) | ||
| lst = style(x) | ||
| if a.dtype.fields is not None: | ||
| arr = asarray([x], dtype=a.dtype) |
There was a problem hiding this comment.
Can it be simplified to arr = array([x])?
| def __call__(self, arr): | ||
| if arr.ndim <= 1: | ||
| return "[" + ", ".join(self.format_function(a) for a in arr) + "]" | ||
| return "[" + ", ".join(self.__call__(a) for a in arr) + "]" |
There was a problem hiding this comment.
Just just to note, probably a more exhaustive solution would recursively call array2string somehow (but with very large max_line_width?). But this looks fine for this PR.
There was a problem hiding this comment.
I think so too. First, I tried it a little and found it's not easy. So, I made SubArrayFormat class as the second way.
| if a.shape == (): | ||
| x = a.item() | ||
| if isinstance(x, tuple): | ||
| x = _convert_arrays(x) |
There was a problem hiding this comment.
This is one of the two places _convert_arrays is called, which is the old-style structure formatter. Is the other call (in _formatArray) still needed? It looks like that one can never be reached. If so, you might remove _convert_arrays completely.
There was a problem hiding this comment.
I removed all _convert_arrays and replace the code on rank == 0 with ValueError to prevent misuse of _formatArray.
Related to PR #8200
|
All right, LGTM. Thanks a lot for the followup @skwbc! Merging now. |
1st commit
dtype_.descrtodtype_.names.2nd commit
Closes #8172
Reason of a test failure
test_format_on_flex_array_element (test_regression.TestRegression) fails because of the following
TypeError.It is because
array2stringcalls.item()method ofarr[0]and.item()casts the type ofdatefield fromnumpy.datetime64todatetime.time.Then
DatetimeFormatcallsdatetime_as_stringwith thedatetime.timeobject andTypeErrorraised (See followings).I don't understand the mechanism of this casting yet.
If this is a bug we should fix it.
If this is not a bug, I have to fix the code to treat this error in this PR.