Skip to content

ENH: Print funny-shaped empty arrays using "empty" #10119

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

ahaldane
Copy link
Member

Makes arrays like np.empty((2,0,1)) print as np.empty((2,0,1), dtype=float64). The old behavior was to print as np.array([], dtype=float64) no matte the shape, which could be misleading.

This was discussed in #9792

@@ -3908,6 +3908,8 @@ def __repr__(self):
self.size == 0
)

is_ndim_empty = self.size == 0 and len(self.shape) > 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: self.ndim

mask=False,
fill_value=1e+20,
dtype=float64)''')
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for non-unusual shapes too?

@eric-wieser
Copy link
Member

Well, that's fun:

AssertionError: 
Items are not equal:
 ACTUAL: 'masked_array(data=empty((0L, 2L)),\n             mask=False,\n       fill_value=1e+20,\n            dtype=float64)'
 DESIRED: 'masked_array(data=empty((0, 2)),\n             mask=False,\n       fill_value=1e+20,\n            dtype=float64)'

Only one of our test instances actually catches this. I suppose we could do repr(shape).replace('L','') # for python 2 to work around it

@eric-wieser
Copy link
Member

eric-wieser commented Nov 29, 2017

Also, we didn't consider subclasses. Currently:

>>> class MyArray(np.ndarray): pass
>>> np.empty((0, 20)).view(MyArray)
MyArray([], shape=(0, 20), dtype=float64)
>>> np.ma.array(_)
masked_MyArray(data=[],
               mask=False,
         fill_value=1e+20)

With this patch, I think we lose the subclass information

@eric-wieser
Copy link
Member

The old behavior was to print as np.array([], dtype=float64) no matte the shape, which could be misleading.

Not true, it prints as array([], shape=(2, 0, 1), dtype=float64).

We could also print it as ndarray(shape=(2, 0, 1), dtype=float64), which would actually be evaluable.

@ahaldane
Copy link
Member Author

ahaldane commented Nov 29, 2017

Hmm yeah. The reprs for subclasses also aren't evaluable currently.

The ouptut for the size-0 array in your example is not evaluable, it gives MyArray([], shape=(0, 20), dtype=float64). But it needs to be MyArray(shape=(0, 20), dtype=float64).

Non-empty subclassed ndarrays also don't work:

>>> MyArray(shape=(2), dtype=float64)
MyArray([  7.74860419e-304,   7.74860419e-304])
>>> MyArray([  7.74860419e-304,   7.74860419e-304])
TypeError: 'float' object cannot be interpreted as an index

Since there seem to be so many cases already that aren't evaluable, I am tempted to give up on this PR and just say "the repr often isn't evaluable", and leave the non-evaluable array([], shape=(xx))) repr.

@ahaldane
Copy link
Member Author

I'm in favor of leaving this the way it is. If anyone else wants to give it a shot feel free, I'm going to close this one.

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.

2 participants