Pandas selecting by label sometimes return series, sometimes returns dataframe

In Pandas, when I select a label that only has one entry in the index I get back a Series, but when I select an entry that has more then one entry I get back a data frame.

Why is that? Is there a way to ensure I always get back a data frame?

    In [1]: import pandas as pd

    In [2]: df = pd.DataFrame(data=range(5), index=[1, 2, 3, 3, 3])

    In [3]: type(df.loc[3])
    Out[3]: pandas.core.frame.DataFrame

    In [4]: type(df.loc[1])
    Out[4]: pandas.core.series.Series

Granted that the behavior is inconsistent, but I think it's easy to imagine cases where this is convenient. Anyway, to get a DataFrame every time, just pass a list to loc. There are other ways, but in my opinion this is the cleanest.

    In [2]: type(df.loc[[3]])
    Out[2]: pandas.core.frame.DataFrame

    In [3]: type(df.loc[[1]])
    Out[3]: pandas.core.frame.DataFrame


Back to homepage or read more recommendations: