Skip to content

Commit a12aa31

Browse files
committed
NotImplementedError + doc string
1 parent beb9d67 commit a12aa31

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

pandas/core/indexing.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,12 @@ class _LocIndexer(_LocationIndexer):
14741474
- A ``callable`` function with one argument (the calling Series or
14751475
DataFrame) and that returns valid output for indexing (one of the above)
14761476
1477+
``.loc`` can be called before selecting using parameters:
1478+
1479+
- ``axis``, to select by a single axis on a DataFrame, e.g. ``.loc(axis=1)['a']``.
1480+
- ``regex``, to let strings be interpreted as regex patterns, e.g.
1481+
``.loc(regex=True)[:, '^col_']``
1482+
14771483
See more at :ref:`Selection by Label <indexing.label>`
14781484
14791485
Raises
@@ -1553,6 +1559,21 @@ class _LocIndexer(_LocationIndexer):
15531559
max_speed shield
15541560
sidewinder 7 8
15551561
1562+
The axis may be preselected
1563+
1564+
>>> df.loc(axis=1)["max_speed"]
1565+
cobra 1
1566+
viper 4
1567+
sidewinder 7
1568+
Name: max_speed, dtype: int64
1569+
1570+
Single strings are considered regex patterns if ``regex=True``
1571+
1572+
>>> df.loc(regex=True)["r$", "d$"]
1573+
max_speed shield
1574+
viper 4 5
1575+
sidewinder 7 8
1576+
15561577
**Setting values**
15571578
15581579
Set value for all items matching the list of labels
@@ -1867,7 +1888,7 @@ def _getitem_lower_dim(self, section, key):
18671888

18681889
def __setitem__(self, key, value):
18691890
if self.regex:
1870-
raise TypeError("Inserting with regex not supported")
1891+
raise NotImplementedError("Inserting with regex has not been implemented")
18711892
return super().__setitem__(key, value)
18721893

18731894

pandas/tests/indexing/test_loc.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def test_regex_inserting(self):
6969

7070
df = pd.DataFrame(1, index=idx, columns=cols)
7171

72-
with pytest.raises(TypeError, match="Inserting with regex not supported"):
72+
msg = "Inserting with regex has not been implemented"
73+
with pytest.raises(NotImplementedError, match=msg):
7374
df.loc(regex=True)["B", "B"] = [[2, 2], [2, 2]]
7475

7576

0 commit comments

Comments
 (0)