summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTravis E. Oliphant <teoliphant@gmail.com>2012-10-09 22:31:25 -0700
committerTravis E. Oliphant <teoliphant@gmail.com>2012-10-09 22:31:25 -0700
commitebc9bbb0f1d3f316254b29f8965112c85b63e62f (patch)
treea3e8e05e937ffd78b4f0719fdbdf17103b40132f
parent87930c4c30f14226ae8ceb0340858fd9940d67ea (diff)
parent1a71edc55b227e590022d402e5b6558d3a9921f1 (diff)
downloadnumpy-ebc9bbb0f1d3f316254b29f8965112c85b63e62f.tar.gz
Merge pull request #476 from njsmith/copy-memory-order
[FIX] preserve memory order in np.copy()
-rw-r--r--numpy/add_newdocs.py5
-rw-r--r--numpy/lib/function_base.py6
-rw-r--r--numpy/lib/tests/test_function_base.py26
3 files changed, 34 insertions, 3 deletions
diff --git a/numpy/add_newdocs.py b/numpy/add_newdocs.py
index 20da8ea8b..09effaaf5 100644
--- a/numpy/add_newdocs.py
+++ b/numpy/add_newdocs.py
@@ -3222,10 +3222,13 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('copy',
Controls the memory layout of the copy. 'C' means C-order,
'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
'C' otherwise. 'K' means match the layout of `a` as closely
- as possible.
+ as possible. (Note that this function and :func:numpy.copy are very
+ similar, but have different default values for their order=
+ arguments.)
See also
--------
+ numpy.copy
numpy.copyto
Examples
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index e32492958..65f4ecb05 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -778,7 +778,7 @@ def select(condlist, choicelist, default=0):
S = S*ones(asarray(pfac).shape, S.dtype)
return choose(S, tuple(choicelist))
-def copy(a, order='C'):
+def copy(a, order='K'):
"""
Return an array copy of the given object.
@@ -790,7 +790,9 @@ def copy(a, order='C'):
Controls the memory layout of the copy. 'C' means C-order,
'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
'C' otherwise. 'K' means match the layout of `a` as closely
- as possible.
+ as possible. (Note that this function and :meth:ndarray.copy are very
+ similar, but have different default values for their order=
+ arguments.)
Returns
-------
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index da3eb2b84..49544b22b 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -42,6 +42,32 @@ class TestAll(TestCase):
assert_array_equal(np.alltrue(y1, axis=1), [0, 0, 1])
+class TestCopy(TestCase):
+ def test_basic(self):
+ a = np.array([[1, 2], [3, 4]])
+ a_copy = np.copy(a)
+ assert_array_equal(a, a_copy)
+ a_copy[0, 0] = 10
+ assert_equal(a[0, 0], 1)
+ assert_equal(a_copy[0, 0], 10)
+
+ def test_order(self):
+ # It turns out that people rely on np.copy() preserving order by
+ # default; changing this broke scikit-learn:
+ # https://github.com/scikit-learn/scikit-learn/commit/7842748cf777412c506a8c0ed28090711d3a3783
+ a = np.array([[1, 2], [3, 4]])
+ assert_(a.flags.c_contiguous)
+ assert_(not a.flags.f_contiguous)
+ a_fort = np.array([[1, 2], [3, 4]], order="F")
+ assert_(not a_fort.flags.c_contiguous)
+ assert_(a_fort.flags.f_contiguous)
+ a_copy = np.copy(a)
+ assert_(a_copy.flags.c_contiguous)
+ assert_(not a_copy.flags.f_contiguous)
+ a_fort_copy = np.copy(a_fort)
+ assert_(not a_fort_copy.flags.c_contiguous)
+ assert_(a_fort_copy.flags.f_contiguous)
+
class TestAverage(TestCase):
def test_basic(self):
y1 = np.array([1, 2, 3])