-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: select_dypes impl #7434
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
ENH: select_dypes impl #7434
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1603,6 +1603,66 @@ def _get_fill_func(method): | |
#---------------------------------------------------------------------- | ||
# Lots of little utilities | ||
|
||
def _validate_date_like_dtype(dtype): | ||
try: | ||
typ = np.datetime_data(dtype)[0] | ||
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. never knew that was a method like this! 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. yeah only thing is that the second tuple element is 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. you can also just test the kind as M right? ( 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. no i'm checking that the unit is either |
||
except ValueError as e: | ||
raise TypeError('%s' % e) | ||
if typ != 'generic' and typ != 'ns': | ||
raise ValueError('%r is too specific of a frequency, try passing %r' | ||
% (dtype.name, dtype.type.__name__)) | ||
|
||
|
||
def _invalidate_string_dtypes(dtype_set): | ||
"""Change string like dtypes to object for ``DataFrame.select_dtypes()``.""" | ||
non_string_dtypes = dtype_set - _string_dtypes | ||
if non_string_dtypes != dtype_set: | ||
raise TypeError("string dtypes are not allowed, use 'object' instead") | ||
|
||
|
||
def _get_dtype_from_object(dtype): | ||
"""Get a numpy dtype.type-style object. | ||
Notes | ||
----- | ||
If nothing can be found, returns ``object``. | ||
""" | ||
# type object from a dtype | ||
if isinstance(dtype, type) and issubclass(dtype, np.generic): | ||
return dtype | ||
elif isinstance(dtype, np.dtype): # dtype object | ||
try: | ||
_validate_date_like_dtype(dtype) | ||
except TypeError: | ||
# should still pass if we don't have a datelike | ||
pass | ||
return dtype.type | ||
elif isinstance(dtype, compat.string_types): | ||
if dtype == 'datetime' or dtype == 'timedelta': | ||
dtype += '64' | ||
try: | ||
return _get_dtype_from_object(getattr(np, dtype)) | ||
except AttributeError: | ||
# handles cases like _get_dtype(int) | ||
# i.e., python objects that are valid dtypes (unlike user-defined | ||
# types, in general) | ||
pass | ||
return _get_dtype_from_object(np.dtype(dtype)) | ||
|
||
|
||
_string_dtypes = frozenset(map(_get_dtype_from_object, (compat.binary_type, | ||
compat.text_type))) | ||
|
||
|
||
def _get_info_slice(obj, indexer): | ||
"""Slice the info axis of `obj` with `indexer`.""" | ||
if not hasattr(obj, '_info_axis_number'): | ||
raise TypeError('object of type %r has no info axis' % | ||
type(obj).__name__) | ||
slices = [slice(None)] * obj.ndim | ||
slices[obj._info_axis_number] = indexer | ||
return tuple(slices) | ||
|
||
|
||
def _maybe_box(indexer, values, obj, key): | ||
|
||
|
@@ -1613,6 +1673,7 @@ def _maybe_box(indexer, values, obj, key): | |
# return the value | ||
return values | ||
|
||
|
||
def _maybe_box_datetimelike(value): | ||
# turn a datetime like into a Timestamp/timedelta as needed | ||
|
||
|
@@ -1797,6 +1858,7 @@ def _possibly_cast_to_datetime(value, dtype, coerce=False): | |
|
||
return value | ||
|
||
|
||
def _possibly_infer_to_datetimelike(value): | ||
# we might have a array (or single object) that is datetime like, | ||
# and no dtype is passed don't change the value unless we find a | ||
|
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.
can you add a
See Docs
link?