-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
REF: simplify BlockManager.quantile #39618
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
Changes from 1 commit
Commits
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
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 |
---|---|---|
|
@@ -31,7 +31,6 @@ | |
is_extension_array_dtype, | ||
is_list_like, | ||
) | ||
from pandas.core.dtypes.concat import concat_compat | ||
from pandas.core.dtypes.dtypes import ExtensionDtype | ||
from pandas.core.dtypes.generic import ABCDataFrame, ABCPandasArray, ABCSeries | ||
from pandas.core.dtypes.missing import array_equals, isna | ||
|
@@ -40,7 +39,7 @@ | |
from pandas.core.arrays.sparse import SparseDtype | ||
from pandas.core.construction import extract_array | ||
from pandas.core.indexers import maybe_convert_indices | ||
from pandas.core.indexes.api import Index, ensure_index | ||
from pandas.core.indexes.api import Float64Index, Index, ensure_index | ||
from pandas.core.internals.base import DataManager | ||
from pandas.core.internals.blocks import ( | ||
Block, | ||
|
@@ -445,7 +444,6 @@ def quantile( | |
transposed: bool = False, | ||
interpolation="linear", | ||
qs=None, | ||
numeric_only=None, | ||
) -> BlockManager: | ||
""" | ||
Iterate over blocks applying quantile reduction. | ||
|
@@ -460,8 +458,7 @@ def quantile( | |
transposed: bool, default False | ||
we are holding transposed data | ||
interpolation : type of interpolation, default 'linear' | ||
qs : a scalar or list of the quantiles to be computed | ||
numeric_only : ignored | ||
qs : list of the quantiles to be computed | ||
|
||
Returns | ||
------- | ||
|
@@ -470,73 +467,26 @@ def quantile( | |
# Series dispatches to DataFrame for quantile, which allows us to | ||
# simplify some of the code here and in the blocks | ||
assert self.ndim >= 2 | ||
assert is_list_like(qs) # caller is responsible for this | ||
assert axis == 1 # only ever called this way | ||
|
||
def get_axe(block, qs, axes): | ||
# Because Series dispatches to DataFrame, we will always have | ||
# block.ndim == 2 | ||
from pandas import Float64Index | ||
|
||
if is_list_like(qs): | ||
ax = Float64Index(qs) | ||
else: | ||
ax = axes[0] | ||
return ax | ||
qs_axe = Float64Index(qs) | ||
new_axes = list(self.axes) | ||
new_axes[1] = qs_axe | ||
|
||
axes, blocks = [], [] | ||
blocks = [] | ||
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. can you make a list comprehension 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. sure |
||
for b in self.blocks: | ||
block = b.quantile(axis=axis, qs=qs, interpolation=interpolation) | ||
|
||
axe = get_axe(b, qs, axes=self.axes) | ||
|
||
axes.append(axe) | ||
blocks.append(block) | ||
|
||
# note that some DatetimeTZ, Categorical are always ndim==1 | ||
ndim = {b.ndim for b in blocks} | ||
assert 0 not in ndim, ndim | ||
|
||
if 2 in ndim: | ||
|
||
new_axes = list(self.axes) | ||
|
||
# multiple blocks that are reduced | ||
if len(blocks) > 1: | ||
new_axes[1] = axes[0] | ||
|
||
# reset the placement to the original | ||
for b, sb in zip(blocks, self.blocks): | ||
b.mgr_locs = sb.mgr_locs | ||
|
||
else: | ||
new_axes[axis] = Index(np.concatenate([ax._values for ax in axes])) | ||
|
||
if transposed: | ||
new_axes = new_axes[::-1] | ||
blocks = [ | ||
b.make_block(b.values.T, placement=np.arange(b.shape[1])) | ||
for b in blocks | ||
] | ||
|
||
return type(self)(blocks, new_axes) | ||
|
||
# single block, i.e. ndim == {1} | ||
values = concat_compat([b.values for b in blocks]) | ||
|
||
# compute the orderings of our original data | ||
if len(self.blocks) > 1: | ||
|
||
indexer = np.empty(len(self.axes[0]), dtype=np.intp) | ||
i = 0 | ||
for b in self.blocks: | ||
for j in b.mgr_locs: | ||
indexer[j] = i | ||
i = i + 1 | ||
|
||
values = values.take(indexer) | ||
if transposed: | ||
new_axes = new_axes[::-1] | ||
blocks = [ | ||
b.make_block(b.values.T, placement=np.arange(b.shape[1])) | ||
for b in blocks | ||
] | ||
|
||
return SingleBlockManager( | ||
make_block(values, ndim=1, placement=np.arange(len(values))), axes[0] | ||
) | ||
return type(self)(blocks, new_axes) | ||
|
||
def isna(self, func) -> BlockManager: | ||
return self.apply("apply", func=func) | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.