Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reshape of pandas series?

Tags:

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.

like image 878
szli Avatar asked Jan 17 '13 23:01

szli


People also ask

How do you change the shape of a series in pandas?

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: convert DataFrame to numpy ndarray. do reshape.

What does reshape do in pandas?

In Pandas data reshaping means the transformation of the structure of a table or vector (i.e. DataFrame or Series) to make it suitable for further analysis. Some of Pandas reshaping capabilities do not readily exist in other environments (e.g. SQL or bare bone R) and can be tricky for a beginner.

How do you reshape a Pandas DataFrame?

You can use the following basic syntax to convert a pandas DataFrame from a wide format to a long format: df = pd. melt(df, id_vars='col1', value_vars=['col2', 'col3', ...]) In this scenario, col1 is the column we use as an identifier and col2, col3, etc.


2 Answers

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.

like image 194
Andy Hayden Avatar answered Nov 06 '22 02:11

Andy Hayden


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]]) 
like image 39
Chang She Avatar answered Nov 06 '22 00:11

Chang She