From 1da008a6945407463d8427abe4e1012da80ceceb Mon Sep 17 00:00:00 2001 From: Paul Reidy Date: Fri, 5 Jan 2018 16:50:23 +0000 Subject: [PATCH 1/3] BUG: show time values in repr of high precision DatetimeIndex --- doc/source/whatsnew/v0.23.0.txt | 3 ++- pandas/io/formats/format.py | 2 +- pandas/tests/io/formats/test_format.py | 28 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index b1efd0dcb43e2..46ff6bc97c449 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -375,7 +375,8 @@ Conversion - Bug in :class:`TimedeltaIndex` where division by a ``Series`` would return a ``TimedeltaIndex`` instead of a ``Series`` (issue:`19042`) - Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (issue:`19043`) - Fixed bug where comparing :class:`DatetimeIndex` failed to raise ``TypeError`` when attempting to compare timezone-aware and timezone-naive datetimelike objects (:issue:`18162`) -- +- Bug in :class:`DatetimeIndex` where the repr was not showing the time values for the end of the day (:issue:`19030`) + Indexing ^^^^^^^^ diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index a4678e5b40849..886a887568d69 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -2188,7 +2188,7 @@ def _is_dates_only(values): consider_values = values_int != iNaT one_day_nanos = (86400 * 1e9) even_days = np.logical_and(consider_values, - values_int % one_day_nanos != 0).sum() == 0 + values_int % int(one_day_nanos) != 0).sum() == 0 if even_days: return True return False diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 23b42b612dace..f89dd4380adaf 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -883,6 +883,20 @@ def test_datetimelike_frame(self): '[10 rows x 2 columns]') assert repr(df) == expected + def test_datetimeindex_highprecision(self): + # GH19030 + # Check that time values for the end of day are included in repr + df = DataFrame({'A': date_range(start='2017-01-01 23:59:59.999999999', + freq='D', periods=5)}) + result = str(df) + assert "23:59:59.999999999" in result + + dti = date_range(start='2017-01-01 23:59:59.999999999', + freq='D', periods=5) + df = DataFrame({'A': range(5)}, index=dti) + result = str(df.index) + assert "23:59:59.999999999" in result + def test_nonunicode_nonascii_alignment(self): df = DataFrame([["aa\xc3\xa4\xc3\xa4", 1], ["bbbb", 2]]) rep_str = df.to_string() @@ -1914,6 +1928,20 @@ def test_datetimeindex(self): result = str(s2.index) assert 'NaT' in result + def test_datetimeindex_highprecision(self): + # GH19030 + # Check that time values for the end of day are included in repr + s1 = Series(date_range(start='2017-01-01 23:59:59.999999999', + freq='D', periods=5)) + result = str(s1) + assert "23:59:59.999999999" in result + + dti = date_range(start='2017-01-01 23:59:59.999999999', freq='D', + periods=5) + s2 = Series(3, index=dti) + result = str(s2.index) + assert "23:59:59.999999999" in result + def test_timedelta64(self): from datetime import datetime, timedelta From 055e0acc9b6e6881c7869225d443b423b6c2ce91 Mon Sep 17 00:00:00 2001 From: Paul Reidy Date: Sun, 7 Jan 2018 12:24:09 +0000 Subject: [PATCH 2/3] clarify whatsnew and parametrize tests --- doc/source/whatsnew/v0.23.0.txt | 2 +- pandas/tests/io/formats/test_format.py | 45 ++++++++++++++++++-------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index 46ff6bc97c449..ebb9e5ad41a77 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -375,7 +375,7 @@ Conversion - Bug in :class:`TimedeltaIndex` where division by a ``Series`` would return a ``TimedeltaIndex`` instead of a ``Series`` (issue:`19042`) - Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (issue:`19043`) - Fixed bug where comparing :class:`DatetimeIndex` failed to raise ``TypeError`` when attempting to compare timezone-aware and timezone-naive datetimelike objects (:issue:`18162`) -- Bug in :class:`DatetimeIndex` where the repr was not showing the time values for the end of the day (:issue:`19030`) +- 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`) Indexing diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index f89dd4380adaf..77d6e0fefbda6 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -883,19 +883,29 @@ def test_datetimelike_frame(self): '[10 rows x 2 columns]') assert repr(df) == expected - def test_datetimeindex_highprecision(self): + + @pytest.mark.parametrize('start_date', [ + '2017-01-01 23:59:59.999999999', + '2017-01-01 23:59:59.99999999', + '2017-01-01 23:59:59.9999999', + '2017-01-01 23:59:59.999999', + '2017-01-01 23:59:59.99999', + '2017-01-01 23:59:59.9999', + ]) + def test_datetimeindex_highprecision(self, start_date): # GH19030 - # Check that time values for the end of day are included in repr - df = DataFrame({'A': date_range(start='2017-01-01 23:59:59.999999999', + # Check that high-precision time values for the end of day are + # included in repr for DatetimeIndex + df = DataFrame({'A': date_range(start=start_date, freq='D', periods=5)}) result = str(df) - assert "23:59:59.999999999" in result + assert start_date in result - dti = date_range(start='2017-01-01 23:59:59.999999999', + dti = date_range(start=start_date, freq='D', periods=5) df = DataFrame({'A': range(5)}, index=dti) result = str(df.index) - assert "23:59:59.999999999" in result + assert start_date in result def test_nonunicode_nonascii_alignment(self): df = DataFrame([["aa\xc3\xa4\xc3\xa4", 1], ["bbbb", 2]]) @@ -1928,19 +1938,26 @@ def test_datetimeindex(self): result = str(s2.index) assert 'NaT' in result - def test_datetimeindex_highprecision(self): + @pytest.mark.parametrize('start_date', [ + '2017-01-01 23:59:59.999999999', + '2017-01-01 23:59:59.99999999', + '2017-01-01 23:59:59.9999999', + '2017-01-01 23:59:59.999999', + '2017-01-01 23:59:59.99999', + '2017-01-01 23:59:59.9999', + ]) + def test_datetimeindex_highprecision(self, start_date): # GH19030 - # Check that time values for the end of day are included in repr - s1 = Series(date_range(start='2017-01-01 23:59:59.999999999', - freq='D', periods=5)) + # Check that high-precision time values for the end of day are + # included in repr for DatetimeIndex + s1 = Series(date_range(start=start_date, freq='D', periods=5)) result = str(s1) - assert "23:59:59.999999999" in result + assert start_date in result - dti = date_range(start='2017-01-01 23:59:59.999999999', freq='D', - periods=5) + dti = date_range(start=start_date, freq='D', periods=5) s2 = Series(3, index=dti) result = str(s2.index) - assert "23:59:59.999999999" in result + assert start_date in result def test_timedelta64(self): From aaa1f0ce5a52f1dac4f7aec12bbe8536a627f9b1 Mon Sep 17 00:00:00 2001 From: Paul Reidy Date: Sun, 7 Jan 2018 12:37:35 +0000 Subject: [PATCH 3/3] pep8 --- pandas/tests/io/formats/test_format.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 77d6e0fefbda6..b277d8256e612 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -883,14 +883,13 @@ def test_datetimelike_frame(self): '[10 rows x 2 columns]') assert repr(df) == expected - @pytest.mark.parametrize('start_date', [ - '2017-01-01 23:59:59.999999999', - '2017-01-01 23:59:59.99999999', - '2017-01-01 23:59:59.9999999', - '2017-01-01 23:59:59.999999', - '2017-01-01 23:59:59.99999', - '2017-01-01 23:59:59.9999', + '2017-01-01 23:59:59.999999999', + '2017-01-01 23:59:59.99999999', + '2017-01-01 23:59:59.9999999', + '2017-01-01 23:59:59.999999', + '2017-01-01 23:59:59.99999', + '2017-01-01 23:59:59.9999', ]) def test_datetimeindex_highprecision(self, start_date): # GH19030 @@ -1939,12 +1938,12 @@ def test_datetimeindex(self): assert 'NaT' in result @pytest.mark.parametrize('start_date', [ - '2017-01-01 23:59:59.999999999', - '2017-01-01 23:59:59.99999999', - '2017-01-01 23:59:59.9999999', - '2017-01-01 23:59:59.999999', - '2017-01-01 23:59:59.99999', - '2017-01-01 23:59:59.9999', + '2017-01-01 23:59:59.999999999', + '2017-01-01 23:59:59.99999999', + '2017-01-01 23:59:59.9999999', + '2017-01-01 23:59:59.999999', + '2017-01-01 23:59:59.99999', + '2017-01-01 23:59:59.9999' ]) def test_datetimeindex_highprecision(self, start_date): # GH19030