diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 29be9a7341f00..83be4f0a721e6 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -641,6 +641,7 @@ Categorical Datetimelike ^^^^^^^^^^^^ +- Bug in :attr:`Series.dt.date` where Series with all NaT values would raise an error when compared to a datetime.date (:issue:`61188`) - 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`) - Bug in :class:`DataFrame` raising ``ValueError`` when ``dtype`` is ``timedelta64`` and ``data`` is a list containing ``None`` (:issue:`60064`) - 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 --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index c404323a1168c..dd2479c394277 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -108,7 +108,9 @@ def _delegate_property_get(self, name: str): else: index = self._parent.index # return the result as a Series - return Series(result, index=index, name=self.name).__finalize__(self._parent) + return Series( + result, index=index, name=self.name, dtype=result.dtype + ).__finalize__(self._parent) def _delegate_property_set(self, name: str, value, *args, **kwargs) -> NoReturn: raise ValueError( diff --git a/pandas/tests/series/indexing/test_datetime.py b/pandas/tests/series/indexing/test_datetime.py index 97cafc33611ed..92b254b91f173 100644 --- a/pandas/tests/series/indexing/test_datetime.py +++ b/pandas/tests/series/indexing/test_datetime.py @@ -491,3 +491,25 @@ def test_compare_datetime_with_all_none(): result = ser > ser2 expected = Series([False, False]) tm.assert_series_equal(result, expected) + + +def test_dt_date_dtype_all_nat_is_object(): + # Ensure .dt.date on all-NaT Series returns object dtype and not datetime64 + # GH#61188 + s = Series([pd.NaT, pd.NaT]) + s = pd.to_datetime(s) + result = s.dt.date + + expected = Series([pd.NaT, pd.NaT], dtype=object) + + tm.assert_series_equal(result, expected) + + +def test_dt_date_all_nat_le_date(): + # All-NaT Series should not raise error when compared to a datetime.date + # GH#61188 + s = Series([pd.NaT, pd.NaT]) + s = pd.to_datetime(s) + result = s.dt.date <= datetime.now().date() + expected = Series([False, False]) + tm.assert_series_equal(result, expected)