Skip to content

BUG: fixed issue with drop on a non-unique index with Series (GH5248) #5249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ Bug Fixes
non-business date. (:issue:`5203`)
- Fixed bug in Excel writers where frames with duplicate column names weren't
written correctly. (:issue:`5235`)
- Fixed issue with ``drop`` and a non-unique index on Series (:issue:`5248`)

pandas 0.12.0
-------------
Expand Down
33 changes: 3 additions & 30 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,9 +672,9 @@ def to_dict(self, outtype='dict'):
raise ValueError("outtype %s not understood" % outtype)

def to_gbq(self, destination_table, schema=None, col_order=None, if_exists='fail', **kwargs):
"""Write a DataFrame to a Google BigQuery table.
If the table exists, the DataFrame will be appended. If not, a new table
"""Write a DataFrame to a Google BigQuery table.

If the table exists, the DataFrame will be appended. If not, a new table
will be created, in which case the schema will have to be specified. By default,
rows will be written in the order they appear in the DataFrame, though
the user may specify an alternative order.
Expand Down Expand Up @@ -2233,33 +2233,6 @@ def rename(self, index=None, columns=None, **kwargs):
return super(DataFrame, self).rename(index=index, columns=columns,
**kwargs)

def reindex_like(self, other, method=None, copy=True, limit=None,
fill_value=NA):
"""
Reindex DataFrame to match indices of another DataFrame, optionally
with filling logic

Parameters
----------
other : DataFrame
method : string or None
copy : boolean, default True
limit : int, default None
Maximum size gap to forward or backward fill

Notes
-----
Like calling s.reindex(index=other.index, columns=other.columns,
method=...)

Returns
-------
reindexed : DataFrame
"""
return self.reindex(index=other.index, columns=other.columns,
method=method, copy=copy, limit=limit,
fill_value=fill_value)

def set_index(self, keys, drop=True, append=False, inplace=False,
verify_integrity=False):
"""
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,8 @@ def drop(self, labels, axis=0, level=None):
"""
axis_name = self._get_axis_name(axis)
axis, axis_ = self._get_axis(axis), axis
if not is_list_like(labels):
labels = [ labels ]

if axis.is_unique:
if level is not None:
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,11 @@ def _multi_take_opportunity(self, tup):
return False

# just too complicated
for ax in self.obj._data.axes:
for indexer, ax in zip(tup,self.obj._data.axes):
if isinstance(ax, MultiIndex):
return False
elif com._is_bool_indexer(indexer):
return False

return True

Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3175,9 +3175,11 @@ def check(result, expected=None):

# drop
df = DataFrame([[1,5,7.],[1,5,7.],[1,5,7.]],columns=['bar','a','a'])
df = df.drop(['a'],axis=1)
result = df.drop(['a'],axis=1)
expected = DataFrame([[1],[1],[1]],columns=['bar'])
check(df,expected)
check(result,expected)
result = df.drop('a',axis=1)
check(result,expected)

# describe
df = DataFrame([[1,1,1],[2,2,2],[3,3,3]],columns=['bar','a','a'],dtype='float64')
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,31 @@ def test_mask(self):
rs = s.where(cond, np.nan)
assert_series_equal(rs, s.mask(~cond))

def test_drop(self):

# unique
s = Series([1,2],index=['one','two'])
expected = Series([1],index=['one'])
result = s.drop(['two'])
assert_series_equal(result,expected)
result = s.drop('two')
assert_series_equal(result,expected)

# non-unique
# GH 5248
s = Series([1,1,2],index=['one','two','one'])
expected = Series([1,2],index=['one','one'])
result = s.drop(['two'])
assert_series_equal(result,expected)
result = s.drop('two')
assert_series_equal(result,expected)

expected = Series([1],index=['two'])
result = s.drop(['one'])
assert_series_equal(result,expected)
result = s.drop('one')
assert_series_equal(result,expected)

def test_ix_setitem(self):
inds = self.series.index[[3, 4, 7]]

Expand Down