Skip to content

Commit 5dc5b1a

Browse files
[ArrayManager] Add Series._as_manager (consolidate with DataFrame) (#40304)
1 parent cb31e5a commit 5dc5b1a

File tree

4 files changed

+64
-26
lines changed

4 files changed

+64
-26
lines changed

pandas/core/frame.py

-20
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
IndexKeyFunc,
6969
IndexLabel,
7070
Level,
71-
Manager,
7271
NpDtype,
7372
PythonFuncType,
7473
Renamer,
@@ -745,25 +744,6 @@ def __init__(
745744

746745
NDFrame.__init__(self, mgr)
747746

748-
def _as_manager(self, typ: str) -> DataFrame:
749-
"""
750-
Private helper function to create a DataFrame with specific manager.
751-
752-
Parameters
753-
----------
754-
typ : {"block", "array"}
755-
756-
Returns
757-
-------
758-
DataFrame
759-
New DataFrame using specified manager type. Is not guaranteed
760-
to be a copy or not.
761-
"""
762-
new_mgr: Manager
763-
new_mgr = mgr_to_mgr(self._mgr, typ=typ)
764-
# fastpath of passing a manager doesn't check the option/manager class
765-
return DataFrame(new_mgr)
766-
767747
# ----------------------------------------------------------------------
768748

769749
@property

pandas/core/generic.py

+19
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,25 @@ def _from_mgr(cls, mgr: Manager):
297297
object.__setattr__(obj, "_attrs", {})
298298
return obj
299299

300+
def _as_manager(self: FrameOrSeries, typ: str) -> FrameOrSeries:
301+
"""
302+
Private helper function to create a DataFrame with specific manager.
303+
304+
Parameters
305+
----------
306+
typ : {"block", "array"}
307+
308+
Returns
309+
-------
310+
DataFrame
311+
New DataFrame using specified manager type. Is not guaranteed
312+
to be a copy or not.
313+
"""
314+
new_mgr: Manager
315+
new_mgr = mgr_to_mgr(self._mgr, typ=typ)
316+
# fastpath of passing a manager doesn't check the option/manager class
317+
return self._constructor(new_mgr).__finalize__(self)
318+
300319
# ----------------------------------------------------------------------
301320
# attrs and flags
302321

pandas/core/internals/construction.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@
7373
get_objs_combined_axis,
7474
union_indexes,
7575
)
76-
from pandas.core.internals.array_manager import ArrayManager
76+
from pandas.core.internals.array_manager import (
77+
ArrayManager,
78+
SingleArrayManager,
79+
)
7780
from pandas.core.internals.blocks import (
7881
ensure_block_shape,
7982
new_block,
8083
)
8184
from pandas.core.internals.managers import (
8285
BlockManager,
86+
SingleBlockManager,
8387
create_block_manager_from_arrays,
8488
create_block_manager_from_blocks,
8589
)
@@ -213,15 +217,21 @@ def mgr_to_mgr(mgr, typ: str):
213217
if isinstance(mgr, BlockManager):
214218
new_mgr = mgr
215219
else:
216-
new_mgr = arrays_to_mgr(
217-
mgr.arrays, mgr.axes[0], mgr.axes[1], mgr.axes[0], typ="block"
218-
)
220+
if mgr.ndim == 2:
221+
new_mgr = arrays_to_mgr(
222+
mgr.arrays, mgr.axes[0], mgr.axes[1], mgr.axes[0], typ="block"
223+
)
224+
else:
225+
new_mgr = SingleBlockManager.from_array(mgr.arrays[0], mgr.index)
219226
elif typ == "array":
220227
if isinstance(mgr, ArrayManager):
221228
new_mgr = mgr
222229
else:
223-
arrays = [mgr.iget_values(i).copy() for i in range(len(mgr.axes[0]))]
224-
new_mgr = ArrayManager(arrays, [mgr.axes[1], mgr.axes[0]])
230+
if mgr.ndim == 2:
231+
arrays = [mgr.iget_values(i).copy() for i in range(len(mgr.axes[0]))]
232+
new_mgr = ArrayManager(arrays, [mgr.axes[1], mgr.axes[0]])
233+
else:
234+
new_mgr = SingleArrayManager([mgr.internal_values()], [mgr.index])
225235
else:
226236
raise ValueError(f"'typ' needs to be one of {{'block', 'array'}}, got '{typ}'")
227237
return new_mgr

pandas/tests/internals/test_managers.py

+29
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from pandas.core.internals import (
99
ArrayManager,
1010
BlockManager,
11+
SingleArrayManager,
12+
SingleBlockManager,
1113
)
1214

1315

@@ -41,3 +43,30 @@ def test_dataframe_creation():
4143
assert isinstance(result._mgr, BlockManager)
4244
tm.assert_frame_equal(result, df_array)
4345
assert len(result._mgr.blocks) == 2
46+
47+
48+
def test_series_creation():
49+
50+
with pd.option_context("mode.data_manager", "block"):
51+
s_block = pd.Series([1, 2, 3], name="A", index=["a", "b", "c"])
52+
assert isinstance(s_block._mgr, SingleBlockManager)
53+
54+
with pd.option_context("mode.data_manager", "array"):
55+
s_array = pd.Series([1, 2, 3], name="A", index=["a", "b", "c"])
56+
assert isinstance(s_array._mgr, SingleArrayManager)
57+
58+
# also ensure both are seen as equal
59+
tm.assert_series_equal(s_block, s_array)
60+
61+
# conversion from one manager to the other
62+
result = s_block._as_manager("block")
63+
assert isinstance(result._mgr, SingleBlockManager)
64+
result = s_block._as_manager("array")
65+
assert isinstance(result._mgr, SingleArrayManager)
66+
tm.assert_series_equal(result, s_block)
67+
68+
result = s_array._as_manager("array")
69+
assert isinstance(result._mgr, SingleArrayManager)
70+
result = s_array._as_manager("block")
71+
assert isinstance(result._mgr, SingleBlockManager)
72+
tm.assert_series_equal(result, s_array)

0 commit comments

Comments
 (0)