From 5209ebcc761124f64fbf05726c73a58924936675 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 15 Feb 2020 21:04:23 -0600 Subject: [PATCH 01/20] Add Index astype tests --- .../tests/indexes/base_class/test_astype.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 pandas/tests/indexes/base_class/test_astype.py diff --git a/pandas/tests/indexes/base_class/test_astype.py b/pandas/tests/indexes/base_class/test_astype.py new file mode 100644 index 0000000000000..82bf293aeb9af --- /dev/null +++ b/pandas/tests/indexes/base_class/test_astype.py @@ -0,0 +1,21 @@ +import pytest + +from pandas import Index + + +@pytest.mark.parametrize( + "from_type, to_type", [("float64", "int64"), ("int64", "float64")] +) +def test_astype_preserves_name(from_type, to_type): + idx = Index([1, 2], name="name", dtype=from_type).astype(to_type) + + assert idx.name == "name" + + +@pytest.mark.parametrize( + "from_type, to_type", [("float64", "int64"), ("int64", "float64")] +) +def test_copy_with_astype_preserves_name(from_type, to_type): + idx = Index([1, 2], name="name", dtype=from_type).copy(dtype=to_type) + + assert idx.name == "name" From 446e3485cc43cab2c9520a14225a275869498b43 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 15 Feb 2020 21:06:28 -0600 Subject: [PATCH 02/20] Pass name to Index constructor --- pandas/core/indexes/numeric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 877b3d1d2ba30..24764d9e59ed9 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -376,7 +376,7 @@ def astype(self, dtype, copy=True): # TODO(jreback); this can change once we have an EA Index type # GH 13149 arr = astype_nansafe(self.values, dtype=dtype) - return Int64Index(arr) + return Int64Index(arr, name=self.name) return super().astype(dtype, copy=copy) # ---------------------------------------------------------------- From 23230fc07f4a4c254a3af405d262f4f28482e420 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 15 Feb 2020 21:11:28 -0600 Subject: [PATCH 03/20] Release note --- doc/source/whatsnew/v1.0.2.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v1.0.2.rst b/doc/source/whatsnew/v1.0.2.rst index 0216007ea5ba8..50d0d109bec1e 100644 --- a/doc/source/whatsnew/v1.0.2.rst +++ b/doc/source/whatsnew/v1.0.2.rst @@ -37,7 +37,9 @@ Bug fixes - Using ``pd.NA`` with :meth:`DataFrame.to_json` now correctly outputs a null value instead of an empty object (:issue:`31615`) - Fixed bug in parquet roundtrip with nullable unsigned integer dtypes (:issue:`31896`). +**Index** +- Fixed bug where :meth:`Index.astype` would lose the name attribute when converting from ``Float64Index`` to ``Int64Index``. (:issue:`32013`) **Experimental dtypes** From 9ca140160ad092648a46c1854c44e3c0873f3533 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 15 Feb 2020 21:12:29 -0600 Subject: [PATCH 04/20] Change name --- pandas/tests/indexes/base_class/test_astype.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexes/base_class/test_astype.py b/pandas/tests/indexes/base_class/test_astype.py index 82bf293aeb9af..11ccba7cc20fd 100644 --- a/pandas/tests/indexes/base_class/test_astype.py +++ b/pandas/tests/indexes/base_class/test_astype.py @@ -7,15 +7,15 @@ "from_type, to_type", [("float64", "int64"), ("int64", "float64")] ) def test_astype_preserves_name(from_type, to_type): - idx = Index([1, 2], name="name", dtype=from_type).astype(to_type) + idx = Index([1, 2], name="abc", dtype=from_type).astype(to_type) - assert idx.name == "name" + assert idx.name == "abc" @pytest.mark.parametrize( "from_type, to_type", [("float64", "int64"), ("int64", "float64")] ) def test_copy_with_astype_preserves_name(from_type, to_type): - idx = Index([1, 2], name="name", dtype=from_type).copy(dtype=to_type) + idx = Index([1, 2], name="abc", dtype=from_type).copy(dtype=to_type) - assert idx.name == "name" + assert idx.name == "abc" From 8f23161e2ae95965a154c8ec40cef9c0f849a83c Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 16 Feb 2020 10:57:17 -0600 Subject: [PATCH 05/20] Move release note --- doc/source/whatsnew/v1.0.2.rst | 4 ---- doc/source/whatsnew/v1.1.0.rst | 3 +++ 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.0.2.rst b/doc/source/whatsnew/v1.0.2.rst index 50d0d109bec1e..888ccaf826632 100644 --- a/doc/source/whatsnew/v1.0.2.rst +++ b/doc/source/whatsnew/v1.0.2.rst @@ -37,10 +37,6 @@ Bug fixes - Using ``pd.NA`` with :meth:`DataFrame.to_json` now correctly outputs a null value instead of an empty object (:issue:`31615`) - Fixed bug in parquet roundtrip with nullable unsigned integer dtypes (:issue:`31896`). -**Index** - -- Fixed bug where :meth:`Index.astype` would lose the name attribute when converting from ``Float64Index`` to ``Int64Index``. (:issue:`32013`) - **Experimental dtypes** - Fix bug in :meth:`DataFrame.convert_dtypes` for columns that were already using the ``"string"`` dtype (:issue:`31731`). diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 13827e8fc4c33..303f2f0204678 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -98,6 +98,9 @@ Performance improvements Bug fixes ~~~~~~~~~ +**Index** + +- Fixed bug where :meth:`Index.astype` would lose the name attribute when converting from ``Float64Index`` to ``Int64Index``. (:issue:`32013`) Categorical ^^^^^^^^^^^ From 2471e5afe8024896992d8d28d9399e39742fe584 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 16 Feb 2020 16:34:35 -0600 Subject: [PATCH 06/20] Checkout file --- doc/source/whatsnew/v1.0.2.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v1.0.2.rst b/doc/source/whatsnew/v1.0.2.rst index 888ccaf826632..0216007ea5ba8 100644 --- a/doc/source/whatsnew/v1.0.2.rst +++ b/doc/source/whatsnew/v1.0.2.rst @@ -37,6 +37,8 @@ Bug fixes - Using ``pd.NA`` with :meth:`DataFrame.to_json` now correctly outputs a null value instead of an empty object (:issue:`31615`) - Fixed bug in parquet roundtrip with nullable unsigned integer dtypes (:issue:`31896`). + + **Experimental dtypes** - Fix bug in :meth:`DataFrame.convert_dtypes` for columns that were already using the ``"string"`` dtype (:issue:`31731`). From 3959813b9947fd560b25fd41cb97f7b3dbddc243 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 16 Feb 2020 17:14:54 -0600 Subject: [PATCH 07/20] Add some tests --- pandas/tests/indexes/base_class/test_astype.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/base_class/test_astype.py b/pandas/tests/indexes/base_class/test_astype.py index 11ccba7cc20fd..a185deb26745a 100644 --- a/pandas/tests/indexes/base_class/test_astype.py +++ b/pandas/tests/indexes/base_class/test_astype.py @@ -4,8 +4,9 @@ @pytest.mark.parametrize( - "from_type, to_type", [("float64", "int64"), ("int64", "float64")] + "from_type", ["int64", "uint64", "float64"], ) +@pytest.mark.parametrize("to_type", ["int64", "uint64", "float64"]) def test_astype_preserves_name(from_type, to_type): idx = Index([1, 2], name="abc", dtype=from_type).astype(to_type) @@ -13,8 +14,9 @@ def test_astype_preserves_name(from_type, to_type): @pytest.mark.parametrize( - "from_type, to_type", [("float64", "int64"), ("int64", "float64")] + "from_type", ["int64", "uint64", "float64"], ) +@pytest.mark.parametrize("to_type", ["int64", "uint64", "float64"]) def test_copy_with_astype_preserves_name(from_type, to_type): idx = Index([1, 2], name="abc", dtype=from_type).copy(dtype=to_type) From f2a24e63660472aa79237cd4d0ad81ad45d0f732 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Mon, 17 Feb 2020 14:15:10 -0600 Subject: [PATCH 08/20] Move note --- doc/source/whatsnew/v1.1.0.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 303f2f0204678..ac7347c2078b8 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -98,9 +98,6 @@ Performance improvements Bug fixes ~~~~~~~~~ -**Index** - -- Fixed bug where :meth:`Index.astype` would lose the name attribute when converting from ``Float64Index`` to ``Int64Index``. (:issue:`32013`) Categorical ^^^^^^^^^^^ @@ -227,6 +224,7 @@ Reshaping - Bug in :func:`crosstab` when inputs are two Series and have tuple names, the output will keep dummy MultiIndex as columns. (:issue:`18321`) - :meth:`DataFrame.pivot` can now take lists for ``index`` and ``columns`` arguments (:issue:`21425`) - Bug in :func:`concat` where the resulting indices are not copied when ``copy=True`` (:issue:`29879`) +- Bug where :meth:`Index.astype` would lose the name attribute when converting from ``Float64Index`` to ``Int64Index`` (:issue:`32013`) Sparse From 01b326c2c00c03d7b49c0dde05339f3276378e7a Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Mon, 17 Feb 2020 20:18:29 -0600 Subject: [PATCH 09/20] Update tests --- .../tests/indexes/base_class/test_astype.py | 16 +++++----- pandas/tests/indexes/datetimes/test_astype.py | 31 ++++++++++++------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/pandas/tests/indexes/base_class/test_astype.py b/pandas/tests/indexes/base_class/test_astype.py index a185deb26745a..9f80f80ebd742 100644 --- a/pandas/tests/indexes/base_class/test_astype.py +++ b/pandas/tests/indexes/base_class/test_astype.py @@ -4,20 +4,20 @@ @pytest.mark.parametrize( - "from_type", ["int64", "uint64", "float64"], + "from_type", ["int64", "uint64", "float64", "category"], ) -@pytest.mark.parametrize("to_type", ["int64", "uint64", "float64"]) +@pytest.mark.parametrize("to_type", ["int64", "uint64", "float64", "category"]) def test_astype_preserves_name(from_type, to_type): - idx = Index([1, 2], name="abc", dtype=from_type).astype(to_type) + idx = Index([], name="idx", dtype=from_type).astype(to_type) - assert idx.name == "abc" + assert idx.name == "idx" @pytest.mark.parametrize( - "from_type", ["int64", "uint64", "float64"], + "from_type", ["int64", "uint64", "float64", "category"], ) -@pytest.mark.parametrize("to_type", ["int64", "uint64", "float64"]) +@pytest.mark.parametrize("to_type", ["int64", "uint64", "float64", "category"]) def test_copy_with_astype_preserves_name(from_type, to_type): - idx = Index([1, 2], name="abc", dtype=from_type).copy(dtype=to_type) + idx = Index([], name="idx", dtype=from_type).copy(dtype=to_type) - assert idx.name == "abc" + assert idx.name == "idx" diff --git a/pandas/tests/indexes/datetimes/test_astype.py b/pandas/tests/indexes/datetimes/test_astype.py index 916f722247a14..3ed00f1e047ab 100644 --- a/pandas/tests/indexes/datetimes/test_astype.py +++ b/pandas/tests/indexes/datetimes/test_astype.py @@ -22,27 +22,32 @@ class TestDatetimeIndex: def test_astype(self): # GH 13149, GH 13209 - idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.NaN]) + idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.NaN], name="idx") result = idx.astype(object) - expected = Index([Timestamp("2016-05-16")] + [NaT] * 3, dtype=object) + expected = Index( + [Timestamp("2016-05-16")] + [NaT] * 3, dtype=object, name="idx" + ) tm.assert_index_equal(result, expected) result = idx.astype(int) expected = Int64Index( - [1463356800000000000] + [-9223372036854775808] * 3, dtype=np.int64 + [1463356800000000000] + [-9223372036854775808] * 3, + dtype=np.int64, + name="idx", ) tm.assert_index_equal(result, expected) - rng = date_range("1/1/2000", periods=10) + rng = date_range("1/1/2000", periods=10, name="idx") result = rng.astype("i8") - tm.assert_index_equal(result, Index(rng.asi8)) + tm.assert_index_equal(result, Index(rng.asi8, name="idx")) tm.assert_numpy_array_equal(result.values, rng.asi8) def test_astype_uint(self): - arr = date_range("2000", periods=2) + arr = date_range("2000", periods=2, name="idx") expected = pd.UInt64Index( - np.array([946684800000000000, 946771200000000000], dtype="uint64") + np.array([946684800000000000, 946771200000000000], dtype="uint64"), + name="idx", ) tm.assert_index_equal(arr.astype("uint64"), expected) @@ -148,7 +153,7 @@ def test_astype_str(self): def test_astype_datetime64(self): # GH 13149, GH 13209 - idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.NaN]) + idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.NaN], name="idx") result = idx.astype("datetime64[ns]") tm.assert_index_equal(result, idx) @@ -158,10 +163,12 @@ def test_astype_datetime64(self): tm.assert_index_equal(result, idx) assert result is idx - idx_tz = DatetimeIndex(["2016-05-16", "NaT", NaT, np.NaN], tz="EST") + idx_tz = DatetimeIndex(["2016-05-16", "NaT", NaT, np.NaN], tz="EST", name="idx") result = idx_tz.astype("datetime64[ns]") expected = DatetimeIndex( - ["2016-05-16 05:00:00", "NaT", "NaT", "NaT"], dtype="datetime64[ns]" + ["2016-05-16 05:00:00", "NaT", "NaT", "NaT"], + dtype="datetime64[ns]", + name="idx", ) tm.assert_index_equal(result, expected) @@ -273,8 +280,8 @@ def _check_rng(rng): def test_integer_index_astype_datetime(self, tz, dtype): # GH 20997, 20964, 24559 val = [pd.Timestamp("2018-01-01", tz=tz).value] - result = pd.Index(val).astype(dtype) - expected = pd.DatetimeIndex(["2018-01-01"], tz=tz) + result = pd.Index(val, name="idx").astype(dtype) + expected = pd.DatetimeIndex(["2018-01-01"], tz=tz, name="idx") tm.assert_index_equal(result, expected) def test_dti_astype_period(self): From 59b49607a331a89ddfaefa419f2ab75f496cf8be Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Mon, 17 Feb 2020 20:19:21 -0600 Subject: [PATCH 10/20] Pass name --- pandas/core/indexes/base.py | 2 +- pandas/tests/indexes/datetimes/test_astype.py | 9 ++++--- pandas/tests/indexes/period/test_astype.py | 25 +++++++++++-------- .../tests/indexes/timedeltas/test_astype.py | 10 +++++--- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 14ee21ea5614c..f4f761f6f2e05 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -695,7 +695,7 @@ def astype(self, dtype, copy=True): return CategoricalIndex(self.values, name=self.name, dtype=dtype, copy=copy) elif is_extension_array_dtype(dtype): - return Index(np.asarray(self), dtype=dtype, copy=copy) + return Index(np.asarray(self), name=self.name, dtype=dtype, copy=copy) try: casted = self.values.astype(dtype, copy=copy) diff --git a/pandas/tests/indexes/datetimes/test_astype.py b/pandas/tests/indexes/datetimes/test_astype.py index 3ed00f1e047ab..34169a670c169 100644 --- a/pandas/tests/indexes/datetimes/test_astype.py +++ b/pandas/tests/indexes/datetimes/test_astype.py @@ -299,10 +299,11 @@ def test_dti_astype_period(self): class TestAstype: @pytest.mark.parametrize("tz", [None, "US/Central"]) def test_astype_category(self, tz): - obj = pd.date_range("2000", periods=2, tz=tz) + obj = pd.date_range("2000", periods=2, tz=tz, name="idx") result = obj.astype("category") expected = pd.CategoricalIndex( - [pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)] + [pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)], + name="idx", ) tm.assert_index_equal(result, expected) @@ -312,9 +313,9 @@ def test_astype_category(self, tz): @pytest.mark.parametrize("tz", [None, "US/Central"]) def test_astype_array_fallback(self, tz): - obj = pd.date_range("2000", periods=2, tz=tz) + obj = pd.date_range("2000", periods=2, tz=tz, name="idx") result = obj.astype(bool) - expected = pd.Index(np.array([True, True])) + expected = pd.Index(np.array([True, True]), name="idx") tm.assert_index_equal(result, expected) result = obj._data.astype(bool) diff --git a/pandas/tests/indexes/period/test_astype.py b/pandas/tests/indexes/period/test_astype.py index 2f10e45193d5d..b286191623ebb 100644 --- a/pandas/tests/indexes/period/test_astype.py +++ b/pandas/tests/indexes/period/test_astype.py @@ -27,31 +27,34 @@ def test_astype_raises(self, dtype): def test_astype_conversion(self): # GH#13149, GH#13209 - idx = PeriodIndex(["2016-05-16", "NaT", NaT, np.NaN], freq="D") + idx = PeriodIndex(["2016-05-16", "NaT", NaT, np.NaN], freq="D", name="idx") result = idx.astype(object) expected = Index( [Period("2016-05-16", freq="D")] + [Period(NaT, freq="D")] * 3, dtype="object", + name="idx", ) tm.assert_index_equal(result, expected) result = idx.astype(np.int64) - expected = Int64Index([16937] + [-9223372036854775808] * 3, dtype=np.int64) + expected = Int64Index( + [16937] + [-9223372036854775808] * 3, dtype=np.int64, name="idx" + ) tm.assert_index_equal(result, expected) result = idx.astype(str) - expected = Index(str(x) for x in idx) + expected = Index([str(x) for x in idx], name="idx") tm.assert_index_equal(result, expected) - idx = period_range("1990", "2009", freq="A") + idx = period_range("1990", "2009", freq="A", name="idx") result = idx.astype("i8") - tm.assert_index_equal(result, Index(idx.asi8)) + tm.assert_index_equal(result, Index(idx.asi8, name="idx")) tm.assert_numpy_array_equal(result.values, idx.asi8) def test_astype_uint(self): - arr = period_range("2000", periods=2) - expected = UInt64Index(np.array([10957, 10958], dtype="uint64")) + arr = period_range("2000", periods=2, name="idx") + expected = UInt64Index(np.array([10957, 10958], dtype="uint64"), name="idx") tm.assert_index_equal(arr.astype("uint64"), expected) tm.assert_index_equal(arr.astype("uint32"), expected) @@ -116,10 +119,10 @@ def test_astype_object2(self): assert result_list[2] is NaT def test_astype_category(self): - obj = period_range("2000", periods=2) + obj = period_range("2000", periods=2, name="idx") result = obj.astype("category") expected = CategoricalIndex( - [Period("2000-01-01", freq="D"), Period("2000-01-02", freq="D")] + [Period("2000-01-01", freq="D"), Period("2000-01-02", freq="D")], name="idx" ) tm.assert_index_equal(result, expected) @@ -128,9 +131,9 @@ def test_astype_category(self): tm.assert_categorical_equal(result, expected) def test_astype_array_fallback(self): - obj = period_range("2000", periods=2) + obj = period_range("2000", periods=2, name="idx") result = obj.astype(bool) - expected = Index(np.array([True, True])) + expected = Index(np.array([True, True]), name="idx") tm.assert_index_equal(result, expected) result = obj._data.astype(bool) diff --git a/pandas/tests/indexes/timedeltas/test_astype.py b/pandas/tests/indexes/timedeltas/test_astype.py index 82c9d995c9c7c..d9f24b4a35520 100644 --- a/pandas/tests/indexes/timedeltas/test_astype.py +++ b/pandas/tests/indexes/timedeltas/test_astype.py @@ -47,20 +47,22 @@ def test_astype_object_with_nat(self): def test_astype(self): # GH 13149, GH 13209 - idx = TimedeltaIndex([1e14, "NaT", NaT, np.NaN]) + idx = TimedeltaIndex([1e14, "NaT", NaT, np.NaN], name="idx") result = idx.astype(object) - expected = Index([Timedelta("1 days 03:46:40")] + [NaT] * 3, dtype=object) + expected = Index( + [Timedelta("1 days 03:46:40")] + [NaT] * 3, dtype=object, name="idx" + ) tm.assert_index_equal(result, expected) result = idx.astype(int) expected = Int64Index( - [100000000000000] + [-9223372036854775808] * 3, dtype=np.int64 + [100000000000000] + [-9223372036854775808] * 3, dtype=np.int64, name="idx" ) tm.assert_index_equal(result, expected) result = idx.astype(str) - expected = Index(str(x) for x in idx) + expected = Index([str(x) for x in idx], name="idx") tm.assert_index_equal(result, expected) rng = timedelta_range("1 days", periods=10) From b41b62b5c82fd4230f8eb2ce85cb10071ff7dd9d Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Tue, 18 Feb 2020 10:17:02 -0600 Subject: [PATCH 11/20] Update whatsnew --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index ac7347c2078b8..da5da01737c91 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -224,7 +224,7 @@ Reshaping - Bug in :func:`crosstab` when inputs are two Series and have tuple names, the output will keep dummy MultiIndex as columns. (:issue:`18321`) - :meth:`DataFrame.pivot` can now take lists for ``index`` and ``columns`` arguments (:issue:`21425`) - Bug in :func:`concat` where the resulting indices are not copied when ``copy=True`` (:issue:`29879`) -- Bug where :meth:`Index.astype` would lose the name attribute when converting from ``Float64Index`` to ``Int64Index`` (:issue:`32013`) +- Bug where :meth:`Index.astype` would lose the name attribute when converting from ``Float64Index`` to ``Int64Index``, or when casting to an ``ExtensionArray`` dtype (:issue:`32013`) Sparse From e9bb2fec08c8da2dc1b943e1dddabe09b2e3a8b0 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 22 Feb 2020 21:31:44 -0600 Subject: [PATCH 12/20] Use indices fixture --- .../tests/indexes/base_class/test_astype.py | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/pandas/tests/indexes/base_class/test_astype.py b/pandas/tests/indexes/base_class/test_astype.py index 9f80f80ebd742..b0ec6abb151a1 100644 --- a/pandas/tests/indexes/base_class/test_astype.py +++ b/pandas/tests/indexes/base_class/test_astype.py @@ -1,23 +1,21 @@ import pytest -from pandas import Index +@pytest.mark.parametrize("dtype", ["int64", "uint64", "float64", "category"]) +def test_astype_preserves_name(indices, dtype): + try: + result = indices.astype(dtype) + except: + return -@pytest.mark.parametrize( - "from_type", ["int64", "uint64", "float64", "category"], -) -@pytest.mark.parametrize("to_type", ["int64", "uint64", "float64", "category"]) -def test_astype_preserves_name(from_type, to_type): - idx = Index([], name="idx", dtype=from_type).astype(to_type) + assert result.name == indices.name - assert idx.name == "idx" +@pytest.mark.parametrize("dtype", ["int64", "uint64", "float64", "category"]) +def test_astype_with_copy_preserves_name(indices, dtype): + try: + result = indices.copy(dtype=dtype) + except: + return -@pytest.mark.parametrize( - "from_type", ["int64", "uint64", "float64", "category"], -) -@pytest.mark.parametrize("to_type", ["int64", "uint64", "float64", "category"]) -def test_copy_with_astype_preserves_name(from_type, to_type): - idx = Index([], name="idx", dtype=from_type).copy(dtype=to_type) - - assert idx.name == "idx" + assert result.name == indices.name From 7fefe7fc18d03026be6b1cd39ef098beb248ee15 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 22 Feb 2020 21:34:41 -0600 Subject: [PATCH 13/20] Specify errors --- pandas/tests/indexes/base_class/test_astype.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/base_class/test_astype.py b/pandas/tests/indexes/base_class/test_astype.py index b0ec6abb151a1..e391db104437a 100644 --- a/pandas/tests/indexes/base_class/test_astype.py +++ b/pandas/tests/indexes/base_class/test_astype.py @@ -5,7 +5,7 @@ def test_astype_preserves_name(indices, dtype): try: result = indices.astype(dtype) - except: + except (ValueError, TypeError, NotImplementedError): return assert result.name == indices.name @@ -15,7 +15,7 @@ def test_astype_preserves_name(indices, dtype): def test_astype_with_copy_preserves_name(indices, dtype): try: result = indices.copy(dtype=dtype) - except: + except (ValueError, TypeError, NotImplementedError): return assert result.name == indices.name From 1ee1b10829b45cdbfeee694c56b8be015b6dfd0f Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 23 Feb 2020 09:05:43 -0600 Subject: [PATCH 14/20] Set index name --- pandas/tests/indexes/base_class/test_astype.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/tests/indexes/base_class/test_astype.py b/pandas/tests/indexes/base_class/test_astype.py index e391db104437a..29964d0ca5eb8 100644 --- a/pandas/tests/indexes/base_class/test_astype.py +++ b/pandas/tests/indexes/base_class/test_astype.py @@ -3,6 +3,7 @@ @pytest.mark.parametrize("dtype", ["int64", "uint64", "float64", "category"]) def test_astype_preserves_name(indices, dtype): + indices.name = "idx" try: result = indices.astype(dtype) except (ValueError, TypeError, NotImplementedError): @@ -13,6 +14,7 @@ def test_astype_preserves_name(indices, dtype): @pytest.mark.parametrize("dtype", ["int64", "uint64", "float64", "category"]) def test_astype_with_copy_preserves_name(indices, dtype): + indices.name = "idx" try: result = indices.copy(dtype=dtype) except (ValueError, TypeError, NotImplementedError): From 0266f7e024d52cd17f0697f63505da05235b5ea7 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Fri, 28 Feb 2020 20:13:56 -0600 Subject: [PATCH 15/20] Update and move tests --- .../tests/indexes/base_class/test_astype.py | 23 ------------------- pandas/tests/indexes/test_common.py | 22 ++++++++++++++++++ 2 files changed, 22 insertions(+), 23 deletions(-) delete mode 100644 pandas/tests/indexes/base_class/test_astype.py diff --git a/pandas/tests/indexes/base_class/test_astype.py b/pandas/tests/indexes/base_class/test_astype.py deleted file mode 100644 index 29964d0ca5eb8..0000000000000 --- a/pandas/tests/indexes/base_class/test_astype.py +++ /dev/null @@ -1,23 +0,0 @@ -import pytest - - -@pytest.mark.parametrize("dtype", ["int64", "uint64", "float64", "category"]) -def test_astype_preserves_name(indices, dtype): - indices.name = "idx" - try: - result = indices.astype(dtype) - except (ValueError, TypeError, NotImplementedError): - return - - assert result.name == indices.name - - -@pytest.mark.parametrize("dtype", ["int64", "uint64", "float64", "category"]) -def test_astype_with_copy_preserves_name(indices, dtype): - indices.name = "idx" - try: - result = indices.copy(dtype=dtype) - except (ValueError, TypeError, NotImplementedError): - return - - assert result.name == indices.name diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index b46e6514b4536..e0a23caf8d8f0 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -337,3 +337,25 @@ def test_has_duplicates(self, indices): idx = holder([indices[0]] * 5) assert idx.is_unique is False assert idx.has_duplicates is True + + @pytest.mark.parametrize("dtype", ["int64", "uint64", "float64", "category"]) + @pytest.mark.parametrize("copy", [True, False]) + def test_astype_preserves_name(self, indices, dtype, copy): + # https://github.com/pandas-dev/pandas/issues/32013 + if isinstance(indices, MultiIndex): + indices.names = ["idx1", "idx2"] + else: + indices.name = "idx" + + try: + if copy: + result = indices.copy(dtype=dtype) + else: + result = indices.astype(dtype) + except (ValueError, TypeError, NotImplementedError): + return + + if isinstance(indices, MultiIndex): + assert result.names == indices.names + else: + assert result.name == indices.name From b461674d89732151eac01c1603458b96e25c9f89 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Mon, 2 Mar 2020 14:09:33 -0600 Subject: [PATCH 16/20] Tests --- pandas/tests/indexes/test_common.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index e0a23caf8d8f0..c58de06c20b25 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -338,7 +338,10 @@ def test_has_duplicates(self, indices): assert idx.is_unique is False assert idx.has_duplicates is True - @pytest.mark.parametrize("dtype", ["int64", "uint64", "float64", "category"]) + @pytest.mark.parametrize( + "dtype", + ["int64", "uint64", "float64", "category", "datetime64[ns]", "timedelta64[ns]"], + ) @pytest.mark.parametrize("copy", [True, False]) def test_astype_preserves_name(self, indices, dtype, copy): # https://github.com/pandas-dev/pandas/issues/32013 From a55bf9ead5bebf7b274a8db53d0348e89e384ada Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Mon, 2 Mar 2020 19:16:39 -0600 Subject: [PATCH 17/20] Catch more --- pandas/tests/indexes/test_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index c58de06c20b25..b3b7356439499 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -355,7 +355,7 @@ def test_astype_preserves_name(self, indices, dtype, copy): result = indices.copy(dtype=dtype) else: result = indices.astype(dtype) - except (ValueError, TypeError, NotImplementedError): + except (ValueError, TypeError, NotImplementedError, UnicodeEncodeError): return if isinstance(indices, MultiIndex): From 045f36486c9e9a7448c90110efc10140ce1587d2 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Mon, 2 Mar 2020 20:29:24 -0600 Subject: [PATCH 18/20] Try another exception --- pandas/tests/indexes/test_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index b3b7356439499..a3a7798b17580 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -355,7 +355,7 @@ def test_astype_preserves_name(self, indices, dtype, copy): result = indices.copy(dtype=dtype) else: result = indices.astype(dtype) - except (ValueError, TypeError, NotImplementedError, UnicodeEncodeError): + except (ValueError, TypeError, NotImplementedError, SystemError): return if isinstance(indices, MultiIndex): From dfa141e08475210d652810a995f6b781249b9775 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Mon, 16 Mar 2020 21:20:28 -0500 Subject: [PATCH 19/20] Comment --- pandas/tests/indexes/test_common.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index d4bb0d9fcbe47..7df45eb3182be 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -387,6 +387,7 @@ def test_astype_preserves_name(self, indices, dtype, copy): indices.name = "idx" try: + # Some of these conversions cannot succeed so we use a try / except if copy: result = indices.copy(dtype=dtype) else: From 2b2fb84563d15f1efea7930afa8a256e78d4ace5 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Tue, 24 Mar 2020 16:57:35 -0500 Subject: [PATCH 20/20] Index names --- pandas/tests/indexes/test_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 7eeb5f8d4bceb..01d72670f37aa 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -378,7 +378,7 @@ def test_has_duplicates(self, indices): def test_astype_preserves_name(self, indices, dtype, copy): # https://github.com/pandas-dev/pandas/issues/32013 if isinstance(indices, MultiIndex): - indices.names = ["idx1", "idx2"] + indices.names = ["idx" + str(i) for i in range(indices.nlevels)] else: indices.name = "idx"