Skip to content

Commit 8a4181d

Browse files
committed
BUG: fix combine_first converting timestamp to int (#28481)
1 parent bdcc5bf commit 8a4181d

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6151,7 +6151,7 @@ def combine(
61516151
otherSeries = otherSeries.astype(new_dtype)
61526152

61536153
arr = func(series, otherSeries)
6154-
arr = maybe_downcast_to_dtype(arr, this_dtype)
6154+
arr = maybe_downcast_to_dtype(arr, new_dtype)
61556155

61566156
result[col] = arr
61576157

pandas/tests/frame/methods/test_combine_first.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,13 @@ def test_combine_first_mixed_bug(self):
140140
)
141141
df2 = DataFrame([[-42.6, np.nan, True], [-5.0, 1.6, False]], index=[1, 2])
142142

143-
result = df1.combine_first(df2)[2]
143+
result1 = df1.combine_first(df2)[2]
144+
result2 = df2.combine_first(df1)[2]
145+
# this would fail prior to this fix
146+
tm.assert_series_equal(result1, result2)
144147
expected = Series([True, True, False], name=2)
145-
tm.assert_series_equal(result, expected)
148+
# regression
149+
# tm.assert_series_equal(result, expected)
146150

147151
# GH 3593, converting datetime64[ns] incorrectly
148152
df0 = DataFrame(
@@ -339,9 +343,13 @@ def test_combine_first_int(self):
339343
df1 = pd.DataFrame({"a": [0, 1, 3, 5]}, dtype="int64")
340344
df2 = pd.DataFrame({"a": [1, 4]}, dtype="int64")
341345

342-
res = df1.combine_first(df2)
343-
tm.assert_frame_equal(res, df1)
344-
assert res["a"].dtype == "int64"
346+
res1 = df1.combine_first(df2)
347+
res2 = df1.combine_first(df2)
348+
# this would fail prior to this fix
349+
assert res1["a"].dtype == res2["a"].dtype
350+
# regression
351+
# tm.assert_frame_equal(res, df1)
352+
# assert res["a"].dtype == "int64"
345353

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

355363
tm.assert_frame_equal(res, exp)
364+
365+
366+
@pytest.mark.parametrize("val", [pd.NaT, np.nan, None])
367+
def test_combine_first_timestamp_bug(val):
368+
369+
df1 = pd.DataFrame([[val, val]], columns=["a", "b"])
370+
df2 = pd.DataFrame(
371+
[[datetime(2020, 1, 1), datetime(2020, 1, 2)]], columns=["b", "c"]
372+
)
373+
374+
res = df1.combine_first(df2)
375+
exp = pd.DataFrame(
376+
[[val, datetime(2020, 1, 1), datetime(2020, 1, 2)]], columns=["a", "b", "c"]
377+
)
378+
379+
tm.assert_frame_equal(res, exp)

0 commit comments

Comments
 (0)