Skip to content

REF: share external_values ArrayManager/BlockManager #40529

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 5 commits into from
Mar 22, 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
2 changes: 2 additions & 0 deletions doc/source/user_guide/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5240,6 +5240,7 @@ Write to a feather file.
Read from a feather file.

.. ipython:: python
:okwarning:

result = pd.read_feather("example.feather")
result
Expand Down Expand Up @@ -5323,6 +5324,7 @@ Write to a parquet file.
Read from a parquet file.

.. ipython:: python
:okwarning:

result = pd.read_parquet("example_fp.parquet", engine="fastparquet")
result = pd.read_parquet("example_pa.parquet", engine="pyarrow")
Expand Down
10 changes: 2 additions & 8 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@
from pandas.core.arrays import (
DatetimeArray,
ExtensionArray,
IntervalArray,
PandasArray,
PeriodArray,
TimedeltaArray,
)
from pandas.core.arrays.sparse import SparseDtype
Expand All @@ -87,6 +85,7 @@
)
from pandas.core.internals.blocks import (
ensure_block_shape,
external_values,
new_block,
to_native_types,
)
Expand Down Expand Up @@ -1203,12 +1202,7 @@ def dtype(self):

def external_values(self):
"""The array that Series.values returns"""
if isinstance(self.array, (PeriodArray, IntervalArray)):
return self.array.astype(object)
elif isinstance(self.array, (DatetimeArray, TimedeltaArray)):
return self.array._data
else:
return self.array
return external_values(self.array)

def internal_values(self):
"""The array that Series._values returns"""
Expand Down
42 changes: 25 additions & 17 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@
ExtensionArray,
FloatingArray,
IntegerArray,
IntervalArray,
PandasArray,
PeriodArray,
TimedeltaArray,
)
from pandas.core.base import PandasObject
Expand Down Expand Up @@ -217,16 +219,9 @@ def is_view(self) -> bool:
def is_categorical(self) -> bool:
return self._holder is Categorical

@final
def external_values(self):
"""
The array that Series.values returns (public attribute).

This has some historical constraints, and is overridden in block
subclasses to return the correct array (e.g. period returns
object ndarray and datetimetz a datetime64[ns] ndarray instead of
proper extension array).
"""
return self.values
return external_values(self.values)

def internal_values(self):
"""
Expand Down Expand Up @@ -1770,8 +1765,7 @@ class ObjectValuesExtensionBlock(HybridMixin, ExtensionBlock):
Series[T].values is an ndarray of objects.
"""

def external_values(self):
return self.values.astype(object)
pass


class NumericBlock(Block):
Expand Down Expand Up @@ -1956,12 +1950,6 @@ def is_view(self) -> bool:
# check the ndarray values of the DatetimeIndex values
return self.values._data.base is not None

def external_values(self):
# NB: this is different from np.asarray(self.values), since that
# return an object-dtype ndarray of Timestamps.
# Avoid FutureWarning in .astype in casting from dt64tz to dt64
return self.values._data


class TimeDeltaBlock(DatetimeLikeBlockMixin):
__slots__ = ()
Expand Down Expand Up @@ -2296,3 +2284,23 @@ def to_native_types(
values[mask] = na_rep
values = values.astype(object, copy=False)
return values


def external_values(values: ArrayLike) -> ArrayLike:
"""
The array that Series.values returns (public attribute).

This has some historical constraints, and is overridden in block
subclasses to return the correct array (e.g. period returns
object ndarray and datetimetz a datetime64[ns] ndarray instead of
proper extension array).
"""
if isinstance(values, (PeriodArray, IntervalArray)):
return values.astype(object)
elif isinstance(values, (DatetimeArray, TimedeltaArray)):
# NB: for datetime64tz this is different from np.asarray(values), since
# that returns an object-dtype ndarray of Timestamps.
# Avoid FutureWarning in .astype in casting from dt64tz to dt64
return values._data
else:
return values