Skip to content

Commit fc5fe33

Browse files
committed
pandas-dev#48188 FIX: iloc works with namedtuple
- unit test for added for test_iloc.py that verifies that all four uses of iloc given in the reproducible example now works
1 parent 31dc138 commit fc5fe33

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

pandas/core/indexing.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,9 @@ def _getitem_axis(self, key, axis: AxisInt):
17381738
self._validate_key(key, axis)
17391739
return self._getbool_axis(key, axis=axis)
17401740

1741+
if type(key) is tuple or (hasattr(key, "_fields") and isinstance(key, type(key))):
1742+
return self.__getitem__(tuple(key))
1743+
17411744
# a list of integers
17421745
elif is_list_like_indexer(key):
17431746
return self._get_list_axis(key, axis=axis)

pandas/tests/indexing/test_iloc.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,3 +1449,20 @@ def test_iloc_nullable_int64_size_1_nan(self):
14491449
result.loc[:, "b"] = result.loc[:, "b"].astype("Int64")
14501450
expected = DataFrame({"a": ["test"], "b": array([NA], dtype="Int64")})
14511451
tm.assert_frame_equal(result, expected)
1452+
1453+
def test_iloc_with_namedtuple(self):
1454+
df = DataFrame(np.arange(120).reshape(6, -1))
1455+
1456+
output1 = df.iloc[1, 2]
1457+
output2 = df.iloc[(1, 2)]
1458+
1459+
from collections import namedtuple
1460+
1461+
indexer_tuple = namedtuple("Indexer", ["x", "y"])
1462+
1463+
output3 = df.iloc[tuple(indexer_tuple(x=1, y=2))]
1464+
output4 = df.iloc[indexer_tuple(x=1, y=2)]
1465+
tm.assert_equal(output1, output2)
1466+
tm.assert_equal(output3, output4)
1467+
tm.assert_equal(output1, output3)
1468+

0 commit comments

Comments
 (0)