Skip to content
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

BUG-24971 copying blocks also considers ndim #25521

Merged
merged 5 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Bug Fixes

**Categorical**

-
- Bug where calling :meth:`Series.replace` on categorical data could return a ``Series`` with incorrect dimensions (:issue:`24971`)
-
-

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ def copy(self, deep=True):
values = self.values
if deep:
values = values.copy()
return self.make_block_same_class(values)
return self.make_block_same_class(values, ndim=self.ndim)

def replace(self, to_replace, value, inplace=False, filter=None,
regex=False, convert=True):
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/series/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,17 @@ def test_replace_mixed_types_with_string(self):
result = s.replace([2, '4'], np.nan)
expected = pd.Series([1, np.nan, 3, np.nan, 4, 5])
tm.assert_series_equal(expected, result)

@pytest.mark.parametrize("categorical, numeric", [
(pd.Categorical('A', categories=['A', 'B']), [1]),
(pd.Categorical(('A', ), categories=['A', 'B']), [1]),
(pd.Categorical(('A', 'B'), categories=['A', 'B']), [1, 2]),
])
def test_copy_categorical_ndim(self, categorical, numeric):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name to to test_replace_categorical

# GH 24971
s = pd.Series(categorical)
result = s.replace({'A': 1, 'B': 2})
expected = pd.Series(numeric)
print(result.dtype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove the prints

print(expected.dtype)
tm.assert_series_equal(expected, result, check_dtype=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this coerces? (I think we have another issue about this) can you find & reference it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like L292 has the reference now (#23305)