Skip to content

Commit ce0efe8

Browse files
authored
BUG: Series.at returning Series with one element instead of scalar (#38101)
1 parent 65319af commit ce0efe8

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ Indexing
631631
- Bug in :meth:`MultiIndex.drop` does not raise if labels are partially found (:issue:`37820`)
632632
- Bug in :meth:`DataFrame.loc` did not raise ``KeyError`` when missing combination was given with ``slice(None)`` for remaining levels (:issue:`19556`)
633633
- Bug in :meth:`DataFrame.loc` raising ``TypeError`` when non-integer slice was given to select values from :class:`MultiIndex` (:issue:`25165`, :issue:`24263`)
634+
- Bug in :meth:`Series.at` returning :class:`Series` with one element instead of scalar when index is a :class:`MultiIndex` with one level (:issue:`38053`)
634635
- Bug in :meth:`DataFrame.loc` returning and assigning elements in wrong order when indexer is differently ordered than the :class:`MultiIndex` to filter (:issue:`31330`, :issue:`34603`)
635636
- Bug in :meth:`DataFrame.loc` and :meth:`DataFrame.__getitem__` raising ``KeyError`` when columns were :class:`MultiIndex` with only one level (:issue:`29749`)
636637
- Bug in :meth:`Series.__getitem__` and :meth:`DataFrame.__getitem__` raising blank ``KeyError`` without missing keys for :class:`IntervalIndex` (:issue:`27365`)

pandas/core/indexes/multi.py

+4
Original file line numberDiff line numberDiff line change
@@ -2530,6 +2530,10 @@ def _get_values_for_loc(self, series: "Series", loc, key):
25302530
if is_scalar(loc):
25312531
return new_values
25322532

2533+
if len(new_values) == 1 and not self.nlevels > 1:
2534+
# If more than one level left, we can not return a scalar
2535+
return new_values[0]
2536+
25332537
new_index = self[loc]
25342538
new_index = maybe_droplevels(new_index, key)
25352539
new_ser = series._constructor(new_values, index=new_index, name=series.name)

pandas/tests/indexing/test_scalar.py

+38-32
Original file line numberDiff line numberDiff line change
@@ -268,35 +268,41 @@ def test_at_with_tuple_index_set():
268268
assert series.at[1, 2] == 3
269269

270270

271-
def test_multiindex_at_get():
272-
# GH 26989
273-
# DataFrame.at and DataFrame.loc getter works with MultiIndex
274-
df = DataFrame({"a": [1, 2]}, index=[[1, 2], [3, 4]])
275-
assert df.index.nlevels == 2
276-
assert df.at[(1, 3), "a"] == 1
277-
assert df.loc[(1, 3), "a"] == 1
278-
279-
# Series.at and Series.loc getter works with MultiIndex
280-
series = df["a"]
281-
assert series.index.nlevels == 2
282-
assert series.at[1, 3] == 1
283-
assert series.loc[1, 3] == 1
284-
285-
286-
def test_multiindex_at_set():
287-
# GH 26989
288-
# DataFrame.at and DataFrame.loc setter works with MultiIndex
289-
df = DataFrame({"a": [1, 2]}, index=[[1, 2], [3, 4]])
290-
assert df.index.nlevels == 2
291-
df.at[(1, 3), "a"] = 3
292-
assert df.at[(1, 3), "a"] == 3
293-
df.loc[(1, 3), "a"] = 4
294-
assert df.loc[(1, 3), "a"] == 4
295-
296-
# Series.at and Series.loc setter works with MultiIndex
297-
series = df["a"]
298-
assert series.index.nlevels == 2
299-
series.at[1, 3] = 5
300-
assert series.at[1, 3] == 5
301-
series.loc[1, 3] = 6
302-
assert series.loc[1, 3] == 6
271+
class TestMultiIndexScalar:
272+
def test_multiindex_at_get(self):
273+
# GH 26989
274+
# DataFrame.at and DataFrame.loc getter works with MultiIndex
275+
df = DataFrame({"a": [1, 2]}, index=[[1, 2], [3, 4]])
276+
assert df.index.nlevels == 2
277+
assert df.at[(1, 3), "a"] == 1
278+
assert df.loc[(1, 3), "a"] == 1
279+
280+
# Series.at and Series.loc getter works with MultiIndex
281+
series = df["a"]
282+
assert series.index.nlevels == 2
283+
assert series.at[1, 3] == 1
284+
assert series.loc[1, 3] == 1
285+
286+
def test_multiindex_at_set(self):
287+
# GH 26989
288+
# DataFrame.at and DataFrame.loc setter works with MultiIndex
289+
df = DataFrame({"a": [1, 2]}, index=[[1, 2], [3, 4]])
290+
assert df.index.nlevels == 2
291+
df.at[(1, 3), "a"] = 3
292+
assert df.at[(1, 3), "a"] == 3
293+
df.loc[(1, 3), "a"] = 4
294+
assert df.loc[(1, 3), "a"] == 4
295+
296+
# Series.at and Series.loc setter works with MultiIndex
297+
series = df["a"]
298+
assert series.index.nlevels == 2
299+
series.at[1, 3] = 5
300+
assert series.at[1, 3] == 5
301+
series.loc[1, 3] = 6
302+
assert series.loc[1, 3] == 6
303+
304+
def test_multiindex_at_get_one_level(self):
305+
# GH#38053
306+
s2 = Series((0, 1), index=[[False, True]])
307+
result = s2.at[False]
308+
assert result == 0

0 commit comments

Comments
 (0)