1
+ import re
1
2
import textwrap
2
3
import warnings
3
4
@@ -1474,6 +1475,12 @@ class _LocIndexer(_LocationIndexer):
1474
1475
- A ``callable`` function with one argument (the calling Series or
1475
1476
DataFrame) and that returns valid output for indexing (one of the above)
1476
1477
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
+
1477
1484
See more at :ref:`Selection by Label <indexing.label>`
1478
1485
1479
1486
Raises
@@ -1553,6 +1560,21 @@ class _LocIndexer(_LocationIndexer):
1553
1560
max_speed shield
1554
1561
sidewinder 7 8
1555
1562
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
+
1556
1578
**Setting values**
1557
1579
1558
1580
Set value for all items matching the list of labels
@@ -1774,7 +1796,6 @@ def _get_partial_string_timestamp_match_key(self, key, labels):
1774
1796
return key
1775
1797
1776
1798
def _get_regex_mappings (self , key , axis = None ):
1777
- import re
1778
1799
1779
1800
if axis is None :
1780
1801
axis = self .axis or 0
@@ -1855,7 +1876,7 @@ def _getitem_axis(self, key, axis: int):
1855
1876
indexer [axis ] = locs
1856
1877
return self .obj .iloc [tuple (indexer )]
1857
1878
1858
- if self .regex and isinstance (key , str ):
1879
+ if self .regex and isinstance (key , ( str , bytes ) ):
1859
1880
return self ._getitem_regex (key , axis = axis )
1860
1881
1861
1882
# fall thru to straight lookup
@@ -1867,7 +1888,7 @@ def _getitem_lower_dim(self, section, key):
1867
1888
1868
1889
def __setitem__ (self , key , value ):
1869
1890
if self .regex :
1870
- raise TypeError ("Inserting with regex not supported " )
1891
+ raise NotImplementedError ("Inserting with regex has not been implemented " )
1871
1892
return super ().__setitem__ (key , value )
1872
1893
1873
1894
0 commit comments