From af02fb0197093f07a83d0fde810815d23d6498d8 Mon Sep 17 00:00:00 2001 From: Sylvain MARIE Date: Sat, 29 Apr 2023 16:17:06 +0200 Subject: [PATCH 1/4] Added tests for period str and repr. Fixes #53000 --- pandas/tests/scalar/period/test_period.py | 41 +++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index a17079d0e76cb..6e316b6edeaea 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -705,12 +705,41 @@ def test_to_timestamp_microsecond(self, ts, expected, freq): # -------------------------------------------------------------- # Rendering: __repr__, strftime, etc - def test_repr(self): - p = Period("Jan-2000") - assert "2000-01" in repr(p) - - p = Period("2000-12-15") - assert "2000-12-15" in repr(p) + @pytest.mark.parametrize("str_ts,freq,str_res", ( + ("Jan-2000", None, "2000-01"), + ("2000-12-15", None, "2000-12-15"), + ("2000-12-15", "N", "2000-12-15 00:00:00.000000000"), + ("2000-12-15", "U", "2000-12-15 00:00:00.000000"), + ("2000-12-15", "L", "2000-12-15 00:00:00.000"), + ("2000-12-15", "S", "2000-12-15 00:00:00"), + ("2000-12-15", "T", "2000-12-15 00:00"), + ("2000-12-15", "H", "2000-12-15 00:00"), + # ("2000-12-15", "BH", "2000-12-15"), + # ("2000-12-15", "BYS", "2000-12-15"), + # ("2000-12-15", "YS", "2000-12-15"), + # ("2000-12-15", "BY", "2000-12-15"), + ("2000-12-15", "Y", "2000"), + # ("2000-12-15", "BQS", "2000-12-15"), + # ("2000-12-15", "QS", "2000-12-15"), + # ("2000-12-15", "BQ", "2000-12-15"), + ("2000-12-15", "Q", "2000Q4"), + # ("2000-12-15", "CBMS", "2000-12-15"), + # ("2000-12-15", "BMS", "2000-12-15"), + # ("2000-12-15", "SMS", "2000-12-15"), + # ("2000-12-15", "MS", "2000-12-15"), + # ("2000-12-15", "CBM", "2000-12-15"), + # ("2000-12-15", "BM", "2000-12-15"), + # ("2000-12-15", "SM", "2000-12-15"), + ("2000-12-15", "M", "2000-12"), + ("2000-12-15", "W", "2000-12-11/2000-12-17"), + ("2000-12-15", "D", "2000-12-15"), + # ("2000-12-15", "C", "2000-12-15"), + ("2000-12-15", "B", "2000-12-15"), + )) + def test_repr(self, str_ts, freq, str_res): + p = Period(str_ts, freq=freq) + assert str(p) == str_res + assert str_res in repr(p) def test_repr_nat(self): p = Period("nat", freq="M") From 1e70ba8b3001ee33421ada9006a755f88970614c Mon Sep 17 00:00:00 2001 From: Sylvain MARIE Date: Sat, 29 Apr 2023 21:24:13 +0200 Subject: [PATCH 2/4] Removed commented params in test, improved test for repr, and blackened. --- pandas/tests/scalar/period/test_period.py | 54 +++++++++-------------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 6e316b6edeaea..c68fddd2f6407 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -705,41 +705,29 @@ def test_to_timestamp_microsecond(self, ts, expected, freq): # -------------------------------------------------------------- # Rendering: __repr__, strftime, etc - @pytest.mark.parametrize("str_ts,freq,str_res", ( - ("Jan-2000", None, "2000-01"), - ("2000-12-15", None, "2000-12-15"), - ("2000-12-15", "N", "2000-12-15 00:00:00.000000000"), - ("2000-12-15", "U", "2000-12-15 00:00:00.000000"), - ("2000-12-15", "L", "2000-12-15 00:00:00.000"), - ("2000-12-15", "S", "2000-12-15 00:00:00"), - ("2000-12-15", "T", "2000-12-15 00:00"), - ("2000-12-15", "H", "2000-12-15 00:00"), - # ("2000-12-15", "BH", "2000-12-15"), - # ("2000-12-15", "BYS", "2000-12-15"), - # ("2000-12-15", "YS", "2000-12-15"), - # ("2000-12-15", "BY", "2000-12-15"), - ("2000-12-15", "Y", "2000"), - # ("2000-12-15", "BQS", "2000-12-15"), - # ("2000-12-15", "QS", "2000-12-15"), - # ("2000-12-15", "BQ", "2000-12-15"), - ("2000-12-15", "Q", "2000Q4"), - # ("2000-12-15", "CBMS", "2000-12-15"), - # ("2000-12-15", "BMS", "2000-12-15"), - # ("2000-12-15", "SMS", "2000-12-15"), - # ("2000-12-15", "MS", "2000-12-15"), - # ("2000-12-15", "CBM", "2000-12-15"), - # ("2000-12-15", "BM", "2000-12-15"), - # ("2000-12-15", "SM", "2000-12-15"), - ("2000-12-15", "M", "2000-12"), - ("2000-12-15", "W", "2000-12-11/2000-12-17"), - ("2000-12-15", "D", "2000-12-15"), - # ("2000-12-15", "C", "2000-12-15"), - ("2000-12-15", "B", "2000-12-15"), - )) - def test_repr(self, str_ts, freq, str_res): + @pytest.mark.parametrize( + "str_ts,freq,str_res,str_freq", + ( + ("Jan-2000", None, "2000-01", "M"), + ("2000-12-15", None, "2000-12-15", "D"), + ("2000-12-15", "N", "2000-12-15 00:00:00.000000000", "N"), + ("2000-12-15", "U", "2000-12-15 00:00:00.000000", "U"), + ("2000-12-15", "L", "2000-12-15 00:00:00.000", "L"), + ("2000-12-15", "S", "2000-12-15 00:00:00", "S"), + ("2000-12-15", "T", "2000-12-15 00:00", "T"), + ("2000-12-15", "H", "2000-12-15 00:00", "H"), + ("2000-12-15", "Y", "2000", "A-DEC"), + ("2000-12-15", "Q", "2000Q4", "Q-DEC"), + ("2000-12-15", "M", "2000-12", "M"), + ("2000-12-15", "W", "2000-12-11/2000-12-17", "W-SUN"), + ("2000-12-15", "D", "2000-12-15", "D"), + ("2000-12-15", "B", "2000-12-15", "B"), + ), + ) + def test_repr(self, str_ts, freq, str_res, str_freq): p = Period(str_ts, freq=freq) assert str(p) == str_res - assert str_res in repr(p) + assert repr(p) == f"Period('{str_res}', '{str_freq}')" def test_repr_nat(self): p = Period("nat", freq="M") From ca2afae2c05b8a3a8a37255ea250f5f350a70258 Mon Sep 17 00:00:00 2001 From: Sylvain MARIE Date: Sun, 30 Apr 2023 10:09:58 +0200 Subject: [PATCH 3/4] Better tests --- pandas/tests/scalar/period/test_period.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index c68fddd2f6407..d4006b9c14a1e 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -710,12 +710,17 @@ def test_to_timestamp_microsecond(self, ts, expected, freq): ( ("Jan-2000", None, "2000-01", "M"), ("2000-12-15", None, "2000-12-15", "D"), - ("2000-12-15", "N", "2000-12-15 00:00:00.000000000", "N"), - ("2000-12-15", "U", "2000-12-15 00:00:00.000000", "U"), - ("2000-12-15", "L", "2000-12-15 00:00:00.000", "L"), - ("2000-12-15", "S", "2000-12-15 00:00:00", "S"), - ("2000-12-15", "T", "2000-12-15 00:00", "T"), - ("2000-12-15", "H", "2000-12-15 00:00", "H"), + ( + "2000-12-15 13:45:26.123456789", + "N", + "2000-12-15 13:45:26.123456789", + "N", + ), + ("2000-12-15 13:45:26.123456789", "U", "2000-12-15 13:45:26.123456", "U"), + ("2000-12-15 13:45:26.123456789", "L", "2000-12-15 13:45:26.123", "L"), + ("2000-12-15 13:45:26", "S", "2000-12-15 13:45:26", "S"), + ("2000-12-15 13:45:26", "T", "2000-12-15 13:45", "T"), + ("2000-12-15 13:45:26", "H", "2000-12-15 13:00", "H"), ("2000-12-15", "Y", "2000", "A-DEC"), ("2000-12-15", "Q", "2000Q4", "Q-DEC"), ("2000-12-15", "M", "2000-12", "M"), From 19ac71807f15a9edffc2651d083198dc56f17eb1 Mon Sep 17 00:00:00 2001 From: Sylvain MARIE Date: Sun, 30 Apr 2023 10:19:07 +0200 Subject: [PATCH 4/4] Moved the two independent tests into parametrized test --- pandas/tests/scalar/period/test_period.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index d4006b9c14a1e..d908c641a30c6 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -717,7 +717,9 @@ def test_to_timestamp_microsecond(self, ts, expected, freq): "N", ), ("2000-12-15 13:45:26.123456789", "U", "2000-12-15 13:45:26.123456", "U"), + ("2000-12-15 13:45:26.123456", None, "2000-12-15 13:45:26.123456", "U"), ("2000-12-15 13:45:26.123456789", "L", "2000-12-15 13:45:26.123", "L"), + ("2000-12-15 13:45:26.123", None, "2000-12-15 13:45:26.123", "L"), ("2000-12-15 13:45:26", "S", "2000-12-15 13:45:26", "S"), ("2000-12-15 13:45:26", "T", "2000-12-15 13:45", "T"), ("2000-12-15 13:45:26", "H", "2000-12-15 13:00", "H"), @@ -738,16 +740,6 @@ def test_repr_nat(self): p = Period("nat", freq="M") assert repr(NaT) in repr(p) - def test_millisecond_repr(self): - p = Period("2000-01-01 12:15:02.123") - - assert repr(p) == "Period('2000-01-01 12:15:02.123', 'L')" - - def test_microsecond_repr(self): - p = Period("2000-01-01 12:15:02.123567") - - assert repr(p) == "Period('2000-01-01 12:15:02.123567', 'U')" - def test_strftime(self): # GH#3363 p = Period("2000-1-1 12:34:12", freq="S")