Skip to content

Commit 2ae7a10

Browse files
Backport PR #57488 on branch 2.2.x (REGR: query raising for all NaT in object column) (#57515)
Backport PR #57488: REGR: query raising for all NaT in object column Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 6766c92 commit 2ae7a10

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Fixed regressions
2727
- Fixed regression in :meth:`DataFrame.loc` which was unnecessarily throwing "incompatible dtype warning" when expanding with partial row indexer and multiple columns (see `PDEP6 <https://pandas.pydata.org/pdeps/0006-ban-upcasting.html>`_) (:issue:`56503`)
2828
- Fixed regression in :meth:`DataFrame.map` with ``na_action="ignore"`` not being respected for NumPy nullable and :class:`ArrowDtypes` (:issue:`57316`)
2929
- Fixed regression in :meth:`DataFrame.merge` raising ``ValueError`` for certain types of 3rd-party extension arrays (:issue:`57316`)
30+
- Fixed regression in :meth:`DataFrame.query` with all ``NaT`` column with object dtype (:issue:`57068`)
3031
- Fixed regression in :meth:`DataFrame.shift` raising ``AssertionError`` for ``axis=1`` and empty :class:`DataFrame` (:issue:`57301`)
3132
- Fixed regression in :meth:`DataFrame.sort_index` not producing a stable sort for a index with duplicates (:issue:`57151`)
3233
- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ def _get_cleaned_column_resolvers(self) -> dict[Hashable, Series]:
657657

658658
return {
659659
clean_column_name(k): Series(
660-
v, copy=False, index=self.index, name=k
660+
v, copy=False, index=self.index, name=k, dtype=self.dtypes[k]
661661
).__finalize__(self)
662662
for k, v in zip(self.columns, self._iter_column_arrays())
663663
if not isinstance(k, int)

pandas/tests/frame/test_query_eval.py

+8
Original file line numberDiff line numberDiff line change
@@ -1415,3 +1415,11 @@ def test_query_ea_equality_comparison(self, dtype, engine):
14151415
}
14161416
)
14171417
tm.assert_frame_equal(result, expected)
1418+
1419+
def test_all_nat_in_object(self):
1420+
# GH#57068
1421+
now = pd.Timestamp.now("UTC") # noqa: F841
1422+
df = DataFrame({"a": pd.to_datetime([None, None], utc=True)}, dtype=object)
1423+
result = df.query("a > @now")
1424+
expected = DataFrame({"a": []}, dtype=object)
1425+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)