summaryrefslogtreecommitdiff
path: root/numpy/add_newdocs.py
diff options
context:
space:
mode:
authorJay Bourque <jay.bourque@continuum.io>2013-12-20 12:08:53 -0600
committerJay Bourque <jay.bourque@continuum.io>2014-02-25 14:53:28 -0600
commit1f9d4d2613d7c8bccf7e16720e8d0fa87e74e34a (patch)
tree76f7d671d3a593849127f74d53b6beed6ae3911c /numpy/add_newdocs.py
parent56eb28ed29573d644696743804decf3a8d3260fc (diff)
downloadnumpy-1f9d4d2613d7c8bccf7e16720e8d0fa87e74e34a.tar.gz
BUG: Fix promote_types, can_cast, as astype issues
- promote_types does not return correct string size for integer and string arguments. Fix so that integer and string types are promoted to string type that is long enough to hold integer type safely cast to string. - can_cast incorrectly returns True for certain integer and string types. Fix so that can_cast only returns True if string type is long enough to hold integer type safely cast to string. - calling astype to convert integer to string should fail if string type is not long enough to hold integer converted to string and casting argument is set to "safe".
Diffstat (limited to 'numpy/add_newdocs.py')
-rw-r--r--numpy/add_newdocs.py28
1 files changed, 23 insertions, 5 deletions
diff --git a/numpy/add_newdocs.py b/numpy/add_newdocs.py
index be343f79d..6934cadcc 100644
--- a/numpy/add_newdocs.py
+++ b/numpy/add_newdocs.py
@@ -1598,6 +1598,14 @@ add_newdoc('numpy.core.multiarray', 'can_cast',
out : bool
True if cast can occur according to the casting rule.
+ Notes
+ -----
+ Starting in NumPy 1.9, can_cast function now returns False in 'safe'
+ casting mode for integer/float dtype and string dtype if the string dtype
+ length is not long enough to store the max integer/float value converted
+ to a string. Previously can_cast in 'safe' mode returned True for
+ integer/float dtype and a string dtype of any length.
+
See also
--------
dtype, result_type
@@ -1618,7 +1626,7 @@ add_newdoc('numpy.core.multiarray', 'can_cast',
>>> np.can_cast('i8', 'f4')
False
>>> np.can_cast('i4', 'S4')
- True
+ False
Casting scalars
@@ -1693,6 +1701,11 @@ add_newdoc('numpy.core.multiarray', 'promote_types',
Notes
-----
.. versionadded:: 1.6.0
+ Starting in NumPy 1.9, promote_types function now returns a valid string
+ length when given an integer or float dtype as one argument and a string
+ dtype as another argument. Previously it always returned the input string
+ dtype, even if it wasn't long enough to store the max integer/float value
+ converted to a string.
See Also
--------
@@ -1709,10 +1722,8 @@ add_newdoc('numpy.core.multiarray', 'promote_types',
>>> np.promote_types('>i8', '<c8')
dtype('complex128')
- >>> np.promote_types('i1', 'S8')
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: invalid type promotion
+ >>> np.promote_types('i4', 'S8')
+ dtype('S11')
""")
@@ -3126,6 +3137,13 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('astype',
is a new array of the same shape as the input array, with dtype, order
given by `dtype`, `order`.
+ Notes
+ -----
+ Starting in NumPy 1.9, astype method now returns an error if the string
+ dtype to cast to is not long enough in 'safe' casting mode to hold the max
+ value of integer/float array that is being casted. Previously the casting
+ was allowed even if the result was truncated.
+
Raises
------
ComplexWarning