Skip to content
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ Categorical

Datetimelike
^^^^^^^^^^^^
-

- Bug in :meth:`DataFrame.combine_first` that would convert datetime-like column on other :class:`DataFrame` to integer when the column is not present in original :class:`DataFrame` (:issue:`28481`)
-

Timedelta
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6151,7 +6151,7 @@ def combine(
otherSeries = otherSeries.astype(new_dtype)

arr = func(series, otherSeries)
arr = maybe_downcast_to_dtype(arr, this_dtype)
arr = maybe_downcast_to_dtype(arr, new_dtype)

result[col] = arr

Expand Down
34 changes: 29 additions & 5 deletions pandas/tests/frame/methods/test_combine_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,13 @@ def test_combine_first_mixed_bug(self):
)
df2 = DataFrame([[-42.6, np.nan, True], [-5.0, 1.6, False]], index=[1, 2])

result = df1.combine_first(df2)[2]
result1 = df1.combine_first(df2)[2]
result2 = df2.combine_first(df1)[2]
# this would fail prior to this fix
tm.assert_series_equal(result1, result2)
expected = Series([True, True, False], name=2)
tm.assert_series_equal(result, expected)
# regression
# tm.assert_series_equal(result, expected)

# GH 3593, converting datetime64[ns] incorrectly
df0 = DataFrame(
Expand Down Expand Up @@ -339,9 +343,13 @@ def test_combine_first_int(self):
df1 = pd.DataFrame({"a": [0, 1, 3, 5]}, dtype="int64")
Copy link
Contributor

Choose a reason for hiding this comment

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

can you parameterize this as well

df2 = pd.DataFrame({"a": [1, 4]}, dtype="int64")

res = df1.combine_first(df2)
tm.assert_frame_equal(res, df1)
assert res["a"].dtype == "int64"
res1 = df1.combine_first(df2)
res2 = df1.combine_first(df2)
# this would fail prior to this fix
assert res1["a"].dtype == res2["a"].dtype
# regression
# tm.assert_frame_equal(res, df1)
# assert res["a"].dtype == "int64"

@pytest.mark.parametrize("val", [1, 1.0])
def test_combine_first_with_asymmetric_other(self, val):
Expand All @@ -353,3 +361,19 @@ def test_combine_first_with_asymmetric_other(self, val):
exp = pd.DataFrame({"isBool": [True], "isNum": [val]})

tm.assert_frame_equal(res, exp)


@pytest.mark.parametrize("val", [pd.NaT, np.nan, None])
def test_combine_first_timestamp_bug(val):

Copy link
Member

Choose a reason for hiding this comment

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

can you add a comment here "# GH#35514"

df1 = pd.DataFrame([[val, val]], columns=["a", "b"])
df2 = pd.DataFrame(
[[datetime(2020, 1, 1), datetime(2020, 1, 2)]], columns=["b", "c"]
)

res = df1.combine_first(df2)
exp = pd.DataFrame(
[[val, datetime(2020, 1, 1), datetime(2020, 1, 2)]], columns=["a", "b", "c"]
)

tm.assert_frame_equal(res, exp)