Repository: pandas-dev/pandas

Issue:
Dropping rows inplace, weird bug with 0.13.0rc1
In c239b06e I have the following weird behavior. I am even unsure how to describe it, but somehow the dropping of rows selected via a boolean leads to a failure of dropping the rows only for the Series of a DataFrame that met the condition in the first place.
Please have a look at the MWE I figured out after some hacking and trying out:

http://nbviewer.ipython.org/gist/michaelaye/7744592

Please tell me I'm not being dumb again?



Discussion:
Definitely a bug (I think it's a caching issue)

I'm going backwards to see where it came in, already at 0.12.0-1140-ge82c1f7 and it's still there.

Unfortunately, you're going to have to rebuild at various points, but if you don't already have this, simpler to do this to check:

``` python
import pandas as pd
df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [5, 6, 7, 8]})
df.drop(labels=df[df.B < 7].index, inplace=True)
assert len(df['B']) == len(df.index)
```

and then you can run that with git bisect.

Cool, didn't know about git bisect. Will do.

you know, not really worth it for you to run. `inplace` was only added for 0.13. I'm pretty sure there's some quirk with inplace that is being missed here.

Well, I need to find where it's working, but don't want to go back to v0.12.0 because there were some improvements regarding HDF files that I would be missing.

@michaelaye the issue is doing it inplace.  It's really just a convenience (i.e. still copies under the hood). If you do it not inplace this should work fine.

So, for example:

``` python
df.drop(labels=df[df.B < 0], inplace=True)
```

is equivalent to:

``` python
df = df.drop(labels=df[df.B < 0])
```

ah, that's true. Thanks.

isn't df[~(df.B<0)] what u r looking for anyhow?

@jreback just to be clear, here's the actual error:

``` python
In [13]: df.drop(labels=df[df.B < 7].index, inplace=True)

In [14]: df
Out[14]:
   A  B
2  3  7
3  4  8

[2 rows x 2 columns]

In [15]: df['B']
Out[15]:
0    5
1    6
2    7
3    8
Name: B, dtype: int64
```

It's just something wrong with `_update_inplace`

not clearing the cache
I'll fix tom

It was an interesting learning experience to use bisect to confirm that the error simply exists since 4e0d29a3f when Jeff added the inplace option to drop and dropna.


Resolving patch:
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -1217,6 +1217,7 @@ def _update_inplace(self, result):
         # NOTE: This does *not* call __finalize__ and that's an explicit
         # decision that we may revisit in the future.
         self._reset_cache()
+        self._clear_item_cache()
         self._data = result._data
         self._maybe_update_cacher()
 


Test patch:
diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py
--- a/pandas/tests/test_frame.py
+++ b/pandas/tests/test_frame.py
@@ -6806,6 +6806,13 @@ def test_drop(self):
         assert_frame_equal(nu_df.drop('X', axis='rows'), nu_df.ix[["Y"], :])
         assert_frame_equal(nu_df.drop(['X', 'Y'], axis=0), nu_df.ix[[], :])
 
+        # inplace cache issue
+        # GH 5628
+        df = pd.DataFrame(np.random.randn(10,3), columns=list('abc'))
+        expected = df[~(df.b>0)]
+        df.drop(labels=df[df.b>0].index, inplace=True)
+        assert_frame_equal(df,expected)
+
     def test_fillna(self):
         self.tsframe['A'][:5] = nan
         self.tsframe['A'][-5:] = nan