From a63365bbbd8934134ad4a0e4bf7157fbb5c235b9 Mon Sep 17 00:00:00 2001 From: Hardik Prajapati Date: Sat, 3 Oct 2020 21:50:04 +0530 Subject: [PATCH 1/4] TST: astype cast via loc with nan values (#31861) --- pandas/tests/frame/methods/test_astype.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandas/tests/frame/methods/test_astype.py b/pandas/tests/frame/methods/test_astype.py index d3f256259b15f..29b68375b5fd0 100644 --- a/pandas/tests/frame/methods/test_astype.py +++ b/pandas/tests/frame/methods/test_astype.py @@ -136,6 +136,15 @@ def test_astype_cast_nan_inf_int(self, val, dtype): with pytest.raises(ValueError, match=msg): df.astype(dtype) + @pytest.mark.parametrize("dtype", ["Int64", "Int32", "Int16"]) + @pytest.mark.parametrize("val", [np.nan]) + def test_astype_cast_via_loc_nan_int(self, val, dtype): + # see GH#31861 + expected = DataFrame({"a": ["foo"], "b": integer_array([val], dtype=dtype)}) + result = DataFrame({"a": ["foo"], "b": [val]}) + result.loc[:, "b"] = result.loc[:, "b"].astype(dtype) + tm.assert_frame_equal(result, expected) + def test_astype_str(self): # see GH#9757 a = Series(date_range("2010-01-04", periods=5)) From 52b7c213aa95e0a37ffbebba659b30aaa7b43bee Mon Sep 17 00:00:00 2001 From: Hardik Prajapati Date: Wed, 7 Oct 2020 20:49:03 +0530 Subject: [PATCH 2/4] TST: added NA to the astype via loc test (#31861) --- pandas/tests/frame/methods/test_astype.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/frame/methods/test_astype.py b/pandas/tests/frame/methods/test_astype.py index 29b68375b5fd0..2348670c29d96 100644 --- a/pandas/tests/frame/methods/test_astype.py +++ b/pandas/tests/frame/methods/test_astype.py @@ -4,6 +4,7 @@ import pytest from pandas import ( + NA, Categorical, CategoricalDtype, DataFrame, @@ -137,7 +138,7 @@ def test_astype_cast_nan_inf_int(self, val, dtype): df.astype(dtype) @pytest.mark.parametrize("dtype", ["Int64", "Int32", "Int16"]) - @pytest.mark.parametrize("val", [np.nan]) + @pytest.mark.parametrize("val", [np.nan, NA]) def test_astype_cast_via_loc_nan_int(self, val, dtype): # see GH#31861 expected = DataFrame({"a": ["foo"], "b": integer_array([val], dtype=dtype)}) From 129b1b51cd109c406d56fc2dcbcc31e8b968cb0b Mon Sep 17 00:00:00 2001 From: Hardik Prajapati Date: Fri, 9 Oct 2020 19:20:46 +0530 Subject: [PATCH 3/4] TST: updated to proper comment and variable name --- pandas/tests/frame/methods/test_astype.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/frame/methods/test_astype.py b/pandas/tests/frame/methods/test_astype.py index 2348670c29d96..6c387ab13ad33 100644 --- a/pandas/tests/frame/methods/test_astype.py +++ b/pandas/tests/frame/methods/test_astype.py @@ -140,11 +140,11 @@ def test_astype_cast_nan_inf_int(self, val, dtype): @pytest.mark.parametrize("dtype", ["Int64", "Int32", "Int16"]) @pytest.mark.parametrize("val", [np.nan, NA]) def test_astype_cast_via_loc_nan_int(self, val, dtype): - # see GH#31861 + # GH31861 expected = DataFrame({"a": ["foo"], "b": integer_array([val], dtype=dtype)}) - result = DataFrame({"a": ["foo"], "b": [val]}) - result.loc[:, "b"] = result.loc[:, "b"].astype(dtype) - tm.assert_frame_equal(result, expected) + df = DataFrame({"a": ["foo"], "b": [val]}) + df.loc[:, "b"] = df.loc[:, "b"].astype(dtype) + tm.assert_frame_equal(df, expected) def test_astype_str(self): # see GH#9757 From 5a993b82869516b812bf795c9d21eb77cf06a313 Mon Sep 17 00:00:00 2001 From: Hardik Prajapati Date: Sun, 11 Oct 2020 12:25:23 +0530 Subject: [PATCH 4/4] TST: added working conditions for astype via loc (#31861) --- pandas/tests/frame/methods/test_astype.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pandas/tests/frame/methods/test_astype.py b/pandas/tests/frame/methods/test_astype.py index 6c387ab13ad33..1d511dceaa0ee 100644 --- a/pandas/tests/frame/methods/test_astype.py +++ b/pandas/tests/frame/methods/test_astype.py @@ -146,6 +146,25 @@ def test_astype_cast_via_loc_nan_int(self, val, dtype): df.loc[:, "b"] = df.loc[:, "b"].astype(dtype) tm.assert_frame_equal(df, expected) + expected2 = DataFrame( + {"a": ["foo", "foo"], "b": integer_array([val, 1.0], dtype=dtype)} + ) + df2 = DataFrame({"a": ["foo", "foo"], "b": [val, 1.0]}) + df2.loc[:, "b"] = df2.loc[:, "b"].astype(dtype) + tm.assert_frame_equal(df2, expected2) + + expected3 = DataFrame( + {"a": ["foo", "foo"], "b": integer_array([val, val], dtype=dtype)} + ) + df3 = DataFrame({"a": ["foo", "foo"], "b": [val, val]}) + df3.loc[:, "b"] = df3.loc[:, "b"].astype(dtype) + tm.assert_frame_equal(df3, expected3) + + expected4 = DataFrame({"b": integer_array([val], dtype=dtype)}) + df4 = DataFrame({"b": [val]}) + df4.loc[:, "b"] = df4.loc[:, "b"].astype(dtype) + tm.assert_frame_equal(df4, expected4) + def test_astype_str(self): # see GH#9757 a = Series(date_range("2010-01-04", periods=5))