summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/tests/test_twodim_base.py22
-rw-r--r--numpy/lib/twodim_base.py4
2 files changed, 24 insertions, 2 deletions
diff --git a/numpy/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py
index c9a220920..d07e4d578 100644
--- a/numpy/lib/tests/test_twodim_base.py
+++ b/numpy/lib/tests/test_twodim_base.py
@@ -324,6 +324,28 @@ def test_tril_triu_with_inf():
assert_array_equal(np.tril(arr), out_tril)
+def test_tril_triu_dtype():
+ # Issue 4916
+ # tril and triu should return the same dtype as input
+ for c in np.typecodes['All']:
+ if c == 'V':
+ continue
+ arr = np.zeros((3, 3), dtype=c)
+ assert_equal(np.triu(arr).dtype, arr.dtype)
+ assert_equal(np.tril(arr).dtype, arr.dtype)
+
+ # check special cases
+ arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
+ ['2004-01-01T12:00', '2003-01-03T13:45']],
+ dtype='datetime64')
+ assert_equal(np.triu(arr).dtype, arr.dtype)
+ assert_equal(np.tril(arr).dtype, arr.dtype)
+
+ arr = np.zeros((3,3), dtype='f4,f4')
+ assert_equal(np.triu(arr).dtype, arr.dtype)
+ assert_equal(np.tril(arr).dtype, arr.dtype)
+
+
def test_mask_indices():
# simple test without offset
iu = mask_indices(3, np.triu)
diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py
index f26ff0619..7670da1ed 100644
--- a/numpy/lib/twodim_base.py
+++ b/numpy/lib/twodim_base.py
@@ -453,7 +453,7 @@ def tril(m, k=0):
m = asanyarray(m)
mask = tri(*m.shape[-2:], k=k, dtype=bool)
- return where(mask, m, 0)
+ return where(mask, m, zeros(1, m.dtype))
def triu(m, k=0):
@@ -481,7 +481,7 @@ def triu(m, k=0):
m = asanyarray(m)
mask = tri(*m.shape[-2:], k=k-1, dtype=bool)
- return where(mask, 0, m)
+ return where(mask, zeros(1, m.dtype), m)
# Originally borrowed from John Hunter and matplotlib