Skip to content

[ArrayManager] Implement concat with axis=1 (merge/join) #39841

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

Conversation

jorisvandenbossche
Copy link
Member

The (hopefully) "easy" part of #39612 for which there seems less discussion: this separates the pieces from #39612 that are needed to get concat with axis=1 working (concat_axis=0), which at least enables the join / merge usecases.

@jorisvandenbossche jorisvandenbossche changed the title [ArrayManager] ENH: implement concat with axis=1 (merge/join) [ArrayManager] Implement concat with axis=1 (merge/join) Feb 16, 2021
@@ -153,6 +153,7 @@ jobs:
run: |
source activate pandas-dev
pytest pandas/tests/frame/methods --array-manager
pytest pandas/tests/reshape/merge --array-manager
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only reshape/merge and not reshape/concat, as there are too many tests still failing in the concat tests (because axis=0 is not yet handled properly)

mgrs.append(mgr)

# concatting along the rows -> concat the reindexed arrays
# TODO(ArrayManager) doesn't yet preserve the correct dtype
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this comment apply to both cases or just concat_axis==1? (this ambiguity is why i like to put comments inside below the "if"/"else")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the comment (yes, it applies only to the axis==1)

for arr in data1._mgr.arrays:
arr.flags.writeable = False
else:
data1._mgr.blocks[0].values.flags.writeable = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could implement BlockManager.arrays as a property yielding the values for compat

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but that would not help for this specific case (as those arrays would not be the actual arrays stored in the BlockManager, and setting the flag wouldn't have an impact on the actual values).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as those arrays would not be the actual arrays stored in the BlockManager

@property
def arrays(self):
   for blk in self.blocks:
       yield blk.values

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, like that. I wouldn't find it great API design, as the same attribute/property would return something quite different (at least what you can do with it is different), but I am fine adding it for testing convenience

Copy link
Member Author

@jorisvandenbossche jorisvandenbossche Feb 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added + test simplified

@jbrockmendel
Copy link
Member

small comments, generally looks good

@@ -754,10 +754,12 @@ def reindex_indexer(
# ignored keywords
consolidate: bool = True,
only_slice: bool = False,
# ArrayManager specific keywords
do_integrity_check: bool = True,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we call this integrity_check elsewhere

Copy link
Member Author

@jorisvandenbossche jorisvandenbossche Feb 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BlockManager/ArrayManager constructors call this do_integrity_check, so it's consistent with that. We don't have integrity_check, but we do have verify_integrity elsewhere (and this is actually used more, also in public APIs).

I think it might be good to change do_integrity_check to verify_integrity, but given that currently do_integrity_check is consistently used within the (already existing) internals code, that's something for a different PR, I would say.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right that's what i mean. can you normalize all to verify_integrity

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you normalize all to verify_integrity

Since that's not strictly related to concat or ArrayManager, will do that in a follow-up PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> #39989

arrays = list(itertools.chain.from_iterable([mgr.arrays for mgr in mgrs]))
return ArrayManager(arrays, [axes[1], axes[0]], do_integrity_check=False)


def concatenate_block_managers(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you at a higher level call concatenate_array_managers instead? (e.g. instead of dispatching thru the BM)

Copy link
Member Author

@jorisvandenbossche jorisvandenbossche Feb 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would require adding if/else checking the manager type in each of the places where currently concatenate_array_managers is called (3 places), so that would not improve the code there.

If you want I can create a new concatenate_managers in this file that simply does the dispatch to concatenate_block_managers / concatenate_array_managers, and call that instead of concatenate_block_managers outside of the /internals

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok maybe rename this then to concatenate_managers? to make it much more obvious.

@jreback jreback added Internals Related to non-user accessible pandas implementation Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Feb 17, 2021
@jreback
Copy link
Contributor

jreback commented Feb 17, 2021

OT should we create a Internal - ArrayManager label?

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small comments

@@ -1381,7 +1391,9 @@ def test_merge_readonly(self):
np.arange(20).reshape((5, 4)) + 1, columns=["a", "b", "x", "y"]
)

data1._mgr.blocks[0].values.flags.writeable = False
for arr in data1._mgr.arrays:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cabn you comment on this (I know what you are doing by seeing what you added, but is non-obvious here i think)

@jorisvandenbossche
Copy link
Member Author

The windows failure is also happening on master, and the linux failure is a flaky resource warning, so both unrelated.

Thanks for the reviews! Going to merge now, and will do the verify_integrity rename in a follow-up.

@@ -282,6 +282,18 @@ def get_dtypes(self):
dtypes = np.array([blk.dtype for blk in self.blocks])
return algos.take_nd(dtypes, self.blknos, allow_fill=False)

@property
def arrays(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

annotate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Internals Related to non-user accessible pandas implementation Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants