Skip to content

Commit 02e005d

Browse files
jbrockmendelyeshsurya
authored andcommitted
REF: move DataFrame-specific methods from NDFrame (pandas-dev#41200)
1 parent 05a7672 commit 02e005d

File tree

5 files changed

+426
-407
lines changed

5 files changed

+426
-407
lines changed

pandas/core/frame.py

+101
Original file line numberDiff line numberDiff line change
@@ -10454,6 +10454,107 @@ def _AXIS_NAMES(self) -> dict[int, str]:
1045410454
boxplot = pandas.plotting.boxplot_frame
1045510455
sparse = CachedAccessor("sparse", SparseFrameAccessor)
1045610456

10457+
# ----------------------------------------------------------------------
10458+
# Internal Interface Methods
10459+
10460+
def _to_dict_of_blocks(self, copy: bool = True):
10461+
"""
10462+
Return a dict of dtype -> Constructor Types that
10463+
each is a homogeneous dtype.
10464+
10465+
Internal ONLY - only works for BlockManager
10466+
"""
10467+
mgr = self._mgr
10468+
# convert to BlockManager if needed -> this way support ArrayManager as well
10469+
mgr = mgr_to_mgr(mgr, "block")
10470+
mgr = cast(BlockManager, mgr)
10471+
return {
10472+
k: self._constructor(v).__finalize__(self)
10473+
for k, v, in mgr.to_dict(copy=copy).items()
10474+
}
10475+
10476+
@property
10477+
def values(self) -> np.ndarray:
10478+
"""
10479+
Return a Numpy representation of the DataFrame.
10480+
10481+
.. warning::
10482+
10483+
We recommend using :meth:`DataFrame.to_numpy` instead.
10484+
10485+
Only the values in the DataFrame will be returned, the axes labels
10486+
will be removed.
10487+
10488+
Returns
10489+
-------
10490+
numpy.ndarray
10491+
The values of the DataFrame.
10492+
10493+
See Also
10494+
--------
10495+
DataFrame.to_numpy : Recommended alternative to this method.
10496+
DataFrame.index : Retrieve the index labels.
10497+
DataFrame.columns : Retrieving the column names.
10498+
10499+
Notes
10500+
-----
10501+
The dtype will be a lower-common-denominator dtype (implicit
10502+
upcasting); that is to say if the dtypes (even of numeric types)
10503+
are mixed, the one that accommodates all will be chosen. Use this
10504+
with care if you are not dealing with the blocks.
10505+
10506+
e.g. If the dtypes are float16 and float32, dtype will be upcast to
10507+
float32. If dtypes are int32 and uint8, dtype will be upcast to
10508+
int32. By :func:`numpy.find_common_type` convention, mixing int64
10509+
and uint64 will result in a float64 dtype.
10510+
10511+
Examples
10512+
--------
10513+
A DataFrame where all columns are the same type (e.g., int64) results
10514+
in an array of the same type.
10515+
10516+
>>> df = pd.DataFrame({'age': [ 3, 29],
10517+
... 'height': [94, 170],
10518+
... 'weight': [31, 115]})
10519+
>>> df
10520+
age height weight
10521+
0 3 94 31
10522+
1 29 170 115
10523+
>>> df.dtypes
10524+
age int64
10525+
height int64
10526+
weight int64
10527+
dtype: object
10528+
>>> df.values
10529+
array([[ 3, 94, 31],
10530+
[ 29, 170, 115]])
10531+
10532+
A DataFrame with mixed type columns(e.g., str/object, int64, float32)
10533+
results in an ndarray of the broadest type that accommodates these
10534+
mixed types (e.g., object).
10535+
10536+
>>> df2 = pd.DataFrame([('parrot', 24.0, 'second'),
10537+
... ('lion', 80.5, 1),
10538+
... ('monkey', np.nan, None)],
10539+
... columns=('name', 'max_speed', 'rank'))
10540+
>>> df2.dtypes
10541+
name object
10542+
max_speed float64
10543+
rank object
10544+
dtype: object
10545+
>>> df2.values
10546+
array([['parrot', 24.0, 'second'],
10547+
['lion', 80.5, 1],
10548+
['monkey', nan, None]], dtype=object)
10549+
"""
10550+
self._consolidate_inplace()
10551+
return self._mgr.as_array(transpose=True)
10552+
10553+
@property
10554+
def _values(self) -> np.ndarray:
10555+
"""internal implementation"""
10556+
return self.values
10557+
1045710558

1045810559
DataFrame._add_numeric_operations()
1045910560

pandas/core/generic.py

+2-92
Original file line numberDiff line numberDiff line change
@@ -5614,85 +5614,12 @@ def _get_bool_data(self):
56145614

56155615
@property
56165616
def values(self) -> np.ndarray:
5617-
"""
5618-
Return a Numpy representation of the DataFrame.
5619-
5620-
.. warning::
5621-
5622-
We recommend using :meth:`DataFrame.to_numpy` instead.
5623-
5624-
Only the values in the DataFrame will be returned, the axes labels
5625-
will be removed.
5626-
5627-
Returns
5628-
-------
5629-
numpy.ndarray
5630-
The values of the DataFrame.
5631-
5632-
See Also
5633-
--------
5634-
DataFrame.to_numpy : Recommended alternative to this method.
5635-
DataFrame.index : Retrieve the index labels.
5636-
DataFrame.columns : Retrieving the column names.
5637-
5638-
Notes
5639-
-----
5640-
The dtype will be a lower-common-denominator dtype (implicit
5641-
upcasting); that is to say if the dtypes (even of numeric types)
5642-
are mixed, the one that accommodates all will be chosen. Use this
5643-
with care if you are not dealing with the blocks.
5644-
5645-
e.g. If the dtypes are float16 and float32, dtype will be upcast to
5646-
float32. If dtypes are int32 and uint8, dtype will be upcast to
5647-
int32. By :func:`numpy.find_common_type` convention, mixing int64
5648-
and uint64 will result in a float64 dtype.
5649-
5650-
Examples
5651-
--------
5652-
A DataFrame where all columns are the same type (e.g., int64) results
5653-
in an array of the same type.
5654-
5655-
>>> df = pd.DataFrame({'age': [ 3, 29],
5656-
... 'height': [94, 170],
5657-
... 'weight': [31, 115]})
5658-
>>> df
5659-
age height weight
5660-
0 3 94 31
5661-
1 29 170 115
5662-
>>> df.dtypes
5663-
age int64
5664-
height int64
5665-
weight int64
5666-
dtype: object
5667-
>>> df.values
5668-
array([[ 3, 94, 31],
5669-
[ 29, 170, 115]])
5670-
5671-
A DataFrame with mixed type columns(e.g., str/object, int64, float32)
5672-
results in an ndarray of the broadest type that accommodates these
5673-
mixed types (e.g., object).
5674-
5675-
>>> df2 = pd.DataFrame([('parrot', 24.0, 'second'),
5676-
... ('lion', 80.5, 1),
5677-
... ('monkey', np.nan, None)],
5678-
... columns=('name', 'max_speed', 'rank'))
5679-
>>> df2.dtypes
5680-
name object
5681-
max_speed float64
5682-
rank object
5683-
dtype: object
5684-
>>> df2.values
5685-
array([['parrot', 24.0, 'second'],
5686-
['lion', 80.5, 1],
5687-
['monkey', nan, None]], dtype=object)
5688-
"""
5689-
self._consolidate_inplace()
5690-
return self._mgr.as_array(transpose=self._AXIS_REVERSED)
5617+
raise AbstractMethodError(self)
56915618

56925619
@property
56935620
def _values(self) -> np.ndarray:
56945621
"""internal implementation"""
5695-
return self.values
5622+
raise AbstractMethodError(self)
56965623

56975624
@property
56985625
def dtypes(self):
@@ -5725,23 +5652,6 @@ def dtypes(self):
57255652
data = self._mgr.get_dtypes()
57265653
return self._constructor_sliced(data, index=self._info_axis, dtype=np.object_)
57275654

5728-
@final
5729-
def _to_dict_of_blocks(self, copy: bool_t = True):
5730-
"""
5731-
Return a dict of dtype -> Constructor Types that
5732-
each is a homogeneous dtype.
5733-
5734-
Internal ONLY - only works for BlockManager
5735-
"""
5736-
mgr = self._mgr
5737-
# convert to BlockManager if needed -> this way support ArrayManager as well
5738-
mgr = mgr_to_mgr(mgr, "block")
5739-
mgr = cast(BlockManager, mgr)
5740-
return {
5741-
k: self._constructor(v).__finalize__(self)
5742-
for k, v, in mgr.to_dict(copy=copy).items()
5743-
}
5744-
57455655
def astype(
57465656
self: FrameOrSeries, dtype, copy: bool_t = True, errors: str = "raise"
57475657
) -> FrameOrSeries:

0 commit comments

Comments
 (0)