From 6fe393675a86c6cba3458564f75281ff71b0e310 Mon Sep 17 00:00:00 2001 From: Michael Hsieh Date: Tue, 25 May 2021 14:09:32 -0700 Subject: [PATCH 1/8] TST: Check Series when cast to object has float in object column (#35603). --- pandas/tests/series/test_constructors.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index af730bf299336..362520a743f02 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1492,7 +1492,16 @@ def test_constructor_cast_object(self, index): s = Series(index.astype(object), dtype=object) exp = Series(index).astype(object) tm.assert_series_equal(s, exp) - + + def test_constructor_with_float_cast_object(self): + # GH#35603 + # data after casting is float not int + ser = Series([1.0]).astype(object) + val = ser.iloc[0] + assert ser.dtype == "object" + assert isinstance(val, float) + assert val == 1.0 + @pytest.mark.parametrize("dtype", [np.datetime64, np.timedelta64]) def test_constructor_generic_timestamp_no_frequency(self, dtype, request): # see gh-15524, gh-15987 From 3f14a966e457ee06fe4f25eeed379390059efe5a Mon Sep 17 00:00:00 2001 From: Michael Hsieh Date: Tue, 25 May 2021 14:38:02 -0700 Subject: [PATCH 2/8] TST: Assert equality with float was changed to assert string has decimal point. May not be good idea to use == to compare floating-point numbers. --- pandas/tests/series/test_constructors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 362520a743f02..d97fe2bad30a3 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1495,12 +1495,12 @@ def test_constructor_cast_object(self, index): def test_constructor_with_float_cast_object(self): # GH#35603 - # data after casting is float not int + # check float format when cast to object ser = Series([1.0]).astype(object) val = ser.iloc[0] assert ser.dtype == "object" assert isinstance(val, float) - assert val == 1.0 + assert "." in str(val) @pytest.mark.parametrize("dtype", [np.datetime64, np.timedelta64]) def test_constructor_generic_timestamp_no_frequency(self, dtype, request): From a1b5dd2cc83bca6e9edde1447bea75011ab69802 Mon Sep 17 00:00:00 2001 From: Michael Hsieh Date: Tue, 25 May 2021 15:13:42 -0700 Subject: [PATCH 3/8] TST: Remove whitespace before and after the new test. To fix PEP8 issues. --- pandas/tests/series/test_constructors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index d97fe2bad30a3..5afcbc658e08d 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1492,7 +1492,7 @@ def test_constructor_cast_object(self, index): s = Series(index.astype(object), dtype=object) exp = Series(index).astype(object) tm.assert_series_equal(s, exp) - + def test_constructor_with_float_cast_object(self): # GH#35603 # check float format when cast to object @@ -1501,7 +1501,7 @@ def test_constructor_with_float_cast_object(self): assert ser.dtype == "object" assert isinstance(val, float) assert "." in str(val) - + @pytest.mark.parametrize("dtype", [np.datetime64, np.timedelta64]) def test_constructor_generic_timestamp_no_frequency(self, dtype, request): # see gh-15524, gh-15987 From 75786bd126dcddcc0c932e24517dce05d3c76392 Mon Sep 17 00:00:00 2001 From: Michael Hsieh Date: Wed, 26 May 2021 10:28:17 -0700 Subject: [PATCH 4/8] TST: Remove new test. Needs to be rewritten to check formatting not construction. --- pandas/tests/series/test_constructors.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 5afcbc658e08d..af730bf299336 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1493,15 +1493,6 @@ def test_constructor_cast_object(self, index): exp = Series(index).astype(object) tm.assert_series_equal(s, exp) - def test_constructor_with_float_cast_object(self): - # GH#35603 - # check float format when cast to object - ser = Series([1.0]).astype(object) - val = ser.iloc[0] - assert ser.dtype == "object" - assert isinstance(val, float) - assert "." in str(val) - @pytest.mark.parametrize("dtype", [np.datetime64, np.timedelta64]) def test_constructor_generic_timestamp_no_frequency(self, dtype, request): # see gh-15524, gh-15987 From 24b70be6b9bbfeb3516593007a5bf10dea3d9b02 Mon Sep 17 00:00:00 2001 From: Michael Hsieh Date: Wed, 26 May 2021 10:50:11 -0700 Subject: [PATCH 5/8] TST: Rewrite and move new test to test_repr folder (#35603). Checks if series representation matches expected String. --- pandas/tests/series/test_repr.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 96a69476ccbef..a0ceb014684cd 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -240,6 +240,15 @@ def test_series_repr_nat(self): ) assert result == expected + def test_float_repr_in_frame(self): + # GH#35603 + # check float format when cast to object + ser = Series([1.0]).astype(object) + expected = ( + "0 1.0\n" + "dtype: object" + ) + assert repr(ser) == expected class TestCategoricalRepr: def test_categorical_repr_unicode(self): From 51f5169750d5d16f3e1dea80060e1ead6601edf5 Mon Sep 17 00:00:00 2001 From: Michael Hsieh Date: Thu, 27 May 2021 10:03:38 -0700 Subject: [PATCH 6/8] TST: Remove in_frame from function name. It is a Series not a DataFrame. --- pandas/tests/series/test_repr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index a0ceb014684cd..6a9a26f9d24f7 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -240,7 +240,7 @@ def test_series_repr_nat(self): ) assert result == expected - def test_float_repr_in_frame(self): + def test_float_repr(self): # GH#35603 # check float format when cast to object ser = Series([1.0]).astype(object) From 72c07f8a421803d19b572d9fadb55314e0a1fd6a Mon Sep 17 00:00:00 2001 From: Michael Hsieh Date: Thu, 27 May 2021 10:17:15 -0700 Subject: [PATCH 7/8] tst: Remove parentheses to try fix linting issue. They should not be there. --- pandas/tests/series/test_repr.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 6a9a26f9d24f7..4a84eec7a5718 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -244,10 +244,7 @@ def test_float_repr(self): # GH#35603 # check float format when cast to object ser = Series([1.0]).astype(object) - expected = ( - "0 1.0\n" - "dtype: object" - ) + expected = "0 1.0\ndtype: object" assert repr(ser) == expected class TestCategoricalRepr: From 968004b0b020656f08821d0264acc2b22d24492e Mon Sep 17 00:00:00 2001 From: Michael Hsieh Date: Thu, 27 May 2021 10:28:42 -0700 Subject: [PATCH 8/8] TST: Add newline to fix linting issue. Failed pre-commit check since want 2 newlines before defining class, but only had 1. --- pandas/tests/series/test_repr.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 4a84eec7a5718..0d5c3bc21c609 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -247,6 +247,7 @@ def test_float_repr(self): expected = "0 1.0\ndtype: object" assert repr(ser) == expected + class TestCategoricalRepr: def test_categorical_repr_unicode(self): # see gh-21002