From 021b80dadeeb46caa6c06c111834f16a91c872b6 Mon Sep 17 00:00:00 2001 From: Cristian Garcia Date: Sun, 11 Apr 2021 18:02:24 -0500 Subject: [PATCH 1/3] add TestCasting --- pandas/tests/indexes/test_indexing.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pandas/tests/indexes/test_indexing.py b/pandas/tests/indexes/test_indexing.py index 8d2637e4a06f6..bc07165452142 100644 --- a/pandas/tests/indexes/test_indexing.py +++ b/pandas/tests/indexes/test_indexing.py @@ -19,6 +19,7 @@ from pandas.errors import InvalidIndexError from pandas import ( + DataFrame, DatetimeIndex, Float64Index, Index, @@ -248,6 +249,20 @@ def test_putmask_with_wrong_mask(self, index): index.putmask("foo", fill) +class TestCasting: + def test_maybe_cast_with_dtype(self): + # https://github.com/pandas-dev/pandas/issues/32413 + frame = DataFrame(index=Index([1, np.nan])) + + cond = frame.index.notna() + other = "a" + frame.reset_index().index.astype(str) + + fixed_index = frame.index.where(cond, other) + + assert (other == Index(["a0", "a1"])).all() + assert (fixed_index == Index([1.0, "a1"])).all() + + @pytest.mark.parametrize( "idx", [Index([1, 2, 3]), Index([0.1, 0.2, 0.3]), Index(["a", "b", "c"])] ) From c6964c488e3a562fc981c2a7fb3c4132bb836532 Mon Sep 17 00:00:00 2001 From: Cristian Garcia Date: Mon, 12 Apr 2021 10:33:57 -0500 Subject: [PATCH 2/3] remove use of DataFrame + use assert_index_equal --- pandas/tests/indexes/test_indexing.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/pandas/tests/indexes/test_indexing.py b/pandas/tests/indexes/test_indexing.py index bc07165452142..3a622e2355abd 100644 --- a/pandas/tests/indexes/test_indexing.py +++ b/pandas/tests/indexes/test_indexing.py @@ -19,7 +19,6 @@ from pandas.errors import InvalidIndexError from pandas import ( - DataFrame, DatetimeIndex, Float64Index, Index, @@ -252,15 +251,15 @@ def test_putmask_with_wrong_mask(self, index): class TestCasting: def test_maybe_cast_with_dtype(self): # https://github.com/pandas-dev/pandas/issues/32413 - frame = DataFrame(index=Index([1, np.nan])) + index = Index([1, np.nan]) - cond = frame.index.notna() - other = "a" + frame.reset_index().index.astype(str) + cond = index.notna() + other = "a" + Index(range(2)).astype(str) - fixed_index = frame.index.where(cond, other) + fixed_index = index.where(cond, other) - assert (other == Index(["a0", "a1"])).all() - assert (fixed_index == Index([1.0, "a1"])).all() + tm.assert_index_equal(other, Index(["a0", "a1"])) + tm.assert_index_equal(fixed_index, Index([1.0, "a1"])) @pytest.mark.parametrize( From 17b8d908c089e90b41a64c00356b1a16f340118b Mon Sep 17 00:00:00 2001 From: Cristian Garcia Date: Thu, 22 Apr 2021 17:53:05 -0500 Subject: [PATCH 3/3] move test --- pandas/tests/indexes/numeric/test_indexing.py | 12 ++++++++++++ pandas/tests/indexes/test_indexing.py | 14 -------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/pandas/tests/indexes/numeric/test_indexing.py b/pandas/tests/indexes/numeric/test_indexing.py index 5f2f8f75045bb..43dc1daf6f392 100644 --- a/pandas/tests/indexes/numeric/test_indexing.py +++ b/pandas/tests/indexes/numeric/test_indexing.py @@ -376,6 +376,18 @@ def test_where(self, klass, index): result = index.where(klass(cond)) tm.assert_index_equal(result, expected) + def test_maybe_cast_with_dtype(self): + # https://github.com/pandas-dev/pandas/issues/32413 + index = Index([1, np.nan]) + + cond = index.notna() + other = "a" + Index(range(2)).astype(str) + + fixed_index = index.where(cond, other) + + tm.assert_index_equal(other, Index(["a0", "a1"])) + tm.assert_index_equal(fixed_index, Index([1.0, "a1"])) + class TestTake: @pytest.mark.parametrize("klass", [Float64Index, Int64Index, UInt64Index]) diff --git a/pandas/tests/indexes/test_indexing.py b/pandas/tests/indexes/test_indexing.py index 3a622e2355abd..8d2637e4a06f6 100644 --- a/pandas/tests/indexes/test_indexing.py +++ b/pandas/tests/indexes/test_indexing.py @@ -248,20 +248,6 @@ def test_putmask_with_wrong_mask(self, index): index.putmask("foo", fill) -class TestCasting: - def test_maybe_cast_with_dtype(self): - # https://github.com/pandas-dev/pandas/issues/32413 - index = Index([1, np.nan]) - - cond = index.notna() - other = "a" + Index(range(2)).astype(str) - - fixed_index = index.where(cond, other) - - tm.assert_index_equal(other, Index(["a0", "a1"])) - tm.assert_index_equal(fixed_index, Index([1.0, "a1"])) - - @pytest.mark.parametrize( "idx", [Index([1, 2, 3]), Index([0.1, 0.2, 0.3]), Index(["a", "b", "c"])] )