From f3d5b1c00a1ec705bcddb79e0774fff85d3b4007 Mon Sep 17 00:00:00 2001 From: auderson Date: Fri, 12 May 2023 13:37:59 +0800 Subject: [PATCH 1/6] use is_integer --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 485cc9db5ffe7..69154a25f9d7b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4800,7 +4800,7 @@ def insert( if not allow_duplicates and column in self.columns: # Should this be a different kind of error?? raise ValueError(f"cannot insert {column}, already exists") - if not isinstance(loc, int): + if not is_integer(loc): raise TypeError("loc must be int") if isinstance(value, DataFrame) and len(value.columns) > 1: From cf6fe68b9191b2201be17bd18df1e4ba31531300 Mon Sep 17 00:00:00 2001 From: auderson Date: Fri, 12 May 2023 13:54:54 +0800 Subject: [PATCH 2/6] add test --- pandas/tests/frame/indexing/test_insert.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/tests/frame/indexing/test_insert.py b/pandas/tests/frame/indexing/test_insert.py index 666a6ec3710a6..61a18dd438465 100644 --- a/pandas/tests/frame/indexing/test_insert.py +++ b/pandas/tests/frame/indexing/test_insert.py @@ -110,3 +110,8 @@ def test_insert_frame(self): ) with pytest.raises(ValueError, match=msg): df.insert(1, "newcol", df) + + def test_insert_int64_loc(self): + df = DataFrame({"a": [1, 2]}) + df.insert(np.int64(0), "b", 0) + tm.assert_frame_equal(df, DataFrame({"b": [0, 0], "a": [1, 2]})) \ No newline at end of file From 7c6c3a60bdd49830f5beb12cc52dfd5fca66f2b5 Mon Sep 17 00:00:00 2001 From: auderson Date: Fri, 12 May 2023 14:10:47 +0800 Subject: [PATCH 3/6] reformat file --- pandas/tests/frame/indexing/test_insert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/indexing/test_insert.py b/pandas/tests/frame/indexing/test_insert.py index 61a18dd438465..7201551631550 100644 --- a/pandas/tests/frame/indexing/test_insert.py +++ b/pandas/tests/frame/indexing/test_insert.py @@ -114,4 +114,4 @@ def test_insert_frame(self): def test_insert_int64_loc(self): df = DataFrame({"a": [1, 2]}) df.insert(np.int64(0), "b", 0) - tm.assert_frame_equal(df, DataFrame({"b": [0, 0], "a": [1, 2]})) \ No newline at end of file + tm.assert_frame_equal(df, DataFrame({"b": [0, 0], "a": [1, 2]})) From 0f3a45e0f0b9169a6f14dae81a53a8829838ce96 Mon Sep 17 00:00:00 2001 From: auderson Date: Sat, 13 May 2023 09:21:54 +0800 Subject: [PATCH 4/6] convert non stdlib ints to satisfy typing checks --- pandas/core/frame.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 69154a25f9d7b..459b162e2a6c9 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4802,7 +4802,8 @@ def insert( raise ValueError(f"cannot insert {column}, already exists") if not is_integer(loc): raise TypeError("loc must be int") - + # convert non stdlib ints to satisfy typing checks + loc = int(loc) if isinstance(value, DataFrame) and len(value.columns) > 1: raise ValueError( f"Expected a one-dimensional object, got a DataFrame with " From 52657b62249ab6710218997597cd5bcc9bac7d8b Mon Sep 17 00:00:00 2001 From: auderson Date: Sat, 13 May 2023 09:23:51 +0800 Subject: [PATCH 5/6] add GH number --- pandas/tests/frame/indexing/test_insert.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/frame/indexing/test_insert.py b/pandas/tests/frame/indexing/test_insert.py index 7201551631550..13e43abe0dd7f 100644 --- a/pandas/tests/frame/indexing/test_insert.py +++ b/pandas/tests/frame/indexing/test_insert.py @@ -112,6 +112,7 @@ def test_insert_frame(self): df.insert(1, "newcol", df) def test_insert_int64_loc(self): + # GH#53193 df = DataFrame({"a": [1, 2]}) df.insert(np.int64(0), "b", 0) tm.assert_frame_equal(df, DataFrame({"b": [0, 0], "a": [1, 2]})) From 6449f27657ff8862beadecf22be9c211e4309617 Mon Sep 17 00:00:00 2001 From: auderson Date: Sat, 13 May 2023 09:47:39 +0800 Subject: [PATCH 6/6] add to whatsnew --- doc/source/whatsnew/v2.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 52fc8512c9db3..e659bf7adbf1a 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -351,6 +351,7 @@ Conversion - Bug in :meth:`ArrowDtype.numpy_dtype` returning nanosecond units for non-nanosecond ``pyarrow.timestamp`` and ``pyarrow.duration`` types (:issue:`51800`) - Bug in :meth:`DataFrame.__repr__` incorrectly raising a ``TypeError`` when the dtype of a column is ``np.record`` (:issue:`48526`) - Bug in :meth:`DataFrame.info` raising ``ValueError`` when ``use_numba`` is set (:issue:`51922`) +- Bug in :meth:`DataFrame.insert` raising ``TypeError`` if ``loc`` is ``np.int64`` (:issue:`53193`) - Strings