Skip to content

BUG: Fix unwanted type casting while replacing values in a DataFrame #33067

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

Closed
wants to merge 5 commits into from
Closed
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
13 changes: 12 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,13 +711,24 @@ def replace(

# try again with a compatible block
block = self.astype(object)
return block.replace(
block_replaced = block.replace(
to_replace=to_replace,
value=value,
inplace=inplace,
regex=regex,
convert=convert,
)
blocks_converted = []
for ls_elem in block_replaced:
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this logic not just in Block.replace itself? seems too complicated here

Copy link
Member Author

@phofl phofl Mar 27, 2020

Choose a reason for hiding this comment

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

I wanted to ensure, that the conversion would only take place, after the block was forcefully cast to an ObjectBlock before.

I can add it to the replace function, if that would be better.

Copy link
Member

Choose a reason for hiding this comment

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

I wanted to ensure, that the conversion would only take place, after the block was forcefully cast to an ObjectBlock before.

I can add it to the replace function, if that would be better.

the regression was caused by a clean-up to Block.replace (#27768), see #32988 (comment)

so fixing Block.replace as @jreback suggests seems more appropriate

# If a replace was executed block_replaced is list of lists,
# if no replace was necessary block_replaced is only a list
if is_list_like(ls_elem):
blocks_converted.extend(
[sub_block.convert() for sub_block in ls_elem]
)
else:
blocks_converted.extend([ls_elem.convert()])
return blocks_converted

values = self.values
if lib.is_scalar(to_replace) and isinstance(values, np.ndarray):
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ def mix_abc() -> Dict[str, List[Union[float, str]]]:


class TestDataFrameReplace:
def test_replace_without_type_cast(self):
# PH: 32988: Fix unwanted type casting while replacing
result = pd.DataFrame(np.eye(2)).replace(
to_replace=[None, -np.inf, np.inf], value=pd.NA
)
tm.assert_frame_equal(result, pd.DataFrame(np.eye(2)))

result = pd.DataFrame(np.eye(2)).replace(
to_replace=[None, -np.inf, np.inf, 1.0], value=pd.NA
)

expected = pd.DataFrame({0: [np.nan, 0.0], 1: [0.0, np.nan]})
tm.assert_frame_equal(result, expected)

def test_replace_inplace(self, datetime_frame, float_string_frame):
datetime_frame["A"][:5] = np.nan
datetime_frame["A"][-5:] = np.nan
Expand Down