Skip to content

Commit d80894c

Browse files
committed
Merge pull request #5249 from jreback/drop_dup
BUG: fixed issue with drop on a non-unique index with Series (GH5248)
2 parents 300a895 + ea4a09e commit d80894c

File tree

6 files changed

+38
-33
lines changed

6 files changed

+38
-33
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ Bug Fixes
621621
non-business date. (:issue:`5203`)
622622
- Fixed bug in Excel writers where frames with duplicate column names weren't
623623
written correctly. (:issue:`5235`)
624+
- Fixed issue with ``drop`` and a non-unique index on Series (:issue:`5248`)
624625

625626
pandas 0.12.0
626627
-------------

pandas/core/frame.py

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,9 @@ def to_dict(self, outtype='dict'):
672672
raise ValueError("outtype %s not understood" % outtype)
673673

674674
def to_gbq(self, destination_table, schema=None, col_order=None, if_exists='fail', **kwargs):
675-
"""Write a DataFrame to a Google BigQuery table.
676-
677-
If the table exists, the DataFrame will be appended. If not, a new table
675+
"""Write a DataFrame to a Google BigQuery table.
676+
677+
If the table exists, the DataFrame will be appended. If not, a new table
678678
will be created, in which case the schema will have to be specified. By default,
679679
rows will be written in the order they appear in the DataFrame, though
680680
the user may specify an alternative order.
@@ -2233,33 +2233,6 @@ def rename(self, index=None, columns=None, **kwargs):
22332233
return super(DataFrame, self).rename(index=index, columns=columns,
22342234
**kwargs)
22352235

2236-
def reindex_like(self, other, method=None, copy=True, limit=None,
2237-
fill_value=NA):
2238-
"""
2239-
Reindex DataFrame to match indices of another DataFrame, optionally
2240-
with filling logic
2241-
2242-
Parameters
2243-
----------
2244-
other : DataFrame
2245-
method : string or None
2246-
copy : boolean, default True
2247-
limit : int, default None
2248-
Maximum size gap to forward or backward fill
2249-
2250-
Notes
2251-
-----
2252-
Like calling s.reindex(index=other.index, columns=other.columns,
2253-
method=...)
2254-
2255-
Returns
2256-
-------
2257-
reindexed : DataFrame
2258-
"""
2259-
return self.reindex(index=other.index, columns=other.columns,
2260-
method=method, copy=copy, limit=limit,
2261-
fill_value=fill_value)
2262-
22632236
def set_index(self, keys, drop=True, append=False, inplace=False,
22642237
verify_integrity=False):
22652238
"""

pandas/core/generic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,8 @@ def drop(self, labels, axis=0, level=None):
11031103
"""
11041104
axis_name = self._get_axis_name(axis)
11051105
axis, axis_ = self._get_axis(axis), axis
1106+
if not is_list_like(labels):
1107+
labels = [ labels ]
11061108

11071109
if axis.is_unique:
11081110
if level is not None:

pandas/core/indexing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,11 @@ def _multi_take_opportunity(self, tup):
581581
return False
582582

583583
# just too complicated
584-
for ax in self.obj._data.axes:
584+
for indexer, ax in zip(tup,self.obj._data.axes):
585585
if isinstance(ax, MultiIndex):
586586
return False
587+
elif com._is_bool_indexer(indexer):
588+
return False
587589

588590
return True
589591

pandas/tests/test_frame.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3175,9 +3175,11 @@ def check(result, expected=None):
31753175

31763176
# drop
31773177
df = DataFrame([[1,5,7.],[1,5,7.],[1,5,7.]],columns=['bar','a','a'])
3178-
df = df.drop(['a'],axis=1)
3178+
result = df.drop(['a'],axis=1)
31793179
expected = DataFrame([[1],[1],[1]],columns=['bar'])
3180-
check(df,expected)
3180+
check(result,expected)
3181+
result = df.drop('a',axis=1)
3182+
check(result,expected)
31813183

31823184
# describe
31833185
df = DataFrame([[1,1,1],[2,2,2],[3,3,3]],columns=['bar','a','a'],dtype='float64')

pandas/tests/test_series.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,31 @@ def test_mask(self):
14151415
rs = s.where(cond, np.nan)
14161416
assert_series_equal(rs, s.mask(~cond))
14171417

1418+
def test_drop(self):
1419+
1420+
# unique
1421+
s = Series([1,2],index=['one','two'])
1422+
expected = Series([1],index=['one'])
1423+
result = s.drop(['two'])
1424+
assert_series_equal(result,expected)
1425+
result = s.drop('two')
1426+
assert_series_equal(result,expected)
1427+
1428+
# non-unique
1429+
# GH 5248
1430+
s = Series([1,1,2],index=['one','two','one'])
1431+
expected = Series([1,2],index=['one','one'])
1432+
result = s.drop(['two'])
1433+
assert_series_equal(result,expected)
1434+
result = s.drop('two')
1435+
assert_series_equal(result,expected)
1436+
1437+
expected = Series([1],index=['two'])
1438+
result = s.drop(['one'])
1439+
assert_series_equal(result,expected)
1440+
result = s.drop('one')
1441+
assert_series_equal(result,expected)
1442+
14181443
def test_ix_setitem(self):
14191444
inds = self.series.index[[3, 4, 7]]
14201445

0 commit comments

Comments
 (0)