Skip to content

Commit f66cd05

Browse files
committed
MAINT: Adjust the codebase to the new np.array copy keyword meaning
1 parent 301c5c7 commit f66cd05

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1919,7 +1919,7 @@ def to_numpy(
19191919
dtype = np.dtype(dtype)
19201920
result = self._mgr.as_array(dtype=dtype, copy=copy, na_value=na_value)
19211921
if result.dtype is not dtype:
1922-
result = np.array(result, dtype=dtype, copy=False)
1922+
result = np.asarray(result, dtype=dtype)
19231923

19241924
return result
19251925

pandas/tests/arrays/test_datetimelike.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Timestamp,
1313
)
1414
from pandas._libs.tslibs import to_offset
15+
from pandas.compat.numpy import np_version_gt2
1516

1617
from pandas.core.dtypes.dtypes import PeriodDtype
1718

@@ -640,13 +641,14 @@ def test_round(self, arr1d):
640641

641642
def test_array_interface(self, datetime_index):
642643
arr = datetime_index._data
644+
copy_false = None if np_version_gt2 else False
643645

644646
# default asarray gives the same underlying data (for tz naive)
645647
result = np.asarray(arr)
646648
expected = arr._ndarray
647649
assert result is expected
648650
tm.assert_numpy_array_equal(result, expected)
649-
result = np.array(arr, copy=False)
651+
result = np.array(arr, copy=copy_false)
650652
assert result is expected
651653
tm.assert_numpy_array_equal(result, expected)
652654

@@ -655,7 +657,7 @@ def test_array_interface(self, datetime_index):
655657
expected = arr._ndarray
656658
assert result is expected
657659
tm.assert_numpy_array_equal(result, expected)
658-
result = np.array(arr, dtype="datetime64[ns]", copy=False)
660+
result = np.array(arr, dtype="datetime64[ns]", copy=copy_false)
659661
assert result is expected
660662
tm.assert_numpy_array_equal(result, expected)
661663
result = np.array(arr, dtype="datetime64[ns]")
@@ -698,6 +700,7 @@ def test_array_tz(self, arr1d):
698700
# GH#23524
699701
arr = arr1d
700702
dti = self.index_cls(arr1d)
703+
copy_false = None if np_version_gt2 else False
701704

702705
expected = dti.asi8.view("M8[ns]")
703706
result = np.array(arr, dtype="M8[ns]")
@@ -706,17 +709,18 @@ def test_array_tz(self, arr1d):
706709
result = np.array(arr, dtype="datetime64[ns]")
707710
tm.assert_numpy_array_equal(result, expected)
708711

709-
# check that we are not making copies when setting copy=False
710-
result = np.array(arr, dtype="M8[ns]", copy=False)
712+
# check that we are not making copies when setting copy=copy_false
713+
result = np.array(arr, dtype="M8[ns]", copy=copy_false)
711714
assert result.base is expected.base
712715
assert result.base is not None
713-
result = np.array(arr, dtype="datetime64[ns]", copy=False)
716+
result = np.array(arr, dtype="datetime64[ns]", copy=copy_false)
714717
assert result.base is expected.base
715718
assert result.base is not None
716719

717720
def test_array_i8_dtype(self, arr1d):
718721
arr = arr1d
719722
dti = self.index_cls(arr1d)
723+
copy_false = None if np_version_gt2 else False
720724

721725
expected = dti.asi8
722726
result = np.array(arr, dtype="i8")
@@ -725,8 +729,8 @@ def test_array_i8_dtype(self, arr1d):
725729
result = np.array(arr, dtype=np.int64)
726730
tm.assert_numpy_array_equal(result, expected)
727731

728-
# check that we are still making copies when setting copy=False
729-
result = np.array(arr, dtype="i8", copy=False)
732+
# check that we are still making copies when setting copy=copy_false
733+
result = np.array(arr, dtype="i8", copy=copy_false)
730734
assert result.base is not expected.base
731735
assert result.base is None
732736

@@ -952,13 +956,14 @@ def test_int_properties(self, timedelta_index, propname):
952956

953957
def test_array_interface(self, timedelta_index):
954958
arr = timedelta_index._data
959+
copy_false = None if np_version_gt2 else False
955960

956961
# default asarray gives the same underlying data
957962
result = np.asarray(arr)
958963
expected = arr._ndarray
959964
assert result is expected
960965
tm.assert_numpy_array_equal(result, expected)
961-
result = np.array(arr, copy=False)
966+
result = np.array(arr, copy=copy_false)
962967
assert result is expected
963968
tm.assert_numpy_array_equal(result, expected)
964969

@@ -967,7 +972,7 @@ def test_array_interface(self, timedelta_index):
967972
expected = arr._ndarray
968973
assert result is expected
969974
tm.assert_numpy_array_equal(result, expected)
970-
result = np.array(arr, dtype="timedelta64[ns]", copy=False)
975+
result = np.array(arr, dtype="timedelta64[ns]", copy=copy_false)
971976
assert result is expected
972977
tm.assert_numpy_array_equal(result, expected)
973978
result = np.array(arr, dtype="timedelta64[ns]")

pandas/tests/extension/array_with_attr/array.py

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import numpy as np
1111

12+
from pandas.compat.numpy import np_version_gt2
13+
1214
from pandas.core.dtypes.base import ExtensionDtype
1315

1416
import pandas as pd
@@ -49,6 +51,8 @@ def __init__(self, values, attr=None) -> None:
4951

5052
@classmethod
5153
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
54+
if np_version_gt2 and not copy:
55+
copy = None
5256
data = np.array(scalars, dtype="float64", copy=copy)
5357
return cls(data)
5458

0 commit comments

Comments
 (0)