Skip to content

Commit fcff0ac

Browse files
wxing11MarcoGorelli
authored andcommitted
BUG: Fixing DataFrame.Update crashes when NaT present (pandas-dev#49395)
* Fixes DataFrame.update crashes when NaT present. GH16713 * Formatting * No longer reindexing columns, skipping non-matching columns * switching to using set intersection to find shared columns * switching to using index intersection * Removing unnecessary variable creation * Formatting and removing test * Adding to whatsnew * updating whatsnew message * Update doc/source/whatsnew/v2.0.0.rst Co-authored-by: Marco Edward Gorelli <[email protected]>
1 parent 722cee1 commit fcff0ac

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ Missing
662662
- Bug in :meth:`Index.equals` raising ``TypeError`` when :class:`Index` consists of tuples that contain ``NA`` (:issue:`48446`)
663663
- Bug in :meth:`Series.map` caused incorrect result when data has NaNs and defaultdict mapping was used (:issue:`48813`)
664664
- Bug in :class:`NA` raising a ``TypeError`` instead of return :class:`NA` when performing a binary operation with a ``bytes`` object (:issue:`49108`)
665+
- Bug in :meth:`DataFrame.update` with ``overwrite=False`` raising ``TypeError`` when ``self`` has column with ``NaT`` values and column not present in ``other`` (:issue:`16713`)
665666

666667
MultiIndex
667668
^^^^^^^^^^

pandas/core/frame.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -8056,11 +8056,12 @@ def update(
80568056
if not isinstance(other, DataFrame):
80578057
other = DataFrame(other)
80588058

8059-
other = other.reindex_like(self)
8059+
other = other.reindex(self.index)
80608060

8061-
for col in self.columns:
8061+
for col in self.columns.intersection(other.columns):
80628062
this = self[col]._values
80638063
that = other[col]._values
8064+
80648065
if filter_func is not None:
80658066
with np.errstate(all="ignore"):
80668067
mask = ~filter_func(this) | isna(that)

pandas/tests/frame/methods/test_update.py

+10
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,13 @@ def test_update_modify_view(self, using_copy_on_write):
166166
tm.assert_frame_equal(result_view, df2_orig)
167167
else:
168168
tm.assert_frame_equal(result_view, expected)
169+
170+
def test_update_dt_column_with_NaT_create_column(self):
171+
# GH#16713
172+
df = DataFrame({"A": [1, None], "B": [pd.NaT, pd.to_datetime("2016-01-01")]})
173+
df2 = DataFrame({"A": [2, 3]})
174+
df.update(df2, overwrite=False)
175+
expected = DataFrame(
176+
{"A": [1.0, 3.0], "B": [pd.NaT, pd.to_datetime("2016-01-01")]}
177+
)
178+
tm.assert_frame_equal(df, expected)

0 commit comments

Comments
 (0)