Skip to content

Commit 1037527

Browse files
committed
NotImplementedError + doc string
1 parent 0c656fd commit 1037527

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
@@ -576,23 +576,24 @@ row/columns axis labels that match a regular expression pattern.
576576

577577
.. ipython:: python
578578
579-
df = pd.DataFrame(1, index=["A", "AB", "BC"], columns=["BC", "AB", "A"])
580-
df
579+
df_re = pd.DataFrame(1, index=["A", "AB", "BC"], columns=["BC", "AB", "A"])
580+
df_re
581581
582-
df.loc(regex=True)["B", "B"]
583-
df.loc(axis=1, regex=True)["B"]
582+
df_re.loc(regex=True)["B", "B"]
583+
df_re.loc(axis=1, regex=True)["B"]
584584
585585
The regex matching will only work when looking up single strings, not list of strings etc.
586586

587587
.. ipython:: python
588588
589-
df.loc(regex=True)[["A"], "A"]
589+
df_re.loc(regex=True)[["A"], "A"]
590590
591591
*Notice*: Is is currently not possible to set values for a given regular expression.
592592

593593
.. ipython:: python
594+
:okexcept:
594595
595-
df.loc(regex=True)["B", "B"] = [[1, 2], [3, 4]]
596+
df_re.loc(regex=True)["B", "B"] = [[1, 2], [3, 4]]
596597
597598
598599
.. _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
from typing import Tuple
34
import warnings
@@ -1471,6 +1472,12 @@ class _LocIndexer(_LocationIndexer):
14711472
- A ``callable`` function with one argument (the calling Series or
14721473
DataFrame) and that returns valid output for indexing (one of the above)
14731474
1475+
``.loc`` can be called before selecting using parameters:
1476+
1477+
- ``axis``, to select by a single axis on a DataFrame, e.g. ``.loc(axis=1)['a']``.
1478+
- ``regex``, to let strings be interpreted as regex patterns, e.g.
1479+
``.loc(regex=True)[:, '^col_']``
1480+
14741481
See more at :ref:`Selection by Label <indexing.label>`
14751482
14761483
Raises
@@ -1550,6 +1557,21 @@ class _LocIndexer(_LocationIndexer):
15501557
max_speed shield
15511558
sidewinder 7 8
15521559
1560+
The axis may be preselected
1561+
1562+
>>> df.loc(axis=1)["max_speed"]
1563+
cobra 1
1564+
viper 4
1565+
sidewinder 7
1566+
Name: max_speed, dtype: int64
1567+
1568+
Single strings are considered regex patterns if ``regex=True``
1569+
1570+
>>> df.loc(regex=True)["r$", "d$"]
1571+
max_speed shield
1572+
viper 4 5
1573+
sidewinder 7 8
1574+
15531575
**Setting values**
15541576
15551577
Set value for all items matching the list of labels
@@ -1768,7 +1790,6 @@ def _get_partial_string_timestamp_match_key(self, key, labels):
17681790
return key
17691791

17701792
def _get_regex_mappings(self, key, axis=None):
1771-
import re
17721793

17731794
if axis is None:
17741795
axis = self.axis or 0
@@ -1849,7 +1870,7 @@ def _getitem_axis(self, key, axis: int):
18491870
indexer[axis] = locs
18501871
return self.obj.iloc[tuple(indexer)]
18511872

1852-
if self.regex and isinstance(key, str):
1873+
if self.regex and isinstance(key, (str, bytes)):
18531874
return self._getitem_regex(key, axis=axis)
18541875

18551876
# fall thru to straight lookup
@@ -1861,7 +1882,7 @@ def _getitem_lower_dim(self, section, key):
18611882

18621883
def __setitem__(self, key, value):
18631884
if self.regex:
1864-
raise TypeError("Inserting with regex not supported")
1885+
raise NotImplementedError("Inserting with regex has not been implemented")
18651886
return super().__setitem__(key, value)
18661887

18671888

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)