diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 839870cb18a0b..1e5918d2e5324 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -621,6 +621,7 @@ Metadata Other ^^^^^ - Bug in :class:`DataFrame` and :class:`Series` raising for data of complex dtype when ``NaN`` values are present (:issue:`53627`) +- Bug in :class:`DatetimeIndex` where ``repr`` of index passed with time does not print time is midnight and non-day based freq(:issue:`53470`) - Bug in :class:`FloatingArray.__contains__` with ``NaN`` item incorrectly returning ``False`` when ``NaN`` values are present (:issue:`52840`) - Bug in :func:`api.interchange.from_dataframe` when converting an empty DataFrame object (:issue:`53155`) - Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 5465442b17fc3..10779bbcde0a2 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -393,11 +393,18 @@ def _is_dates_only(self) -> bool: ------- bool """ + from pandas.io.formats.format import is_dates_only + delta = getattr(self.freq, "delta", None) + + if delta and delta % dt.timedelta(days=1) != dt.timedelta(days=0): + return False + # error: Argument 1 to "is_dates_only" has incompatible type # "Union[ExtensionArray, ndarray]"; expected "Union[ndarray, # DatetimeArray, Index, DatetimeIndex]" + return self.tz is None and is_dates_only(self._values) # type: ignore[arg-type] def __reduce__(self): diff --git a/pandas/tests/indexes/datetimes/test_formats.py b/pandas/tests/indexes/datetimes/test_formats.py index a927799e39783..cb3e0179bf46c 100644 --- a/pandas/tests/indexes/datetimes/test_formats.py +++ b/pandas/tests/indexes/datetimes/test_formats.py @@ -68,6 +68,36 @@ def test_dti_repr_short(self): dr = pd.date_range(start="1/1/2012", periods=3) repr(dr) + @pytest.mark.parametrize( + "dates, freq, expected_repr", + [ + ( + ["2012-01-01 00:00:00"], + "60T", + ( + "DatetimeIndex(['2012-01-01 00:00:00'], " + "dtype='datetime64[ns]', freq='60T')" + ), + ), + ( + ["2012-01-01 00:00:00", "2012-01-01 01:00:00"], + "60T", + "DatetimeIndex(['2012-01-01 00:00:00', '2012-01-01 01:00:00'], " + "dtype='datetime64[ns]', freq='60T')", + ), + ( + ["2012-01-01"], + "24H", + "DatetimeIndex(['2012-01-01'], dtype='datetime64[ns]', freq='24H')", + ), + ], + ) + def test_dti_repr_time_midnight(self, dates, freq, expected_repr): + # GH53634 + dti = DatetimeIndex(dates, freq) + actual_repr = repr(dti) + assert actual_repr == expected_repr + @pytest.mark.parametrize("method", ["__repr__", "__str__"]) def test_dti_representation(self, method): idxs = []