Skip to content

Commit 049b71d

Browse files
committed
ENH: add Series.isin, address GH #289
1 parent 36851c7 commit 049b71d

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pandas 0.5.1
3434
descriptive statistics (PR #313)
3535
- Add `head` and `tail` methods to Series, analogous to to DataFrame (PR
3636
#296)
37+
- Add `Series.isin` function which checks if each value is contained in a
38+
passed sequence (GH #289)
3739

3840
**Improvements to existing features**
3941

pandas/core/series.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,11 @@ def sort(self, axis=0, kind='quicksort', order=None):
12211221
ndarray. No return value
12221222
"""
12231223
sortedSeries = self.order(na_last=True)
1224+
1225+
# if not self.flags.owndata:
1226+
# raise Exception('This Series is a view of some other array, to '
1227+
# 'sort in-place you must create a copy')
1228+
12241229
self[:] = sortedSeries
12251230
self.index = sortedSeries.index
12261231

@@ -1609,6 +1614,22 @@ def fillna(self, value=None, method='pad'):
16091614
new_values = self.values.take(indexer)
16101615
return Series(new_values, index=self.index, name=self.name)
16111616

1617+
def isin(self, values):
1618+
"""
1619+
Return boolean vector showing whether each element in the Series is
1620+
exactly contained in the passed sequence of values
1621+
1622+
Parameters
1623+
----------
1624+
values : sequence
1625+
1626+
Returns
1627+
-------
1628+
isin : Series (boolean dtype)
1629+
"""
1630+
value_set = set(values)
1631+
return self.map(value_set.__contains__)
1632+
16121633
#-------------------------------------------------------------------------------
16131634
# Miscellaneous
16141635

pandas/tests/test_frame.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2510,7 +2510,7 @@ def test_select(self):
25102510
expected = self.frame.reindex(columns=['B', 'D'])
25112511
assert_frame_equal(result, expected)
25122512

2513-
def test_sort(self):
2513+
def test_sort_index(self):
25142514
frame = DataFrame(np.random.randn(4, 4), index=[1, 2, 3, 4],
25152515
columns=['A', 'B', 'C', 'D'])
25162516

@@ -2554,6 +2554,12 @@ def test_sort(self):
25542554
expected = frame.sort_index(by='A', ascending=False)
25552555
assert_frame_equal(sorted_df, expected)
25562556

2557+
# punting on trying to fix this for now
2558+
# def test_frame_column_inplace_sort_exception(self):
2559+
# s = self.frame['A']
2560+
# self.assert_(not s.flags.owndata)
2561+
# self.assertRaises(Exception, s.sort)
2562+
25572563
def test_combine_first(self):
25582564
# disjoint
25592565
head, tail = self.frame[:5], self.frame[5:]

pandas/tests/test_series.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,13 @@ def test_head_tail(self):
12481248
assert_series_equal(self.series.head(), self.series[:5])
12491249
assert_series_equal(self.series.tail(), self.series[-5:])
12501250

1251+
def test_isin(self):
1252+
s = Series(['A', 'B', 'C', 'a', 'B', 'B', 'A', 'C'])
1253+
1254+
result = s.isin(['A', 'C'])
1255+
expected = Series([True, False, True, False, False, False, True, True])
1256+
assert_series_equal(result, expected)
1257+
12511258
#-------------------------------------------------------------------------------
12521259
# TimeSeries-specific
12531260

0 commit comments

Comments
 (0)