Skip to content

Commit 3c01ce2

Browse files
Simar-Bmroeschke
andauthored
BUG: fix datetimeindex repr (pandas-dev#53634)
* add bug fix for datetimeindex repr * Add tests * Run precommit * Add bug fix to docs * Add check for delta * Fix check for delta * Fix precheck * Fix union-attrs mypy check * Address Pr comments * Reset test_datetime.py * Fix precommit * Fix precommit quotes * Fix formatting * Fix whatsnew doc * Update tests to assert * run precommit * break up long line * Fix space placement * Add github issue number * use datetime timedelta * run precommit * Fix formatting * Use getattr Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 620ae26 commit 3c01ce2

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ Metadata
626626
Other
627627
^^^^^
628628
- Bug in :class:`DataFrame` and :class:`Series` raising for data of complex dtype when ``NaN`` values are present (:issue:`53627`)
629+
- Bug in :class:`DatetimeIndex` where ``repr`` of index passed with time does not print time is midnight and non-day based freq(:issue:`53470`)
629630
- Bug in :class:`FloatingArray.__contains__` with ``NaN`` item incorrectly returning ``False`` when ``NaN`` values are present (:issue:`52840`)
630631
- Bug in :func:`api.interchange.from_dataframe` when converting an empty DataFrame object (:issue:`53155`)
631632
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)

pandas/core/indexes/datetimes.py

+7
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,18 @@ def _is_dates_only(self) -> bool:
393393
-------
394394
bool
395395
"""
396+
396397
from pandas.io.formats.format import is_dates_only
397398

399+
delta = getattr(self.freq, "delta", None)
400+
401+
if delta and delta % dt.timedelta(days=1) != dt.timedelta(days=0):
402+
return False
403+
398404
# error: Argument 1 to "is_dates_only" has incompatible type
399405
# "Union[ExtensionArray, ndarray]"; expected "Union[ndarray,
400406
# DatetimeArray, Index, DatetimeIndex]"
407+
401408
return self.tz is None and is_dates_only(self._values) # type: ignore[arg-type]
402409

403410
def __reduce__(self):

pandas/tests/indexes/datetimes/test_formats.py

+30
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,36 @@ def test_dti_repr_short(self):
6868
dr = pd.date_range(start="1/1/2012", periods=3)
6969
repr(dr)
7070

71+
@pytest.mark.parametrize(
72+
"dates, freq, expected_repr",
73+
[
74+
(
75+
["2012-01-01 00:00:00"],
76+
"60T",
77+
(
78+
"DatetimeIndex(['2012-01-01 00:00:00'], "
79+
"dtype='datetime64[ns]', freq='60T')"
80+
),
81+
),
82+
(
83+
["2012-01-01 00:00:00", "2012-01-01 01:00:00"],
84+
"60T",
85+
"DatetimeIndex(['2012-01-01 00:00:00', '2012-01-01 01:00:00'], "
86+
"dtype='datetime64[ns]', freq='60T')",
87+
),
88+
(
89+
["2012-01-01"],
90+
"24H",
91+
"DatetimeIndex(['2012-01-01'], dtype='datetime64[ns]', freq='24H')",
92+
),
93+
],
94+
)
95+
def test_dti_repr_time_midnight(self, dates, freq, expected_repr):
96+
# GH53634
97+
dti = DatetimeIndex(dates, freq)
98+
actual_repr = repr(dti)
99+
assert actual_repr == expected_repr
100+
71101
@pytest.mark.parametrize("method", ["__repr__", "__str__"])
72102
def test_dti_representation(self, method):
73103
idxs = []

0 commit comments

Comments
 (0)