Skip to content

BUG GH#40498 Add mask to fillna to only modify indexes specified in v… #42859

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 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,10 @@ def fillna(
"""
inplace = validate_bool_kwarg(inplace, "inplace")

mask = isna(self.values)
isna_mask = isna(self.values)
value_mask = ~isna(value)
mask = isna_mask & value_mask

mask, noop = validate_putmask(self.values, mask)

if limit is not None:
Expand Down
5 changes: 4 additions & 1 deletion pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,10 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
if not self.dtype:
if all(notna(data)):
return data, False
return data.fillna(np.nan), True
data = data.where(notna(data), np.nan)
if all(isna(data)):
Copy link
Contributor

Choose a reason for hiding this comment

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

use isna(data).all()

data = data.astype(np.float64)
return data, True

# error: Non-overlapping identity check (left operand type:
# "Union[ExtensionDtype, str, dtype[Any], Type[object],
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/frame/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ def test_fillna_tzaware_different_column(self):
)
tm.assert_frame_equal(result, expected)

def test_other_missing_values_not_modified(
self, unique_nulls_fixture, unique_nulls_fixture2
):
# GH#40498
df = DataFrame(
{
"A": [1, unique_nulls_fixture, unique_nulls_fixture2, "four"],
"B": [2, unique_nulls_fixture, unique_nulls_fixture2, "eight"],
}
)
value = {"A": {1: 0}, "B": {2: 0}}
result = df.fillna(value)

expected = DataFrame(
{
"A": [1, 0, unique_nulls_fixture2, "four"],
"B": [2, unique_nulls_fixture, 0, "eight"],
}
)

tm.assert_frame_equal(result, expected)

def test_na_actions_categorical(self):

cat = Categorical([1, 2, 3, np.nan], categories=[1, 2, 3])
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,19 @@ def test_fillna_numeric_inplace(self):
expected = x.fillna(value=0)
tm.assert_series_equal(y, expected)

def test_fillna_other_missing_values_not_modified(
self, unique_nulls_fixture, unique_nulls_fixture2
):
# GH#40498
ser = Series([1, unique_nulls_fixture, unique_nulls_fixture2, "four"])

value = {2: 0}
result = ser.fillna(value)

expected = Series([1, unique_nulls_fixture, 0, "four"])

tm.assert_series_equal(result, expected)

# ---------------------------------------------------------------
# CategoricalDtype

Expand Down