-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
CLN: BlockManager.apply #29825
Merged
Merged
CLN: BlockManager.apply #29825
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f1a1a03
CLN: astype
jbrockmendel c164a51
CLN: remove redundant convert method
jbrockmendel e30176f
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
jbrockmendel 35d6804
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
jbrockmendel 6aef8ed
CLN: BlockManager.apply
jbrockmendel 05ace2e
CLN: appyl signature
jbrockmendel 9a6401b
CLN: BlockManager.copy
jbrockmendel 22e2808
blackify
jbrockmendel 6f5591c
mypy fixups
jbrockmendel 40b8c7b
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
jbrockmendel cb95d64
make copy_func non-lambda
jbrockmendel 100ebf2
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
jbrockmendel b8bca08
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
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 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 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 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 |
---|---|---|
|
@@ -343,33 +343,20 @@ def _verify_integrity(self): | |
"tot_items: {1}".format(len(self.items), tot_items) | ||
) | ||
|
||
def apply( | ||
self, | ||
f, | ||
axes=None, | ||
filter=None, | ||
do_integrity_check=False, | ||
consolidate=True, | ||
**kwargs, | ||
): | ||
def apply(self, f: str, filter=None, **kwargs): | ||
""" | ||
iterate over the blocks, collect and create a new block manager | ||
Iterate over the blocks, collect and create a new BlockManager. | ||
|
||
Parameters | ||
---------- | ||
f : the callable or function name to operate on at the block level | ||
axes : optional (if not supplied, use self.axes) | ||
f : str | ||
Name of the Block method to apply. | ||
filter : list, if supplied, only call the block if the filter is in | ||
the block | ||
do_integrity_check : boolean, default False. Do the block manager | ||
integrity check | ||
consolidate: boolean, default True. Join together blocks having same | ||
dtype | ||
|
||
Returns | ||
------- | ||
Block Manager (new object) | ||
|
||
BlockManager | ||
""" | ||
|
||
result_blocks = [] | ||
|
@@ -383,8 +370,7 @@ def apply( | |
else: | ||
kwargs["filter"] = filter_locs | ||
|
||
if consolidate: | ||
self._consolidate_inplace() | ||
self._consolidate_inplace() | ||
|
||
if f == "where": | ||
align_copy = True | ||
|
@@ -432,10 +418,8 @@ def apply( | |
result_blocks = _extend_blocks(applied, result_blocks) | ||
|
||
if len(result_blocks) == 0: | ||
return self.make_empty(axes or self.axes) | ||
bm = self.__class__( | ||
result_blocks, axes or self.axes, do_integrity_check=do_integrity_check | ||
) | ||
return self.make_empty(self.axes) | ||
bm = self.__class__(result_blocks, self.axes, do_integrity_check=False) | ||
topper-123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bm._consolidate_inplace() | ||
return bm | ||
|
||
|
@@ -543,8 +527,8 @@ def get_axe(block, qs, axes): | |
[make_block(values, ndim=1, placement=np.arange(len(values)))], axes[0] | ||
) | ||
|
||
def isna(self, func, **kwargs): | ||
return self.apply("apply", func=func, **kwargs) | ||
def isna(self, func): | ||
return self.apply("apply", func=func) | ||
|
||
def where(self, **kwargs): | ||
return self.apply("where", **kwargs) | ||
|
@@ -570,8 +554,8 @@ def fillna(self, **kwargs): | |
def downcast(self, **kwargs): | ||
return self.apply("downcast", **kwargs) | ||
|
||
def astype(self, dtype, **kwargs): | ||
return self.apply("astype", dtype=dtype, **kwargs) | ||
def astype(self, dtype, copy: bool = False, errors: str = "raise"): | ||
return self.apply("astype", dtype=dtype, copy=copy, errors=errors) | ||
|
||
def convert(self, **kwargs): | ||
return self.apply("convert", **kwargs) | ||
|
@@ -772,13 +756,15 @@ def copy(self, deep=True): | |
# this preserves the notion of view copying of axes | ||
if deep: | ||
if deep == "all": | ||
copy = lambda ax: ax.copy(deep=True) | ||
copy_func = lambda ax: ax.copy(deep=True) | ||
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. Could you make this a named function? Same below. |
||
else: | ||
copy = lambda ax: ax.view() | ||
new_axes = [copy(ax) for ax in self.axes] | ||
copy_func = lambda ax: ax.view() | ||
new_axes = [copy_func(ax) for ax in self.axes] | ||
else: | ||
new_axes = list(self.axes) | ||
return self.apply("copy", axes=new_axes, deep=deep, do_integrity_check=False) | ||
res = self.apply("copy", deep=deep) | ||
res.axes = new_axes | ||
return res | ||
|
||
def as_array(self, transpose=False, items=None): | ||
"""Convert the blockmanager data into an numpy array. | ||
|
@@ -1532,10 +1518,6 @@ def get_slice(self, slobj, axis=0): | |
def index(self): | ||
return self.axes[0] | ||
|
||
def convert(self, **kwargs): | ||
""" convert the whole block as one """ | ||
return self.apply("convert", **kwargs) | ||
|
||
@property | ||
def dtype(self): | ||
return self._block.dtype | ||
|
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.
did you mean to use bool_t here?
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.
Yes. We can't use
bool
here because NDFrame defines bool, see note on L142There 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.
ok, interesting