Skip to content

Commit 27aa9d8

Browse files
JustinZhengBCjreback
authored andcommitted
BUG-24971 copying blocks also considers ndim (#25521)
1 parent ba7826f commit 27aa9d8

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v0.24.2.rst

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ Bug Fixes
4949
- Bug in reading a JSON with ``orient='table'`` generated by :meth:`DataFrame.to_json` with ``index=False`` (:issue:`25170`)
5050
- Bug where float indexes could have misaligned values when printing (:issue:`25061`)
5151

52+
**Categorical**
53+
54+
- Bug where calling :meth:`Series.replace` on categorical data could return a ``Series`` with incorrect dimensions (:issue:`24971`)
55+
-
56+
-
57+
5258
**Reshaping**
5359

5460
- 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`)

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ def copy(self, deep=True):
739739
values = self.values
740740
if deep:
741741
values = values.copy()
742-
return self.make_block_same_class(values)
742+
return self.make_block_same_class(values, ndim=self.ndim)
743743

744744
def replace(self, to_replace, value, inplace=False, filter=None,
745745
regex=False, convert=True):

pandas/tests/series/test_replace.py

+14
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,20 @@ def test_replace_mixed_types_with_string(self):
281281
expected = pd.Series([1, np.nan, 3, np.nan, 4, 5])
282282
tm.assert_series_equal(expected, result)
283283

284+
@pytest.mark.parametrize("categorical, numeric", [
285+
(pd.Categorical('A', categories=['A', 'B']), [1]),
286+
(pd.Categorical(('A', ), categories=['A', 'B']), [1]),
287+
(pd.Categorical(('A', 'B'), categories=['A', 'B']), [1, 2]),
288+
])
289+
def test_replace_categorical(self, categorical, numeric):
290+
# GH 24971
291+
# Do not check if dtypes are equal due to a known issue that
292+
# Categorical.replace sometimes coerces to object (GH 23305)
293+
s = pd.Series(categorical)
294+
result = s.replace({'A': 1, 'B': 2})
295+
expected = pd.Series(numeric)
296+
tm.assert_series_equal(expected, result, check_dtype=False)
297+
284298
def test_replace_with_no_overflowerror(self):
285299
# GH 25616
286300
# casts to object without Exception from OverflowError

0 commit comments

Comments
 (0)