Skip to content

Commit bfe5a7f

Browse files
cyrusmaherjreback
authored andcommitted
API: allow a filter regex to work on numeric labels, #10506
1 parent 9da54ad commit bfe5a7f

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v0.17.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Other enhancements
3535

3636
- ``.as_blocks`` will now take a ``copy`` optional argument to return a copy of the data, default is to copy (no change in behavior from prior versions), (:issue:`9607`)
3737

38+
- ``regex`` argument to ``DataFrame.filter`` now handles numeric column names instead of raising ``ValueError`` (:issue:`10384`).
39+
3840
.. _whatsnew_0170.api:
3941

4042
Backwards incompatible API changes

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ def filter(self, items=None, like=None, regex=None, axis=None):
19281928
return self.select(matchf, axis=axis_name)
19291929
elif regex:
19301930
matcher = re.compile(regex)
1931-
return self.select(lambda x: matcher.search(x) is not None,
1931+
return self.select(lambda x: matcher.search(str(x)) is not None,
19321932
axis=axis_name)
19331933
else:
19341934
raise TypeError('Must pass either `items`, `like`, or `regex`')

pandas/tests/test_frame.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -10781,7 +10781,7 @@ def test_filter(self):
1078110781
idx = self.frame.index[0:4]
1078210782
filtered = self.frame.filter(idx, axis='index')
1078310783
expected = self.frame.reindex(index=idx)
10784-
assert_frame_equal(filtered,expected)
10784+
assert_frame_equal(filtered, expected)
1078510785

1078610786
# like
1078710787
fcopy = self.frame.copy()
@@ -10796,6 +10796,17 @@ def test_filter(self):
1079610796
filtered = df.filter(like='_')
1079710797
self.assertEqual(len(filtered.columns), 2)
1079810798

10799+
# regex with ints in column names
10800+
# from PR #10384
10801+
df = DataFrame(0., index=[0, 1, 2], columns=['A1', 1, 'B', 2, 'C'])
10802+
expected = DataFrame(0., index=[0, 1, 2], columns=[1, 2])
10803+
filtered = df.filter(regex='^[0-9]+$')
10804+
self.assert_frame_equal(filtered, expected)
10805+
10806+
expected = DataFrame(0., index=[0, 1, 2], columns=[0, '0', 1, '1'])
10807+
filtered = expected.filter(regex='^[0-9]+$') # shouldn't remove anything
10808+
self.assert_frame_equal(filtered, expected)
10809+
1079910810
# pass in None
1080010811
with assertRaisesRegexp(TypeError, 'Must pass'):
1080110812
self.frame.filter(items=None)

0 commit comments

Comments
 (0)