Skip to content
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

API: Reshaping Series to its own shape raises TypeError #4554

Closed
goyodiaz opened this issue Aug 13, 2013 · 5 comments · Fixed by #4659
Closed

API: Reshaping Series to its own shape raises TypeError #4554

goyodiaz opened this issue Aug 13, 2013 · 5 comments · Fixed by #4659

Comments

@goyodiaz
Copy link
Contributor

This may be seen like a weird function call but it is actually triggered when plotting with development versions of matplotlib and the case is explicitly (and wrongly as it seems) by pandas:

>>> import pandas as pd
>>> x = pd.Series(range(5))
>>> x.reshape(x.shape)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.7/pandas/core/series.py", line 981, in reshape
    return ndarray.reshape(self, newshape, order)
TypeError: an integer is required

It is easily fixed just replacing the offending line by this one:

return ndarray.reshape(self, newshape, order=order)

@jreback
Copy link
Contributor

jreback commented Aug 13, 2013

actually....this should be changed entirely; something like this:

if not isinstance(newshape, tuple):
  newshape = tuple([ newshape ])
if newshape == self.shape:
      return self
return self.values.reshape(newshape,order=order)

IOW a reshape that does nothing is ok, other wise you get back an ndarray

a Series is by definition a 1-D object

@goyodiaz
Copy link
Contributor Author

The current implementation (with the change I proposed) behaves well AFAICS:

>>> import pandas as pd
>>> s = pd.Series(range(4))
>>> s.reshape((4,))
0    0
1    1
2    2
3    3
dtype: int64
>>> s.reshape(4)
0    0
1    1
2    2
3    3
dtype: int64
>>> s.reshape((2, 2))
array([[0, 1],
       [2, 3]])
>>> s.reshape(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.7/pandas/core/series.py", line 981, in reshape
    return ndarray.reshape(self, newshape, order=order)
ValueError: total size of new array must be unchanged
>>> s.reshape((3,))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.7/pandas/core/series.py", line 981, in reshape
    return ndarray.reshape(self, newshape, order=order)
ValueError: total size of new array must be unchanged

and is pretty clear to me, except for the order check at the beginning which is irrelevant for my use cases.

But yours passes these same tests so I would be happy with it and I think matplotlib would be as well.

@jreback
Copy link
Contributor

jreback commented Aug 13, 2013

I think the order is just for compat with numpy
I agree not that useful
want to so a pull request for this change?

@jtratner
Copy link
Contributor

Nitpicky: why not just add a comma at end instead of passing list to tuple
constructor?

@goyodiaz
Copy link
Contributor Author

#4659

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

Successfully merging a pull request may close this issue.

3 participants