28

It looks to me like a bug in pandas.Series.

a = pd.Series([1,2,3,4])
b = a.reshape(2,2)
b

b has type Series but can not be displayed, the last statement gives exception, very lengthy, the last line is "TypeError: %d format: a number is required, not numpy.ndarray". b.shape returns (2,2), which contradicts its type Series. I am guessing perhaps pandas.Series does not implement reshape function and I am calling the version from np.array? Anyone see this error as well? I am at pandas 0.9.1.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
szli
  • 36,893
  • 11
  • 32
  • 40
  • 3
    I am not very familiar with Pandas, but I understand that its charms and limitations lie in having dedicated objects for arrays of different dimensions. So even if there is numpy in the background, `pd.Series` is always 1D, and `pd.DataFrame` is always 2D. So reshaping one of those objects the way your doing does not make much sense. – Jaime Jan 18 '13 at 00:02
  • And "the way **your** doing" should be "the way **you're** doing"... Shame on me! – Jaime Jan 18 '13 at 00:29

5 Answers5

50

You can call reshape on the values array of the Series:

In [4]: a.values.reshape(2,2)
Out[4]: 
array([[1, 2],
       [3, 4]], dtype=int64)

I actually think it won't always make sense to apply reshape to a Series (do you ignore the index?), and that you're correct in thinking it's just numpy's reshape:

a.reshape?
Docstring: See numpy.ndarray.reshape

that said, I agree the fact that it let's you try to do this looks like a bug.

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • I once subclassed `ndarray` to implement a fixed dimensionality object. It is tempting to catch the `reshape`s and don't allow them, but a lot of the cool things you've come to love in numpy rely on altering the dimensions of the underlying data, e.g. get rid of `reshape` and `tile` does not work any more. May be this is a small, unavoidable, price to pay for reusing the numpy engine in Pandas. – Jaime Jan 18 '13 at 02:19
  • @Jaime the fact that it causes an exception when you try to do it is surely a bug, either it should let you do it to a DataFrame (and reindex) or the method shouldn't be available? – Andy Hayden Jan 18 '13 at 07:09
  • The point is you cannot make it unavailable without breaking other functionality, unless you are willing to redo a lot of what numpy gives you for free. It isn't nice, I agree, but it may really be the best that is possible. – Jaime Jan 18 '13 at 07:13
  • @isulsz I don't use Pandas, so I really couldn't care less, I am not against changing the behavior of `Series`, do submit a bug report. But the problem is not having access to `reshape` from within your object, but knowing if the call to `reshape` your object got is coming from a user, that should get a `NotImplemented` exception, or from another numpy method relying on `reshape` to do its thing. In numpy, `matrix` is supposed to be a 2D object, but you can reshape it to 1D or 3D, because if not you wouldn't be able to, for instance, `np.tile` a `matrix`. – Jaime Jan 18 '13 at 22:48
3

The reshape function takes the new shape as a tuple rather than as multiple arguments:

In [4]: a.reshape?
Type:       function
String Form:<function reshape at 0x1023d2578>
File:       /Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/numpy/core/fromnumeric.py
Definition: numpy.reshape(a, newshape, order='C')
Docstring:
Gives a new shape to an array without changing its data.

Parameters
----------
a : array_like
    Array to be reshaped.
newshape : int or tuple of ints
    The new shape should be compatible with the original shape. If
    an integer, then the result will be a 1-D array of that length.
    One shape dimension can be -1. In this case, the value is inferred
    from the length of the array and remaining dimensions.

Reshape is actually implemented in Series and will return an ndarray:

In [11]: a
Out[11]: 
0    1
1    2
2    3
3    4

In [12]: a.reshape((2, 2))
Out[12]: 
array([[1, 2],
       [3, 4]])
Chang She
  • 16,692
  • 8
  • 40
  • 25
2

you can directly use a.reshape((2,2)) to reshape a Series, but you can not reshape a pandas DataFrame directly, because there is no reshape function for pandas DataFrame, but you can do reshape on numpy ndarray:

  1. convert DataFrame to numpy ndarray
  2. do reshape
  3. convert back

e.g.

a = pd.DataFrame([[1,2,3],[4,5,6]])
b = a.as_matrix().reshape(3,2)
a = pd.DataFrame(b)
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
176coding
  • 2,933
  • 4
  • 17
  • 18
0

Just use this below code:

b=a.values.reshape(2,2)

I think it will help you. u can directly use only reshape() function.but it will give future warning

Akash Nayak
  • 811
  • 10
  • 13
  • 1
    Do add a bit of explanation along with the code as it helps to understand your code. Code only answers are frowned upon. – Bhargav Rao Nov 15 '17 at 13:04
-2

for example we have a series. We can change it to dataframe like this way;

a = pd.DataFrame(a)

Navarra B
  • 1
  • 1