Skip to content

Commit

Permalink
BUG: fix datetimeindex repr (pandas-dev#53634)
Browse files Browse the repository at this point in the history
* 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 <10647082+mroeschke@users.noreply.github.com>

---------

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
  • Loading branch information
Simar-B and mroeschke committed Jul 22, 2023
1 parent 620ae26 commit 3c01ce2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,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`)
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/indexes/datetimes/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down

0 comments on commit 3c01ce2

Please sign in to comment.