Skip to content

Commit 3467469

Browse files
authored
BUG: fillna not replacing missing values when duplicate colnames (#43492)
* BUG: fillna not replacing missing values when duplicate colnames * No need for obj
1 parent cd61b59 commit 3467469

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ Indexing
371371
Missing
372372
^^^^^^^
373373
- Bug in :meth:`DataFrame.fillna` with limit and no method ignores axis='columns' or ``axis = 1`` (:issue:`40989`)
374+
- Bug in :meth:`DataFrame.fillna` not replacing missing values when using a dict-like ``value`` and duplicate column names (:issue:`43476`)
374375
-
375376

376377
MultiIndex

pandas/core/generic.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6339,9 +6339,8 @@ def fillna(
63396339
for k, v in value.items():
63406340
if k not in result:
63416341
continue
6342-
obj = result[k]
63436342
downcast_k = downcast if not is_dict else downcast.get(k)
6344-
obj.fillna(v, limit=limit, inplace=True, downcast=downcast_k)
6343+
result[k] = result[k].fillna(v, limit=limit, downcast=downcast_k)
63456344
return result if not inplace else None
63466345

63476346
elif not is_list_like(value):

pandas/tests/frame/methods/test_fillna.py

+11
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,17 @@ def test_fillna_downcast(self):
247247
expected = DataFrame({"a": [1, 0]})
248248
tm.assert_frame_equal(result, expected)
249249

250+
@pytest.mark.parametrize("columns", [["A", "A", "B"], ["A", "A"]])
251+
def test_fillna_dictlike_value_duplicate_colnames(self, columns):
252+
# GH#43476
253+
df = DataFrame(np.nan, index=[0, 1], columns=columns)
254+
with tm.assert_produces_warning(None):
255+
result = df.fillna({"A": 0})
256+
257+
expected = df.copy()
258+
expected["A"] = 0.0
259+
tm.assert_frame_equal(result, expected)
260+
250261
@td.skip_array_manager_not_yet_implemented # TODO(ArrayManager) object upcasting
251262
def test_fillna_dtype_conversion(self):
252263
# make sure that fillna on an empty frame works

0 commit comments

Comments
 (0)