1

What are the rules of using pandas.Series.at? How is it different from pandas.Series.loc with a single label as input?

More specifically, I am trying to understand why the following example does not work with at:

import pandas as pd
d = pd.Series(index=pd.MultiIndex.from_arrays([(0,0),('a','b')]), data=777).sort_index()
print(d)
# 0  a    777
#    b    777
# dtype: int64
d    [d.index[0]] # works
d.loc[d.index[0]] # works
d.at [d.index[0]] # ValueError: At based indexing on an non-integer index can only have non-integer indexers

Thank you very much for your help!

S.V
  • 2,149
  • 2
  • 18
  • 41
  • I think the documentation is clear enough, isnt it? - Similar to loc, in that both provide label-based lookups. Use at if you only need to get or set a single value in a DataFrame or Series. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.at.html – Anshul Jun 02 '20 at 16:57
  • 1
    That is exactly what I am trying to do in my example: to get a single value in a `Series` using `at`. So, the documentation is anything but clear. – S.V Jun 02 '20 at 17:10
  • I guess you can't use `at` on a `Series` when it has a multiindex? Interestingly, following [the example in this issue](https://stackoverflow.com/questions/49800835/df-at-with-multiindex-vs-df-loc), I can call `at` on a multiindex DataFrame (`df.at[('foo','bar','c'),'value1']` works for me but apparently not in every version) – Tom Jun 02 '20 at 18:42
  • 1
    but a workaround would be to use `iat`: say if you wanted to look up a multiindex value but didn't know the index position, you could call (in your case) `d.iat[d.index.get_loc((0,'a'))]`, which returns `777` – Tom Jun 02 '20 at 18:43
  • 2
    interesting enough `d.to_frame().at[d.index[0],0]` works (a series without name gets converted to a dataframe column with the name `0`) – Stef Jun 02 '20 at 18:50

0 Answers0