AttributeError: 'Series' object has no attribute 'reshape' AttributeError: 'Series' object has no attribute 'reshape' python-3.x python-3.x

AttributeError: 'Series' object has no attribute 'reshape'


Solution was linked on reshaped method on documentation page.

Insted of Y.reshape(-1,1) you need to use:

Y.values.reshape(-1,1)


The solution is indeed to do:

Y.values.reshape(-1,1)

This extracts a numpy array with the values of your pandas Series object and then reshapes it to a 2D array.

The reason you need to do this is that pandas Series objects are by design one dimensional. Another solution if you would like to stay within the pandas library would be to convert the Series to a DataFrame which would then be 2D:

Y = pd.Series([1,2,3,1,2,3,4,32,2,3,42,3])scaler = StandardScaler()Ys = scaler.fit_transform(pd.DataFrame(Y))


You cannot reshape a pandas series, so you need to perform the operation on a numpy array. As others have suggested, you can use y.values.reshape(-1, 1), but if you want to impress your friends, you can use:

y.values[Ellipsis, None]

Which is equivalent to:

y.values[..., None]

It basically means all dimensions as they where, then a new dimension for the last one. Here's a fully working example:

import numpy as npimport pandas as pdfrom sklearn.preprocessing import StandardScalery = pd.Series(np.random.rand(5))
0    0.4971651    0.8186592    0.3270643    0.7725484    0.095715dtype: float64
scaler = StandardScaler()scaler.fit_transform(y.values[Ellipsis, None])
array([[-0.019],       [ 1.165],       [-0.645],       [ 0.995],       [-1.496]])