From 07f0348d1dc14dda3ee7c5467202395869df6cf0 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Tue, 18 Aug 2020 00:12:14 +0200 Subject: [PATCH 1/2] TST: Verify whether read-only datetime64 array can be factorized (35650) --- pandas/tests/test_algos.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index a080bf0feaebc..5a84e82f16aad 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -251,6 +251,25 @@ def test_object_factorize(self, writable): tm.assert_numpy_array_equal(codes, expected_codes) tm.assert_numpy_array_equal(uniques, expected_uniques) + @pytest.mark.parametrize( + "data, expected_codes, expected_uniques", + [ + ( + np.datetime64("2020-01-01T00:00:00.000"), + np.array([0], dtype=np.int64), + np.array(["2020-01-01T00:00:00.000000000"], dtype="datetime64[ns]"), + ) + ], + ) + def test_datetime64_factorize(self, data, expected_codes, expected_uniques): + # GH35650 Verify whether read-only datetime64 array can be factorized + arr = np.array([data]) + arr.flags.writeable = False + + codes, uniques = pd.factorize(arr) + tm.assert_numpy_array_equal(codes, expected_codes) + tm.assert_numpy_array_equal(uniques, expected_uniques) + def test_deprecate_order(self): # gh 19727 - check warning is raised for deprecated keyword, order. # Test not valid once order keyword is removed. From f1f5b9b54dbd3e2b6456e9dbd8aefe551971ca2e Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Tue, 18 Aug 2020 20:58:29 +0200 Subject: [PATCH 2/2] CLN: Make test more inline with similar factorize tests --- pandas/tests/test_algos.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 5a84e82f16aad..04ad5b479016f 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -251,22 +251,16 @@ def test_object_factorize(self, writable): tm.assert_numpy_array_equal(codes, expected_codes) tm.assert_numpy_array_equal(uniques, expected_uniques) - @pytest.mark.parametrize( - "data, expected_codes, expected_uniques", - [ - ( - np.datetime64("2020-01-01T00:00:00.000"), - np.array([0], dtype=np.int64), - np.array(["2020-01-01T00:00:00.000000000"], dtype="datetime64[ns]"), - ) - ], - ) - def test_datetime64_factorize(self, data, expected_codes, expected_uniques): + def test_datetime64_factorize(self, writable): # GH35650 Verify whether read-only datetime64 array can be factorized - arr = np.array([data]) - arr.flags.writeable = False + data = np.array([np.datetime64("2020-01-01T00:00:00.000")]) + data.setflags(write=writable) + expected_codes = np.array([0], dtype=np.int64) + expected_uniques = np.array( + ["2020-01-01T00:00:00.000000000"], dtype="datetime64[ns]" + ) - codes, uniques = pd.factorize(arr) + codes, uniques = pd.factorize(data) tm.assert_numpy_array_equal(codes, expected_codes) tm.assert_numpy_array_equal(uniques, expected_uniques)