-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CLN: Assorted cleanups #28563
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
Merged
CLN: Assorted cleanups #28563
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3acb3bc
CLN: assorted cleanups
jbrockmendel dd880ad
Merge branch 'master' of https://github.com/pandas-dev/pandas into fa…
jbrockmendel 72dccaa
Merge branch 'master' of https://github.com/pandas-dev/pandas into fa…
jbrockmendel 7ef8736
Merge branch 'master' of https://github.com/pandas-dev/pandas into fa…
jbrockmendel dbb0098
cleanup
jbrockmendel b09a739
Merge branch 'master' of https://github.com/pandas-dev/pandas into fa…
jbrockmendel 6e6dc96
remove redundant check
jbrockmendel 81b9dbf
mypy fixup
jbrockmendel 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
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
import numpy as np | ||
import numpy.ma as ma | ||
|
||
from pandas._libs import lib, tslibs | ||
from pandas._libs import lib | ||
from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime | ||
|
||
from pandas.core.dtypes.cast import ( | ||
|
@@ -36,7 +36,7 @@ | |
is_timedelta64_ns_dtype, | ||
pandas_dtype, | ||
) | ||
from pandas.core.dtypes.dtypes import ExtensionDtype, registry | ||
from pandas.core.dtypes.dtypes import CategoricalDtype, ExtensionDtype, registry | ||
from pandas.core.dtypes.generic import ( | ||
ABCExtensionArray, | ||
ABCIndexClass, | ||
|
@@ -275,7 +275,7 @@ def array( | |
if inferred_dtype == "period": | ||
try: | ||
return period_array(data, copy=copy) | ||
except tslibs.IncompatibleFrequency: | ||
except IncompatibleFrequency: | ||
# We may have a mixture of frequencies. | ||
# We choose to return an ndarray, rather than raising. | ||
pass | ||
|
@@ -365,7 +365,9 @@ def extract_array(obj, extract_numpy=False): | |
return obj | ||
|
||
|
||
def sanitize_array(data, index, dtype=None, copy=False, raise_cast_failure=False): | ||
def sanitize_array( | ||
data, index, dtype=None, copy: bool = False, raise_cast_failure: bool = False | ||
): | ||
""" | ||
Sanitize input data to an ndarray, copy if specified, coerce to the | ||
dtype if specified. | ||
|
@@ -486,13 +488,19 @@ def sanitize_array(data, index, dtype=None, copy=False, raise_cast_failure=False | |
return subarr | ||
|
||
|
||
def _try_cast(arr, dtype, copy, raise_cast_failure): | ||
def _try_cast( | ||
arr, | ||
dtype: Optional[Union[np.dtype, "ExtensionDtype"]], | ||
copy: bool, | ||
raise_cast_failure: bool, | ||
): | ||
""" | ||
Convert input to numpy ndarray and optionally cast to a given dtype. | ||
|
||
Parameters | ||
---------- | ||
arr : array-like | ||
arr : ndarray, list, tuple, iterator (catchall) | ||
Excludes: ExtensionArray, Series, Index. | ||
dtype : np.dtype, ExtensionDtype or None | ||
copy : bool | ||
If False, don't copy the data if not needed. | ||
|
@@ -528,11 +536,13 @@ def _try_cast(arr, dtype, copy, raise_cast_failure): | |
if is_categorical_dtype(dtype): | ||
# We *do* allow casting to categorical, since we know | ||
# that Categorical is the only array type for 'category'. | ||
dtype = cast(CategoricalDtype, dtype) | ||
subarr = dtype.construct_array_type()( | ||
arr, dtype.categories, ordered=dtype._ordered | ||
) | ||
elif is_extension_array_dtype(dtype): | ||
# create an extension array from its dtype | ||
dtype = cast(ExtensionDtype, dtype) | ||
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. For everyone's edification: why are we doing this? Same for the similar cast a few lines above. 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. This is to keep mypy from complaining |
||
array_type = dtype.construct_array_type()._from_sequence | ||
subarr = array_type(arr, dtype=dtype, copy=copy) | ||
elif dtype is not None and raise_cast_failure: | ||
|
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.
For everyone's edification: why can we remove this check?
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.
is_sparse is a subset of is_extension_array_dtype