-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
BUG: dropna changes index even when nothing dropped #43618
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
Changes from all commits
4ad5f13 d7cfef2 39a2f5c 2e11c4d ec9344f ce90c65 939c0d8 7d511e1 5cbe1f0 4ef2825 e875e74 aa5320c 2cff764 1a1cabb afd20b8 8a0089f 3bf4a82 df074e4 2c42b44 81b9c14 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -16,7 +16,10 @@ | |
| import numpy as np | ||
| | ||
| from pandas._libs import index as libindex | ||
| from pandas._libs.lib import no_default | ||
| from pandas._libs.lib import ( | ||
| maybe_indices_to_slice, | ||
| no_default, | ||
| ) | ||
| from pandas._typing import ( | ||
| Dtype, | ||
| npt, | ||
| | @@ -433,8 +436,19 @@ def delete(self, loc) -> Int64Index: # type: ignore[override] | |
| | ||
| def take( | ||
| self, indices, axis: int = 0, allow_fill: bool = True, fill_value=None, **kwargs | ||
| ) -> Int64Index: | ||
| ) -> Int64Index | RangeIndex: | ||
| with rewrite_exception("Int64Index", type(self).__name__): | ||
| if ( | ||
| fill_value is None | ||
| and isinstance(indices, np.ndarray) | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is way too complicated and likely hiding bugs. @jbrockmendel can you suggest anything here. Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @phofl Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. take a look at what we do in | ||
| and indices.dtype == int | ||
| ): | ||
| try: | ||
| slc = maybe_indices_to_slice(indices, len(indices)) | ||
| return self[slc] | ||
| except ValueError: | ||
| # Buffer dtype mismatch, expected 'intp_t' but got something else | ||
| pass | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when does this raise (and you are catching)? Contributor Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was raising in cases where I was not getting a slice (either because indices was Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you just do something like to avoid the try/except Contributor Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tried to do that.. but there can be a lot of possibilities of the contents of | ||
| return self._int64index.take( | ||
| indices, | ||
| axis=axis, | ||
| | @@ -770,7 +784,8 @@ def _concat(self, indexes: list[Index], name: Hashable) -> Index: | |
| if start is None: | ||
| # This is set by the first non-empty index | ||
| start = rng.start | ||
| if step is None and len(rng) > 1: | ||
| if step is None and (len(rng) > 1 or rng.step < 0): | ||
| # GH #41965 for decreasing RangeIndex, like range(0, -1, -1) | ||
| step = rng.step | ||
| elif step is None: | ||
| # First non-empty index had only one element | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -243,3 +243,9 @@ def test_dropna_pos_args_deprecation(self): | |
| result = df.dropna(1) | ||
| expected = DataFrame({"a": [1, 2, 3]}) | ||
| tm.assert_frame_equal(result, expected) | ||
| | ||
| def test_dropna_retains_RangeIndex_when_nothing_dropped(self): | ||
| Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RangeIndex -> rangeindex | ||
| # GH#41965 | ||
| expected = DataFrame({"a": range(10)}) | ||
| result = expected.dropna() | ||
| tm.assert_frame_equal(result, expected, check_index_type=True) | ||
Uh oh!
There was an error while loading. Please reload this page.