Skip to content
7 changes: 6 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8190,11 +8190,16 @@ def update(
if not isinstance(other, DataFrame):
other = DataFrame(other)

other = other.reindex_like(self)
# reindex rows, non-matching columns get skipped
Copy link
Member

Choose a reason for hiding this comment

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

not sure we need this comment

other = other.reindex(self.index)

for col in self.columns:
if col not in other.columns:
continue

this = self[col]._values
that = other[col]._values

if filter_func is not None:
with np.errstate(all="ignore"):
mask = ~filter_func(this) | isna(that)
Expand Down
34 changes: 34 additions & 0 deletions pandas/tests/frame/methods/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,37 @@ def test_update_modify_view(self, using_copy_on_write):
tm.assert_frame_equal(result_view, df2_orig)
else:
tm.assert_frame_equal(result_view, expected)

def test_update_dt_column_with_NaT_create_column(self):
df = DataFrame(
{
"A": [1, None],
"B": [
pd.NaT,
pd.to_datetime("2016-01-01"),
],
Copy link
Member

Choose a reason for hiding this comment

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

can we keep this on a single line?

}
)
df2 = DataFrame({"A": [2, 3]})

Copy link
Member

Choose a reason for hiding this comment

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

let's remove all these newlines in the tests

df.update(df2, overwrite=False)

expected = DataFrame(
{"A": [1.0, 3.0], "B": [pd.NaT, pd.to_datetime("2016-01-01")]}
)

tm.assert_frame_equal(df, expected)

def test_update_dt_column_with_NaT_create_row(self):
Copy link
Member

Choose a reason for hiding this comment

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

not really sure what this test adds, I'd suggest to either:

  • parametrize over the first test
  • just remove this one


df = DataFrame({"A": [1, None], "B": [pd.to_datetime("2017-1-1"), pd.NaT]})

df2 = DataFrame({"A": [2], "B": [pd.to_datetime("2016-01-01")]})

df.update(df2, overwrite=False)

expected = DataFrame(
{"A": [1, None], "B": [pd.to_datetime("2017-1-1"), pd.NaT]}
)

tm.assert_frame_equal(df, expected)