-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Make maybe_convert_object respect dtype itemsize #40908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jreback
merged 14 commits into
pandas-dev:master
from
rhshadrach:maybe_convert_object_itemsize
Apr 21, 2021
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
314ed71
ENH: Make maybe_convert_object respect dtype itemsize
rhshadrach a8008d1
fixup
rhshadrach 83c3ab5
fixup
rhshadrach 64086a4
Move get_itemsize, use dtype.itemsize, safer casting
rhshadrach 24044de
Fix Series.count and tests
rhshadrach 127158a
Merge branch 'master' of https://github.com/pandas-dev/pandas into ma…
rhshadrach 5950138
WIP
rhshadrach 45f5f31
Added test
rhshadrach 8e1e7c2
Fixed replace
rhshadrach f13edc0
Remove higher itemsize for earlier versions of numpy
rhshadrach 5d3ffe0
fixup
rhshadrach c128896
np_version_under1p19 -> np_version_under1p20
rhshadrach c03fbf6
whatsnew and comment
rhshadrach 968942c
Merge branch 'master' into maybe_convert_object_itemsize
jreback File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,7 @@ from pandas._libs.util cimport ( | |
INT64_MAX, | ||
INT64_MIN, | ||
UINT64_MAX, | ||
get_itemsize, | ||
is_nan, | ||
) | ||
|
||
|
@@ -2187,7 +2188,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False, | |
|
||
Parameters | ||
---------- | ||
values : ndarray[object] | ||
objects : ndarray[object] | ||
Array of object elements to convert. | ||
try_float : bool, default False | ||
If an array-like object contains only float or NaN values is | ||
|
@@ -2211,7 +2212,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False, | |
Array of converted object values to more specific dtypes if applicable. | ||
""" | ||
cdef: | ||
Py_ssize_t i, n | ||
Py_ssize_t i, n, itemsize_max = 0 | ||
ndarray[float64_t] floats | ||
ndarray[complex128_t] complexes | ||
ndarray[int64_t] ints | ||
|
@@ -2244,6 +2245,10 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False, | |
|
||
for i in range(n): | ||
val = objects[i] | ||
if itemsize_max != -1: | ||
itemsize = get_itemsize(val) | ||
if itemsize > itemsize_max or itemsize == -1: | ||
itemsize_max = itemsize | ||
|
||
if val is None: | ||
seen.null_ = True | ||
|
@@ -2345,92 +2350,100 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=False, | |
seen.object_ = True | ||
|
||
if not seen.object_: | ||
result = None | ||
if not safe: | ||
if seen.null_ or seen.nan_: | ||
if seen.is_float_or_complex: | ||
if seen.complex_: | ||
return complexes | ||
result = complexes | ||
elif seen.float_: | ||
return floats | ||
result = floats | ||
elif seen.int_: | ||
if convert_to_nullable_integer: | ||
from pandas.core.arrays import IntegerArray | ||
return IntegerArray(ints, mask) | ||
result = IntegerArray(ints, mask) | ||
itemsize_max = -1 | ||
else: | ||
return floats | ||
result = floats | ||
elif seen.nan_: | ||
return floats | ||
result = floats | ||
else: | ||
if not seen.bool_: | ||
if seen.datetime_: | ||
if not seen.numeric_ and not seen.timedelta_: | ||
return datetimes | ||
result = datetimes | ||
elif seen.timedelta_: | ||
if not seen.numeric_: | ||
return timedeltas | ||
result = timedeltas | ||
elif seen.nat_: | ||
if not seen.numeric_: | ||
if convert_datetime and convert_timedelta: | ||
# TODO: array full of NaT ambiguity resolve here needed | ||
pass | ||
elif convert_datetime: | ||
return datetimes | ||
result = datetimes | ||
elif convert_timedelta: | ||
return timedeltas | ||
result = timedeltas | ||
else: | ||
if seen.complex_: | ||
return complexes | ||
result = complexes | ||
elif seen.float_: | ||
return floats | ||
result = floats | ||
elif seen.int_: | ||
if seen.uint_: | ||
return uints | ||
result = uints | ||
else: | ||
return ints | ||
result = ints | ||
elif seen.is_bool: | ||
return bools.view(np.bool_) | ||
result = bools.view(np.bool_) | ||
|
||
else: | ||
# don't cast int to float, etc. | ||
if seen.null_: | ||
if seen.is_float_or_complex: | ||
if seen.complex_: | ||
if not seen.int_: | ||
return complexes | ||
result = complexes | ||
elif seen.float_ or seen.nan_: | ||
if not seen.int_: | ||
return floats | ||
result = floats | ||
else: | ||
if not seen.bool_: | ||
if seen.datetime_: | ||
if not seen.numeric_ and not seen.timedelta_: | ||
return datetimes | ||
result = datetimes | ||
elif seen.timedelta_: | ||
if not seen.numeric_: | ||
return timedeltas | ||
result = timedeltas | ||
elif seen.nat_: | ||
if not seen.numeric_: | ||
if convert_datetime and convert_timedelta: | ||
# TODO: array full of NaT ambiguity resolve here needed | ||
pass | ||
elif convert_datetime: | ||
return datetimes | ||
result = datetimes | ||
elif convert_timedelta: | ||
return timedeltas | ||
result = timedeltas | ||
else: | ||
if seen.complex_: | ||
if not seen.int_: | ||
return complexes | ||
result = complexes | ||
elif seen.float_ or seen.nan_: | ||
if not seen.int_: | ||
return floats | ||
result = floats | ||
elif seen.int_: | ||
if seen.uint_: | ||
return uints | ||
result = uints | ||
else: | ||
return ints | ||
result = ints | ||
elif seen.is_bool and not seen.nan_: | ||
return bools.view(np.bool_) | ||
result = bools.view(np.bool_) | ||
if result is not None: | ||
if itemsize_max > 0: | ||
curr_itemsize = cnp.PyArray_ITEMSIZE(result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. id just use |
||
if itemsize_max != curr_itemsize: | ||
result = result.astype(result.dtype.kind + str(itemsize_max)) | ||
return result | ||
|
||
return objects | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
|
||
cimport numpy as cnp | ||
from cpython.object cimport PyTypeObject | ||
from numpy cimport PyArray_DescrFromScalar | ||
|
||
cnp.import_array() | ||
|
||
|
||
cdef extern from *: | ||
|
@@ -44,6 +48,7 @@ cdef extern from "numpy/ndarrayobject.h": | |
|
||
bint PyArray_IsIntegerScalar(obj) nogil | ||
bint PyArray_Check(obj) nogil | ||
bint PyArray_CheckScalar(obj) nogil | ||
|
||
cdef extern from "numpy/npy_common.h": | ||
int64_t NPY_MIN_INT64 | ||
|
@@ -195,6 +200,24 @@ cdef inline bint is_nan(object val): | |
return is_complex_object(val) and val != val | ||
|
||
|
||
cdef inline int64_t get_itemsize(object val): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if this is only used in lib.pyx, i think better to put it there |
||
""" | ||
Get the itemsize of a NumPy scalar, -1 if not a NumPy scalar. | ||
|
||
Parameters | ||
---------- | ||
val : object | ||
|
||
Returns | ||
------- | ||
is_ndarray : bool | ||
""" | ||
if PyArray_CheckScalar(val): | ||
return PyArray_DescrFromScalar(val).itemsize | ||
else: | ||
return -1 | ||
|
||
|
||
cdef inline const char* get_c_string_buf_and_size(str py_string, | ||
Py_ssize_t *length) except NULL: | ||
""" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be tightened to something like
result is floats or result is uints or result is ints
? i.e. exclude datetimes/timedeltas/bools