Skip to content

Commit 51d2c73

Browse files
committed
NotImplementedError + doc string
1 parent beb9d67 commit 51d2c73

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

doc/source/user_guide/indexing.rst

+7-6
Original file line numberDiff line numberDiff line change
@@ -586,23 +586,24 @@ row/columns axis labels that match a regular expression pattern.
586586

587587
.. ipython:: python
588588
589-
df = pd.DataFrame(1, index=["A", "AB", "BC"], columns=["BC", "AB", "A"])
590-
df
589+
df_re = pd.DataFrame(1, index=["A", "AB", "BC"], columns=["BC", "AB", "A"])
590+
df_re
591591
592-
df.loc(regex=True)["B", "B"]
593-
df.loc(axis=1, regex=True)["B"]
592+
df_re.loc(regex=True)["B", "B"]
593+
df_re.loc(axis=1, regex=True)["B"]
594594
595595
The regex matching will only work when looking up single strings, not list of strings etc.
596596

597597
.. ipython:: python
598598
599-
df.loc(regex=True)[["A"], "A"]
599+
df_re.loc(regex=True)[["A"], "A"]
600600
601601
*Notice*: Is is currently not possible to set values for a given regular expression.
602602

603603
.. ipython:: python
604+
:okexcept:
604605
605-
df.loc(regex=True)["B", "B"] = [[1, 2], [3, 4]]
606+
df_re.loc(regex=True)["B", "B"] = [[1, 2], [3, 4]]
606607
607608
608609
.. _indexing.deprecate_ix:

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ The regex matching will only work when looking up single strings, not list of st
207207
Is is currently not possible to set values for a given regular expression.
208208

209209
.. ipython:: python
210+
:okexcept:
210211
211212
df.loc(regex=True)["B", "B"] = [[1, 2], [3, 4]]
212213

pandas/core/indexing.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import textwrap
23
import warnings
34

@@ -1474,6 +1475,12 @@ class _LocIndexer(_LocationIndexer):
14741475
- A ``callable`` function with one argument (the calling Series or
14751476
DataFrame) and that returns valid output for indexing (one of the above)
14761477
1478+
``.loc`` can be called before selecting using parameters:
1479+
1480+
- ``axis``, to select by a single axis on a DataFrame, e.g. ``.loc(axis=1)['a']``.
1481+
- ``regex``, to let strings be interpreted as regex patterns, e.g.
1482+
``.loc(regex=True)[:, '^col_']``
1483+
14771484
See more at :ref:`Selection by Label <indexing.label>`
14781485
14791486
Raises
@@ -1553,6 +1560,21 @@ class _LocIndexer(_LocationIndexer):
15531560
max_speed shield
15541561
sidewinder 7 8
15551562
1563+
The axis may be preselected
1564+
1565+
>>> df.loc(axis=1)["max_speed"]
1566+
cobra 1
1567+
viper 4
1568+
sidewinder 7
1569+
Name: max_speed, dtype: int64
1570+
1571+
Single strings are considered regex patterns if ``regex=True``
1572+
1573+
>>> df.loc(regex=True)["r$", "d$"]
1574+
max_speed shield
1575+
viper 4 5
1576+
sidewinder 7 8
1577+
15561578
**Setting values**
15571579
15581580
Set value for all items matching the list of labels
@@ -1774,7 +1796,6 @@ def _get_partial_string_timestamp_match_key(self, key, labels):
17741796
return key
17751797

17761798
def _get_regex_mappings(self, key, axis=None):
1777-
import re
17781799

17791800
if axis is None:
17801801
axis = self.axis or 0
@@ -1855,7 +1876,7 @@ def _getitem_axis(self, key, axis: int):
18551876
indexer[axis] = locs
18561877
return self.obj.iloc[tuple(indexer)]
18571878

1858-
if self.regex and isinstance(key, str):
1879+
if self.regex and isinstance(key, (str, bytes)):
18591880
return self._getitem_regex(key, axis=axis)
18601881

18611882
# fall thru to straight lookup
@@ -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)