Skip to content

BUG: fix datetimeindex repr #53634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
c0e59e7
add bug fix for datetimeindex repr
Simar-B Jun 9, 2023
642ee78
Add tests
Simar-B Jun 12, 2023
dcd565e
Run precommit
Simar-B Jun 12, 2023
6a8551e
Add bug fix to docs
Simar-B Jun 12, 2023
8fac63d
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jun 13, 2023
ba877ba
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jun 13, 2023
39b93e0
Add check for delta
Simar-B Jun 22, 2023
2b6cd5e
Merge branch 'bug-fix-datetimeindex' of github.com:Simar-B/pandas int…
Simar-B Jun 22, 2023
09303b2
Fix check for delta
Simar-B Jun 22, 2023
4d1506a
Fix precheck
Simar-B Jun 23, 2023
b1c0cf6
Fix union-attrs mypy check
Simar-B Jun 24, 2023
8cc72fd
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jun 27, 2023
43ac7b8
Address Pr comments
Simar-B Jul 14, 2023
593e28c
Reset test_datetime.py
Simar-B Jul 14, 2023
234bebb
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jul 14, 2023
2b862ea
Fix precommit
Simar-B Jul 14, 2023
86b2fe0
Merge branch 'bug-fix-datetimeindex' of github.com:Simar-B/pandas int…
Simar-B Jul 14, 2023
c22f7e7
Fix precommit quotes
Simar-B Jul 14, 2023
d12977a
Fix formatting
Simar-B Jul 14, 2023
6e651eb
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jul 14, 2023
e6ef416
Fix whatsnew doc
Simar-B Jul 17, 2023
0896cc9
Update tests to assert
Simar-B Jul 17, 2023
95a4a5a
run precommit
Simar-B Jul 17, 2023
7d78db5
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jul 17, 2023
7cc38c2
break up long line
Simar-B Jul 17, 2023
b2ac29d
Merge branch 'bug-fix-datetimeindex' of github.com:Simar-B/pandas int…
Simar-B Jul 17, 2023
56c5b16
Fix space placement
Simar-B Jul 17, 2023
76e0138
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jul 17, 2023
ad6189f
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jul 17, 2023
9dc91ec
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jul 18, 2023
41becc0
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jul 18, 2023
3f7adc6
Add github issue number
Simar-B Jul 18, 2023
8e685b5
Merge branch 'bug-fix-datetimeindex' of github.com:Simar-B/pandas int…
Simar-B Jul 18, 2023
423036f
use datetime timedelta
Simar-B Jul 18, 2023
f89920b
run precommit
Simar-B Jul 18, 2023
208d2a9
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jul 18, 2023
de007da
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jul 18, 2023
dfc26c0
Merge branch 'main' into bug-fix-datetimeindex
Simar-B Jul 19, 2023
64ddebd
Fix formatting
Simar-B Jul 22, 2023
a9d1698
Use getattr
Simar-B Jul 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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`)
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
Copy link

@pmlpm1986 pmlpm1986 Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seem to be no tests for non-daily and non-hourly freq values. I provided a more comprehensive set of tests in Simar-B#1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately right now it is impossible to differentiate a DatetimeIndex where time has been passed vs when time has not been passed (see here). Therefore, nothing more can be done afaik.

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