Skip to content

Commit fe66b56

Browse files
reidy-pjreback
authored andcommitted
BUG: show time values in repr of high precision DatetimeIndex (#19109)
1 parent e27ae70 commit fe66b56

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ Conversion
375375
- Bug in :class:`TimedeltaIndex` where division by a ``Series`` would return a ``TimedeltaIndex`` instead of a ``Series`` (issue:`19042`)
376376
- Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (issue:`19043`)
377377
- Fixed bug where comparing :class:`DatetimeIndex` failed to raise ``TypeError`` when attempting to compare timezone-aware and timezone-naive datetimelike objects (:issue:`18162`)
378-
-
378+
- Bug in :class:`DatetimeIndex` where the repr was not showing high-precision time values at the end of a day (e.g., 23:59:59.999999999) (:issue:`19030`)
379+
379380

380381
Indexing
381382
^^^^^^^^

pandas/io/formats/format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2188,7 +2188,7 @@ def _is_dates_only(values):
21882188
consider_values = values_int != iNaT
21892189
one_day_nanos = (86400 * 1e9)
21902190
even_days = np.logical_and(consider_values,
2191-
values_int % one_day_nanos != 0).sum() == 0
2191+
values_int % int(one_day_nanos) != 0).sum() == 0
21922192
if even_days:
21932193
return True
21942194
return False

pandas/tests/io/formats/test_format.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,29 @@ def test_datetimelike_frame(self):
883883
'[10 rows x 2 columns]')
884884
assert repr(df) == expected
885885

886+
@pytest.mark.parametrize('start_date', [
887+
'2017-01-01 23:59:59.999999999',
888+
'2017-01-01 23:59:59.99999999',
889+
'2017-01-01 23:59:59.9999999',
890+
'2017-01-01 23:59:59.999999',
891+
'2017-01-01 23:59:59.99999',
892+
'2017-01-01 23:59:59.9999',
893+
])
894+
def test_datetimeindex_highprecision(self, start_date):
895+
# GH19030
896+
# Check that high-precision time values for the end of day are
897+
# included in repr for DatetimeIndex
898+
df = DataFrame({'A': date_range(start=start_date,
899+
freq='D', periods=5)})
900+
result = str(df)
901+
assert start_date in result
902+
903+
dti = date_range(start=start_date,
904+
freq='D', periods=5)
905+
df = DataFrame({'A': range(5)}, index=dti)
906+
result = str(df.index)
907+
assert start_date in result
908+
886909
def test_nonunicode_nonascii_alignment(self):
887910
df = DataFrame([["aa\xc3\xa4\xc3\xa4", 1], ["bbbb", 2]])
888911
rep_str = df.to_string()
@@ -1914,6 +1937,27 @@ def test_datetimeindex(self):
19141937
result = str(s2.index)
19151938
assert 'NaT' in result
19161939

1940+
@pytest.mark.parametrize('start_date', [
1941+
'2017-01-01 23:59:59.999999999',
1942+
'2017-01-01 23:59:59.99999999',
1943+
'2017-01-01 23:59:59.9999999',
1944+
'2017-01-01 23:59:59.999999',
1945+
'2017-01-01 23:59:59.99999',
1946+
'2017-01-01 23:59:59.9999'
1947+
])
1948+
def test_datetimeindex_highprecision(self, start_date):
1949+
# GH19030
1950+
# Check that high-precision time values for the end of day are
1951+
# included in repr for DatetimeIndex
1952+
s1 = Series(date_range(start=start_date, freq='D', periods=5))
1953+
result = str(s1)
1954+
assert start_date in result
1955+
1956+
dti = date_range(start=start_date, freq='D', periods=5)
1957+
s2 = Series(3, index=dti)
1958+
result = str(s2.index)
1959+
assert start_date in result
1960+
19171961
def test_timedelta64(self):
19181962

19191963
from datetime import datetime, timedelta

0 commit comments

Comments
 (0)