Code Sample, a copy-pastable example if possible
def gen():
yield '1','2',1,2
yield '1','2',1,2
yield '1','3',3,2
yield '2','4',1,2
ser = pd.DataFrame(gen(), columns=["a", "b", "c", "d",]).set_index(["a", "b"])["c"]
print ser
print ser.loc[('1','3')]
print ser.at[('1','3')] # TypeError: _get_value() got multiple values for keyword argument 'takeable'
Problem description
The .at
accessor should return the indexed value 3
.
There doesn't seem any way reliably look up a single scalar in a multiindex. If this is not supported, a better error would be appreciated.
Expected Output
3
Output of pd.show_versions()
Comment From: allComputableThings
Seems related to: https://github.com/pandas-dev/pandas/issues/9259
Comment From: allComputableThings
An expanded example with even more weirdness.
It seems that, if I want to assign a new row, or lookup an existing row in a series or dataframe, I need to choose different accessors depending on whether the multindex label is defined for all indexes.
def gen():
yield 's',None,'sval'
ser = pd.DataFrame(gen(), columns=["a", "b", "c", ]).set_index(["a", "b"])["c"]
print ser
# Writing ---
#ser[('s1',)] = "z" # AttributeError: 'float' object has no attribute 'axes'
ser.at[('s2',)] = "z" # success
#ser.loc[('s3',)] = "z" # AttributeError: 'float' object has no attribute 'axes'
ser[('s4','x')] = "z" # success
#ser.at[('s5','x')] = "z" # ValueError: Not enough indexers for scalar access (setting)!
ser.loc[('s6','x')] = "z" # success
# Reading ---
print type(ser.loc[('s6','x')]) # Success => scalar
#print type(ser.at[('s6','x')]) # _get_value() got multiple values for keyword argument 'takeable'
print type(ser[('s6','x')]) # Success => scalar
print type(ser.loc[('s2',)]) # Success => series
#print type(ser.at[('s2',)]) # AttributeError: 'numpy.ndarray' object has no attribute '_values'
print type(ser[('s2',)]) # Success => series
Comment From: jreback
this is a duplicate of #9259
.iat/.at
are not too multi-index aware currently.
Comment From: allComputableThings
I guess if you leave it open, someone could work on it. Unless, there’s a good reason to want this feature
On Mon, Feb 26, 2018 at 5:04 PM Jeff Reback notifications@github.com wrote:
this is a duplicate of #9259 https://github.com/pandas-dev/pandas/issues/9259
.iat/.at are not too multi-index aware currently.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pandas-dev/pandas/issues/19916#issuecomment-368709139, or mute the thread https://github.com/notifications/unsubscribe-auth/AHcErMKNd8wHAv0PlK415kc3XaZIoanvks5tY1S2gaJpZM4ST06R .