Skip to content

[ArrayManager] Add Series._as_manager (consolidate with DataFrame) #40304

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 2 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
IndexKeyFunc,
IndexLabel,
Level,
Manager,
NpDtype,
PythonFuncType,
Renamer,
Expand Down Expand Up @@ -775,25 +774,6 @@ def __init__(

NDFrame.__init__(self, mgr)

def _as_manager(self, typ: str) -> DataFrame:
"""
Private helper function to create a DataFrame with specific manager.

Parameters
----------
typ : {"block", "array"}

Returns
-------
DataFrame
New DataFrame using specified manager type. Is not guaranteed
to be a copy or not.
"""
new_mgr: Manager
new_mgr = mgr_to_mgr(self._mgr, typ=typ)
# fastpath of passing a manager doesn't check the option/manager class
return DataFrame(new_mgr)

# ----------------------------------------------------------------------

@property
Expand Down
19 changes: 19 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,25 @@ def _from_mgr(cls, mgr: Manager):
object.__setattr__(obj, "_attrs", {})
return obj

def _as_manager(self: FrameOrSeries, typ: str) -> FrameOrSeries:
"""
Private helper function to create a DataFrame with specific manager.

Parameters
----------
typ : {"block", "array"}

Returns
-------
DataFrame
New DataFrame using specified manager type. Is not guaranteed
to be a copy or not.
"""
new_mgr: Manager
new_mgr = mgr_to_mgr(self._mgr, typ=typ)
# fastpath of passing a manager doesn't check the option/manager class
return self._constructor(new_mgr).__finalize__(self)

# ----------------------------------------------------------------------
# attrs and flags

Expand Down
22 changes: 16 additions & 6 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,17 @@
get_objs_combined_axis,
union_indexes,
)
from pandas.core.internals.array_manager import ArrayManager
from pandas.core.internals.array_manager import (
ArrayManager,
SingleArrayManager,
)
from pandas.core.internals.blocks import (
ensure_block_shape,
new_block,
)
from pandas.core.internals.managers import (
BlockManager,
SingleBlockManager,
create_block_manager_from_arrays,
create_block_manager_from_blocks,
)
Expand Down Expand Up @@ -212,15 +216,21 @@ def mgr_to_mgr(mgr, typ: str):
if isinstance(mgr, BlockManager):
new_mgr = mgr
else:
new_mgr = arrays_to_mgr(
mgr.arrays, mgr.axes[0], mgr.axes[1], mgr.axes[0], typ="block"
)
if mgr.ndim == 2:
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 make elif/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.

I personally wouldn't find that an improvement in readability. Now it's very clearly separated into two cases (manager already has the same type -> use as is, or else the manager needs to be converted)

Copy link
Contributor

Choose a reason for hiding this comment

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

using nested elif/if is an anti-pattern

Copy link
Member Author

Choose a reason for hiding this comment

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

It's only an anti-pattern if it's deeply nested and hampering readability. In this case, it makes the code easier to understand IMO.

new_mgr = arrays_to_mgr(
mgr.arrays, mgr.axes[0], mgr.axes[1], mgr.axes[0], typ="block"
)
else:
new_mgr = SingleBlockManager.from_array(mgr.arrays[0], mgr.index)
elif typ == "array":
if isinstance(mgr, ArrayManager):
new_mgr = mgr
else:
arrays = [mgr.iget_values(i).copy() for i in range(len(mgr.axes[0]))]
new_mgr = ArrayManager(arrays, [mgr.axes[1], mgr.axes[0]])
if mgr.ndim == 2:
Copy link
Contributor

Choose a reason for hiding this comment

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

same

arrays = [mgr.iget_values(i).copy() for i in range(len(mgr.axes[0]))]
new_mgr = ArrayManager(arrays, [mgr.axes[1], mgr.axes[0]])
else:
new_mgr = SingleArrayManager([mgr.internal_values()], [mgr.index])
else:
raise ValueError(f"'typ' needs to be one of {{'block', 'array'}}, got '{typ}'")
return new_mgr
Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/internals/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pandas.core.internals import (
ArrayManager,
BlockManager,
SingleArrayManager,
SingleBlockManager,
)


Expand Down Expand Up @@ -41,3 +43,30 @@ def test_dataframe_creation():
assert isinstance(result._mgr, BlockManager)
tm.assert_frame_equal(result, df_array)
assert len(result._mgr.blocks) == 2


def test_series_creation():

with pd.option_context("mode.data_manager", "block"):
s_block = pd.Series([1, 2, 3], name="A", index=["a", "b", "c"])
assert isinstance(s_block._mgr, SingleBlockManager)

with pd.option_context("mode.data_manager", "array"):
s_array = pd.Series([1, 2, 3], name="A", index=["a", "b", "c"])
assert isinstance(s_array._mgr, SingleArrayManager)

# also ensure both are seen as equal
tm.assert_series_equal(s_block, s_array)

# conversion from one manager to the other
result = s_block._as_manager("block")
assert isinstance(result._mgr, SingleBlockManager)
result = s_block._as_manager("array")
assert isinstance(result._mgr, SingleArrayManager)
tm.assert_series_equal(result, s_block)

result = s_array._as_manager("array")
assert isinstance(result._mgr, SingleArrayManager)
result = s_array._as_manager("block")
assert isinstance(result._mgr, SingleBlockManager)
tm.assert_series_equal(result, s_array)