From b43f71c8b5a56b60ff48ba953fb01555fb1a9f34 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 25 Jun 2020 17:48:05 +0000 Subject: [PATCH 1/8] TST: added test of view vs copy --- pandas/tests/indexing/test_loc.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 47980e88f76d4..b4ef633537112 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -346,6 +346,16 @@ def test_loc_index(self): result = df.loc[pd.array(mask, dtype="boolean")] tm.assert_frame_equal(result, expected) + def test_loc_copy_vs_view(self): + # GH 15631 + x = DataFrame(zip(range(3), range(3)), columns=["a", "b"]) + + y = x.copy() + q = x.loc[:, "a"] + q += 2 + + tm.assert_frame_equal(x, y) + def test_loc_general(self): df = DataFrame( From 071f80ae6113aa1f931d3775f16f225565c9a0e9 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Fri, 26 Jun 2020 17:31:16 +0000 Subject: [PATCH 2/8] TST: moved test closer to others related --- pandas/tests/indexing/test_loc.py | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index b4ef633537112..4af90408fe859 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -346,24 +346,6 @@ def test_loc_index(self): result = df.loc[pd.array(mask, dtype="boolean")] tm.assert_frame_equal(result, expected) - def test_loc_copy_vs_view(self): - # GH 15631 - x = DataFrame(zip(range(3), range(3)), columns=["a", "b"]) - - y = x.copy() - q = x.loc[:, "a"] - q += 2 - - tm.assert_frame_equal(x, y) - - def test_loc_general(self): - - df = DataFrame( - np.random.rand(4, 4), - columns=["A", "B", "C", "D"], - index=["A", "B", "C", "D"], - ) - # want this to work result = df.loc[:, "A":"B"].iloc[0:2, :] assert (result.columns == ["A", "B"]).all() @@ -904,6 +886,16 @@ def test_identity_slice_returns_new_object(self): original_series[:3] = [7, 8, 9] assert all(sliced_series[:3] == [7, 8, 9]) + def test_loc_copy_vs_view(self): + # GH 15631 + x = DataFrame(zip(range(3), range(3)), columns=["a", "b"]) + + y = x.copy() + q = x.loc[:, "a"] + q += 2 + + tm.assert_frame_equal(x, y) + def test_loc_uint64(self): # GH20722 # Test whether loc accept uint64 max value as index. From 30c9b83447a6d5d578344ddfd48995ae0f3bb01f Mon Sep 17 00:00:00 2001 From: arw2019 Date: Sat, 27 Jun 2020 05:24:03 +0000 Subject: [PATCH 3/8] add values.dtype.kind==f branch to array_with_unit_datetime --- pandas/_libs/tslib.pyx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 44693d60486a9..7ff309b3725d6 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -416,7 +416,6 @@ def array_with_unit_to_datetime( m = cast_from_unit(None, unit) if is_raise: - # try a quick conversion to i8 # if we have nulls that are not type-compat # then need to iterate @@ -429,9 +428,17 @@ def array_with_unit_to_datetime( fvalues = iresult.astype('f8') * m need_to_iterate = False + # GH20445 + if values.dtype.kind == "f": + fresult = values.astype('f8', casting='same_kind', copy=False) + # fill by comparing to NPY_NAT constant + mask = fresult == NPY_NAT + fresult[mask] = 0.0 + fvalues = fvalues.astype('f8') * m # FIXME: this line segfaults rn + need_to_iterate = False + # check the bounds if not need_to_iterate: - if ((fvalues < Timestamp.min.value).any() or (fvalues > Timestamp.max.value).any()): raise OutOfBoundsDatetime(f"cannot convert input with unit '{unit}'") @@ -599,7 +606,6 @@ cpdef array_to_datetime( float offset_seconds, tz_offset set out_tzoffset_vals = set() bint string_to_dts_failed - # specify error conditions assert is_raise or is_ignore or is_coerce From 572363a2928fa1de6b2ca2789147d5a416710faa Mon Sep 17 00:00:00 2001 From: arw2019 Date: Mon, 29 Jun 2020 01:06:23 +0000 Subject: [PATCH 4/8] revert pandas/_libs/tslib.pyx --- pandas/_libs/tslib.pyx | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 7ff309b3725d6..44693d60486a9 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -416,6 +416,7 @@ def array_with_unit_to_datetime( m = cast_from_unit(None, unit) if is_raise: + # try a quick conversion to i8 # if we have nulls that are not type-compat # then need to iterate @@ -428,17 +429,9 @@ def array_with_unit_to_datetime( fvalues = iresult.astype('f8') * m need_to_iterate = False - # GH20445 - if values.dtype.kind == "f": - fresult = values.astype('f8', casting='same_kind', copy=False) - # fill by comparing to NPY_NAT constant - mask = fresult == NPY_NAT - fresult[mask] = 0.0 - fvalues = fvalues.astype('f8') * m # FIXME: this line segfaults rn - need_to_iterate = False - # check the bounds if not need_to_iterate: + if ((fvalues < Timestamp.min.value).any() or (fvalues > Timestamp.max.value).any()): raise OutOfBoundsDatetime(f"cannot convert input with unit '{unit}'") @@ -606,6 +599,7 @@ cpdef array_to_datetime( float offset_seconds, tz_offset set out_tzoffset_vals = set() bint string_to_dts_failed + # specify error conditions assert is_raise or is_ignore or is_coerce From 285022468cf97391dbe0e4fc4954d5cc1b506b26 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Tue, 30 Jun 2020 03:17:02 +0000 Subject: [PATCH 5/8] add test_loc_copy_vs_view to pandas/tests/indexing/test_loc.py --- pandas/tests/indexing/test_loc.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 47980e88f76d4..b4ef633537112 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -346,6 +346,16 @@ def test_loc_index(self): result = df.loc[pd.array(mask, dtype="boolean")] tm.assert_frame_equal(result, expected) + def test_loc_copy_vs_view(self): + # GH 15631 + x = DataFrame(zip(range(3), range(3)), columns=["a", "b"]) + + y = x.copy() + q = x.loc[:, "a"] + q += 2 + + tm.assert_frame_equal(x, y) + def test_loc_general(self): df = DataFrame( From 395e79c9e841f51571e9b987a10a8e34bf3efc0a Mon Sep 17 00:00:00 2001 From: arw2019 Date: Tue, 30 Jun 2020 23:16:53 +0000 Subject: [PATCH 6/8] added second test from GH15631 --- pandas/tests/indexing/test_loc.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 90a941879b452..74f8b21a509af 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -904,6 +904,14 @@ def test_loc_copy_vs_view(self): tm.assert_frame_equal(x, y) + x = DataFrame(zip(range(3), range(3)), columns=["a", "b"]) + + y = x.copy() + q = x.loc[x.index, "a"] + q += 2 + + tm.assert_frame_equal(x, y) + def test_loc_uint64(self): # GH20722 # Test whether loc accept uint64 max value as index. From 21e92067478c3aff2e3b4d744a604c55f3fba74f Mon Sep 17 00:00:00 2001 From: arw2019 Date: Tue, 30 Jun 2020 23:19:50 +0000 Subject: [PATCH 7/8] improved variable names --- pandas/tests/indexing/test_loc.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 74f8b21a509af..1fa81c1aa76fb 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -904,13 +904,11 @@ def test_loc_copy_vs_view(self): tm.assert_frame_equal(x, y) - x = DataFrame(zip(range(3), range(3)), columns=["a", "b"]) - - y = x.copy() + z = x.copy() q = x.loc[x.index, "a"] q += 2 - tm.assert_frame_equal(x, y) + tm.assert_frame_equal(x, z) def test_loc_uint64(self): # GH20722 From 654d25c6726944a848ee2516a20a426ef1b84aee Mon Sep 17 00:00:00 2001 From: arw2019 Date: Thu, 2 Jul 2020 07:04:38 +0000 Subject: [PATCH 8/8] clarified test_in tests/indexing/test_loc.py --- pandas/tests/indexing/test_loc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 1fa81c1aa76fb..30b13b6ea9fce 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -899,13 +899,13 @@ def test_loc_copy_vs_view(self): x = DataFrame(zip(range(3), range(3)), columns=["a", "b"]) y = x.copy() - q = x.loc[:, "a"] + q = y.loc[:, "a"] q += 2 tm.assert_frame_equal(x, y) z = x.copy() - q = x.loc[x.index, "a"] + q = z.loc[x.index, "a"] q += 2 tm.assert_frame_equal(x, z)