Skip to content

Commit ec41faa

Browse files
authored
BUG: date comparison fails when series is all pd.NaT values (#61200)
1 parent d739c92 commit ec41faa

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

Diff for: doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ Categorical
647647

648648
Datetimelike
649649
^^^^^^^^^^^^
650+
- Bug in :attr:`Series.dt.date` returned datetime dtype on Series with all NaT values; now returns object dtype (:issue:`61188`)
650651
- Bug in :attr:`is_year_start` where a DateTimeIndex constructed via a date_range with frequency 'MS' wouldn't have the correct year or quarter start attributes (:issue:`57377`)
651652
- Bug in :class:`DataFrame` raising ``ValueError`` when ``dtype`` is ``timedelta64`` and ``data`` is a list containing ``None`` (:issue:`60064`)
652653
- Bug in :class:`Timestamp` constructor failing to raise when ``tz=None`` is explicitly specified in conjunction with timezone-aware ``tzinfo`` or data (:issue:`48688`)

Diff for: pandas/core/indexes/accessors.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ def _delegate_property_get(self, name: str):
108108
else:
109109
index = self._parent.index
110110
# return the result as a Series
111-
return Series(result, index=index, name=self.name).__finalize__(self._parent)
111+
return Series(
112+
result, index=index, name=self.name, dtype=result.dtype
113+
).__finalize__(self._parent)
112114

113115
def _delegate_property_set(self, name: str, value, *args, **kwargs) -> NoReturn:
114116
raise ValueError(

Diff for: pandas/tests/series/indexing/test_datetime.py

+20
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,23 @@ def test_compare_datetime_with_all_none():
491491
result = ser > ser2
492492
expected = Series([False, False])
493493
tm.assert_series_equal(result, expected)
494+
495+
496+
def test_dt_date_dtype_all_nat_is_object():
497+
# Ensure .dt.date on all-NaT Series returns object dtype and not datetime64
498+
# GH#61188
499+
s = Series([pd.NaT, pd.NaT], dtype="datetime64[s]")
500+
result = s.dt.date
501+
502+
expected = Series([pd.NaT, pd.NaT], dtype=object)
503+
504+
tm.assert_series_equal(result, expected)
505+
506+
507+
def test_dt_date_all_nat_le_date():
508+
# All-NaT Series should not raise error when compared to a datetime.date
509+
# GH#61188
510+
s = Series([pd.NaT, pd.NaT], dtype="datetime64[s]")
511+
result = s.dt.date <= datetime.now().date()
512+
expected = Series([False, False])
513+
tm.assert_series_equal(result, expected)

Diff for: web/pandas/static/img/books/pandas_cookbook_3.jpeg

1.3 MB
Loading

0 commit comments

Comments
 (0)