Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: allow reshape 2-D to return a bare 1-d list #11921

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

Merged
merged 2 commits into from
Sep 9, 2018

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Aug 23, 2018

PR Summary

reverts a bit of the logic in #8116 Attn @anntzer.

This case allows

import matplotlib.cbook as cbook

x = [datetime(2018,1,1),  datetime(2018, 2, 1),  datetime(2018,3, 1)]
xnew = cbook._reshape_2D(x, 'x')

to return

[array([datetime.datetime(2018, 1, 1, 0, 0),
       datetime.datetime(2018, 2, 1, 0, 0),
       datetime.datetime(2018, 3, 1, 0, 0)], dtype=object)]

rather than

[array([datetime.datetime(2018, 1, 1, 0, 0)], dtype=object), 
 array([datetime.datetime(2018, 2, 1, 0, 0)], dtype=object), 
 array([datetime.datetime(2018, 3, 1, 0, 0)], dtype=object)]

Closes #11899 Attn @anntzer and @dstansby

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@@ -1397,8 +1397,11 @@ def _reshape_2D(X, name):
# 1D array of scalars: directly return it.
return [X]
elif X.ndim in [1, 2]:
# 2D array, or 1D array of iterables: flatten them first.
return [np.reshape(x, -1) for x in X]
if hasattr(X[0], '__len__'):
Copy link
Contributor

Choose a reason for hiding this comment

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

Or just replace the X.dtype.type != np.object_ check above by not hasattr(X[0], "__iter__")/not isinstance(X[0], collections.abc.Iterable)? That seems to match what the comments intended too. (__iter__ or __len__, I don't really care.)

@jklymak
Copy link
Member Author

jklymak commented Aug 29, 2018

@anntzer that seems to work. I don't understand this function very well though and why it bothers to transpose ndarrays, but not lists of lists. I actually think this should be fixed to be consistent, but I don't use box plot, or violin plot, to be able to do it properly. ping @phobson who seemed to work on box plot at some point...

@phobson
Copy link
Member

phobson commented Aug 31, 2018

I don't have much attachment to this function in particular w/r/t boxplots.

In the context of boxplots plots, you can think of an array in two ways:

  1. as table of columns
  2. as a list of 1-d arrays

The boxplot function assumes the former.

This doesn't seem to break that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Histogram of list of datetimes
4 participants