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 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
6 changes: 6 additions & 0 deletions doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ Bug Fixes
- Bug in reading a JSON with ``orient='table'`` generated by :meth:`DataFrame.to_json` with ``index=False`` (:issue:`25170`)
- Bug where float indexes could have misaligned values when printing (:issue:`25061`)

**Categorical**

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

**Reshaping**

- Bug in :meth:`~pandas.core.groupby.GroupBy.transform` where applying a function to a timezone aware column would return a timezone naive result (:issue:`24198`)
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 @@ -739,7 +739,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 @@ -281,6 +281,20 @@ def test_replace_mixed_types_with_string(self):
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_replace_categorical(self, categorical, numeric):
# GH 24971
# Do not check if dtypes are equal due to a known issue that
# Categorical.replace sometimes coerces to object (GH 23305)
s = pd.Series(categorical)
result = s.replace({'A': 1, 'B': 2})
expected = pd.Series(numeric)
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)


def test_replace_with_no_overflowerror(self):
# GH 25616
# casts to object without Exception from OverflowError
Expand Down