From e4b8cbfec4507db2a64d018d35232bb85c51328a Mon Sep 17 00:00:00 2001 From: andjhall Date: Tue, 5 Apr 2022 19:49:16 +0000 Subject: [PATCH 1/5] Added test for github issue #33830 to test that categorical was preserving DateTimeIndex freq attribute --- pandas/tests/indexes/datetimes/test_constructors.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index b1e764ceb7009..c16e29d7e34b7 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -8,6 +8,7 @@ import dateutil import numpy as np +from pandas._testing.asserters import assert_equal import pytest import pytz @@ -84,6 +85,16 @@ def test_categorical_preserves_tz(self): result = DatetimeIndex(obj) tm.assert_index_equal(result, dti) + def test_categorical_preserves_freq(self): + # GH33830 freq retention in categorical + dti = pd.date_range('2016-01-01', periods=5) + expected = dti.freq + + cat = pd.Categorical(dti) + result = cat.categories.freq + + assert expected == result + def test_dti_with_period_data_raises(self): # GH#23675 data = pd.PeriodIndex(["2016Q1", "2016Q2"], freq="Q") From 32f394d7ec63fb1865ed1123384dd1a9bcca92ad Mon Sep 17 00:00:00 2001 From: andjhall Date: Tue, 5 Apr 2022 16:15:10 -0400 Subject: [PATCH 2/5] Added an additional test for value_counts preserving frequency --- pandas/tests/indexes/datetimes/test_constructors.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index c16e29d7e34b7..fc9f757c99a0b 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -88,12 +88,23 @@ def test_categorical_preserves_tz(self): def test_categorical_preserves_freq(self): # GH33830 freq retention in categorical dti = pd.date_range('2016-01-01', periods=5) + expected = dti.freq cat = pd.Categorical(dti) result = cat.categories.freq assert expected == result + + def test_value_counts_preserves_freq(self): + # GH33830 freq retention in value_counts + dti = pd.date_range('2016-01-01', periods=5) + + expected = dti.freq + + result = dti.value_counts().index.freq + + assert expected == result def test_dti_with_period_data_raises(self): # GH#23675 From 507c7362ee2efc5889e015b99ca63dcd38fe761a Mon Sep 17 00:00:00 2001 From: andjhall Date: Thu, 7 Apr 2022 18:33:40 +0000 Subject: [PATCH 3/5] Modified tests in test_constructors.py to pass the pre-commit check --- .../tests/indexes/datetimes/test_constructors.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index fc9f757c99a0b..1b4148ad4d28e 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -8,7 +8,6 @@ import dateutil import numpy as np -from pandas._testing.asserters import assert_equal import pytest import pytz @@ -27,6 +26,7 @@ to_datetime, ) import pandas._testing as tm +from pandas._testing.asserters import assert_equal from pandas.core.arrays import ( DatetimeArray, period_array, @@ -87,24 +87,24 @@ def test_categorical_preserves_tz(self): def test_categorical_preserves_freq(self): # GH33830 freq retention in categorical - dti = pd.date_range('2016-01-01', periods=5) - + dti = date_range("2016-01-01", periods=5) + expected = dti.freq cat = pd.Categorical(dti) result = cat.categories.freq - assert expected == result - + assert_equal(expected, result) + def test_value_counts_preserves_freq(self): # GH33830 freq retention in value_counts - dti = pd.date_range('2016-01-01', periods=5) - + dti = date_range("2016-01-01", periods=5) + expected = dti.freq result = dti.value_counts().index.freq - assert expected == result + assert_equal(expected, result) def test_dti_with_period_data_raises(self): # GH#23675 From 57e85350715f34a8d47832a7d78d304dd66f8636 Mon Sep 17 00:00:00 2001 From: andjhall Date: Mon, 11 Apr 2022 19:53:20 +0000 Subject: [PATCH 4/5] Removed tests from test_constructors that were not needed --- pandas/tests/indexes/datetimes/test_constructors.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index 1b4148ad4d28e..c1429a075d057 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -96,16 +96,6 @@ def test_categorical_preserves_freq(self): assert_equal(expected, result) - def test_value_counts_preserves_freq(self): - # GH33830 freq retention in value_counts - dti = date_range("2016-01-01", periods=5) - - expected = dti.freq - - result = dti.value_counts().index.freq - - assert_equal(expected, result) - def test_dti_with_period_data_raises(self): # GH#23675 data = pd.PeriodIndex(["2016Q1", "2016Q2"], freq="Q") From 79256757d865948bae3b84b7b96bbfe7e964e442 Mon Sep 17 00:00:00 2001 From: Brian Gollop Date: Mon, 18 Apr 2022 16:21:41 +0000 Subject: [PATCH 5/5] Moved categorical freq retention test to pandas/tests/arrays/categorical/ --- pandas/tests/arrays/categorical/test_constructors.py | 11 +++++++++++ pandas/tests/indexes/datetimes/test_constructors.py | 12 ------------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/pandas/tests/arrays/categorical/test_constructors.py b/pandas/tests/arrays/categorical/test_constructors.py index d221ef5373fea..c108ce9fcf100 100644 --- a/pandas/tests/arrays/categorical/test_constructors.py +++ b/pandas/tests/arrays/categorical/test_constructors.py @@ -759,3 +759,14 @@ def test_constructor_datetime64_non_nano(self): cat = Categorical(values, categories=categories) assert (cat == values).all() + + def test_constructor_preserves_freq(self): + # GH33830 freq retention in categorical + dti = date_range("2016-01-01", periods=5) + + expected = dti.freq + + cat = Categorical(dti) + result = cat.categories.freq + + assert expected == result diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index c1429a075d057..b1e764ceb7009 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -26,7 +26,6 @@ to_datetime, ) import pandas._testing as tm -from pandas._testing.asserters import assert_equal from pandas.core.arrays import ( DatetimeArray, period_array, @@ -85,17 +84,6 @@ def test_categorical_preserves_tz(self): result = DatetimeIndex(obj) tm.assert_index_equal(result, dti) - def test_categorical_preserves_freq(self): - # GH33830 freq retention in categorical - dti = date_range("2016-01-01", periods=5) - - expected = dti.freq - - cat = pd.Categorical(dti) - result = cat.categories.freq - - assert_equal(expected, result) - def test_dti_with_period_data_raises(self): # GH#23675 data = pd.PeriodIndex(["2016Q1", "2016Q2"], freq="Q")