Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87a9d5c

Browse files
committed
Merge pull request #3984 from ContinuumIO/promote_types_fix
Fix promote_types for strings and numbers
2 parents 5a44a9f + 1f9d4d2 commit 87a9d5c

7 files changed

Lines changed: 315 additions & 47 deletions

File tree

doc/release/1.9.0-notes.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,30 @@ indexing operations:
179179
* Indexing with more then one ellipsis (`...`) is deprecated.
180180

181181

182+
promote_types and string dtype
183+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184+
promote_types function now returns a valid string length when given an
185+
integer or float dtype as one argument and a string dtype as another argument.
186+
Previously it always returned the input string dtype, even if it wasn't
187+
long enough to store the max integer/float value converted to a string.
188+
189+
190+
can_cast and string dtype
191+
~~~~~~~~~~~~~~~~~~~~~~~~~
192+
can_cast function now returns False in "safe" casting mode for integer/float
193+
dtype and string dtype if the string dtype length is not long enough to store
194+
the max integer/float value converted to a string. Previously can_cast in "safe"
195+
mode returned True for integer/float dtype and a string dtype of any length.
196+
197+
198+
astype and string dtype
199+
~~~~~~~~~~~~~~~~~~~~~~~
200+
astype method now returns an error if the string dtype to cast to is not long
201+
enough in "safe" casting mode to hold the max value of integer/float array that
202+
is being casted. Previously the casting was allowed even if the result was
203+
truncated.
204+
205+
182206
C-API
183207
~~~~~
184208

doc/source/reference/c-api.array.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,10 @@ Converting data types
10361036
the casting rule *casting*. For simple types with :cdata:`NPY_SAFE_CASTING`,
10371037
this is basically a wrapper around :cfunc:`PyArray_CanCastSafely`, but
10381038
for flexible types such as strings or unicode, it produces results
1039-
taking into account their sizes.
1039+
taking into account their sizes. Integer and float types can only be cast
1040+
to a string or unicode type using :cdata:`NPY_SAFE_CASTING` if the string
1041+
or unicode type is big enough to hold the max value of the integer/float
1042+
type being cast from.
10401043

10411044
.. cfunction:: int PyArray_CanCastArrayTo(PyArrayObject* arr, PyArray_Descr* totype, NPY_CASTING casting)
10421045

@@ -1073,7 +1076,8 @@ Converting data types
10731076

10741077
Finds the data type of smallest size and kind to which *type1* and
10751078
*type2* may be safely converted. This function is symmetric and
1076-
associative.
1079+
associative. A string or unicode result will be the proper size for
1080+
storing the max value of the input types converted to a string or unicode.
10771081

10781082
.. cfunction:: PyArray_Descr* PyArray_ResultType(npy_intp narrs, PyArrayObject**arrs, npy_intp ndtypes, PyArray_Descr**dtypes)
10791083

numpy/add_newdocs.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,14 @@ def luf(lamdaexpr, *args, **kwargs):
15981598
out : bool
15991599
True if cast can occur according to the casting rule.
16001600
1601+
Notes
1602+
-----
1603+
Starting in NumPy 1.9, can_cast function now returns False in 'safe'
1604+
casting mode for integer/float dtype and string dtype if the string dtype
1605+
length is not long enough to store the max integer/float value converted
1606+
to a string. Previously can_cast in 'safe' mode returned True for
1607+
integer/float dtype and a string dtype of any length.
1608+
16011609
See also
16021610
--------
16031611
dtype, result_type
@@ -1618,7 +1626,7 @@ def luf(lamdaexpr, *args, **kwargs):
16181626
>>> np.can_cast('i8', 'f4')
16191627
False
16201628
>>> np.can_cast('i4', 'S4')
1621-
True
1629+
False
16221630
16231631
Casting scalars
16241632
@@ -1693,6 +1701,11 @@ def luf(lamdaexpr, *args, **kwargs):
16931701
Notes
16941702
-----
16951703
.. versionadded:: 1.6.0
1704+
Starting in NumPy 1.9, promote_types function now returns a valid string
1705+
length when given an integer or float dtype as one argument and a string
1706+
dtype as another argument. Previously it always returned the input string
1707+
dtype, even if it wasn't long enough to store the max integer/float value
1708+
converted to a string.
16961709
16971710
See Also
16981711
--------
@@ -1709,10 +1722,8 @@ def luf(lamdaexpr, *args, **kwargs):
17091722
>>> np.promote_types('>i8', '<c8')
17101723
dtype('complex128')
17111724
1712-
>>> np.promote_types('i1', 'S8')
1713-
Traceback (most recent call last):
1714-
File "<stdin>", line 1, in <module>
1715-
TypeError: invalid type promotion
1725+
>>> np.promote_types('i4', 'S8')
1726+
dtype('S11')
17161727
17171728
""")
17181729

@@ -3126,6 +3137,13 @@ def luf(lamdaexpr, *args, **kwargs):
31263137
is a new array of the same shape as the input array, with dtype, order
31273138
given by `dtype`, `order`.
31283139
3140+
Notes
3141+
-----
3142+
Starting in NumPy 1.9, astype method now returns an error if the string
3143+
dtype to cast to is not long enough in 'safe' casting mode to hold the max
3144+
value of integer/float array that is being casted. Previously the casting
3145+
was allowed even if the result was truncated.
3146+
31293147
Raises
31303148
------
31313149
ComplexWarning

0 commit comments

Comments
 (0)