Confusing API for constructing Series from DataFrame #20658
Labels
Constructors
Series/DataFrame/Index/pd.array Constructors
Enhancement
Error Reporting
Incorrect or improved errors from pandas
The current API for constructing a Series from a DataFrame containing just a single column currently is quite confusing in my opinion. The following illustrates my experience trying to transform a Dataframe containing just a single column into a Series object. This may or may not be exemplary for other users.
to_frame
(why notto_dataframe
?) method for Series, so I guess there's also ato_series
method for dataframes...? [There isn't.]pd.Series(df)
. From a user perspective, the only sensible thing that this could possibly do is return the Series corresponding to the (single) column. I'm not exactly sure what it does, but it definitely doesn't do what I expected it to do and the result doesn't look very useful in any case:squeeze
appears to be the almost-inverse of theto_frame
method, except that it returns anint
instead of aSeries
in the 1x1 case:df.iloc[:, 0]
.Specific questions:
to_frame
and notto_dataframe
? Is there any reason not to introduce the latter as an alias to the former?to_series
method on dataframes that raises an exception if there's more than one column and otherwise works likesqueeze
except that it always returns aSeries
?Series
constructor do what it does, and can't this be changed so that it does something useful for this use case?(I'm not sure if this has been discussed before; I couldn't find any related issue. Sorry if this is a duplicate post!)
On a sidenote, this issue is quite representative of my experiences with pandas so far: it is almost always capable of doing what I'd like it to do, but it often takes me longer than expected to get things to work due to (for me, at least) unintuitive and partly inconsistent API design. That being said, it's still immensely useful and I use it on an almost daily basis. Thanks a lot to everyone involved!
The text was updated successfully, but these errors were encountered: