Skip to content

Commit e95dca3

Browse files
jbrockmendelJulianWgs
authored andcommitted
REF: share external_values ArrayManager/BlockManager (pandas-dev#40529)
1 parent 1d6ce0f commit e95dca3

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

pandas/core/internals/array_manager.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@
6161
from pandas.core.arrays import (
6262
DatetimeArray,
6363
ExtensionArray,
64-
IntervalArray,
6564
PandasArray,
66-
PeriodArray,
6765
TimedeltaArray,
6866
)
6967
from pandas.core.arrays.sparse import SparseDtype
@@ -87,6 +85,7 @@
8785
)
8886
from pandas.core.internals.blocks import (
8987
ensure_block_shape,
88+
external_values,
9089
new_block,
9190
to_native_types,
9291
)
@@ -1205,12 +1204,7 @@ def dtype(self):
12051204

12061205
def external_values(self):
12071206
"""The array that Series.values returns"""
1208-
if isinstance(self.array, (PeriodArray, IntervalArray)):
1209-
return self.array.astype(object)
1210-
elif isinstance(self.array, (DatetimeArray, TimedeltaArray)):
1211-
return self.array._data
1212-
else:
1213-
return self.array
1207+
return external_values(self.array)
12141208

12151209
def internal_values(self):
12161210
"""The array that Series._values returns"""

pandas/core/internals/blocks.py

+25-17
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@
9696
ExtensionArray,
9797
FloatingArray,
9898
IntegerArray,
99+
IntervalArray,
99100
PandasArray,
101+
PeriodArray,
100102
TimedeltaArray,
101103
)
102104
from pandas.core.base import PandasObject
@@ -227,16 +229,9 @@ def _can_hold_na(self) -> bool:
227229
def is_categorical(self) -> bool:
228230
return self._holder is Categorical
229231

232+
@final
230233
def external_values(self):
231-
"""
232-
The array that Series.values returns (public attribute).
233-
234-
This has some historical constraints, and is overridden in block
235-
subclasses to return the correct array (e.g. period returns
236-
object ndarray and datetimetz a datetime64[ns] ndarray instead of
237-
proper extension array).
238-
"""
239-
return self.values
234+
return external_values(self.values)
240235

241236
def internal_values(self):
242237
"""
@@ -1775,8 +1770,7 @@ class ObjectValuesExtensionBlock(HybridMixin, ExtensionBlock):
17751770
Series[T].values is an ndarray of objects.
17761771
"""
17771772

1778-
def external_values(self):
1779-
return self.values.astype(object)
1773+
pass
17801774

17811775

17821776
class NumericBlock(Block):
@@ -1951,12 +1945,6 @@ def is_view(self) -> bool:
19511945
# check the ndarray values of the DatetimeIndex values
19521946
return self.values._data.base is not None
19531947

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

19611949
class TimeDeltaBlock(DatetimeLikeBlockMixin):
19621950
__slots__ = ()
@@ -2288,3 +2276,23 @@ def to_native_types(
22882276
values[mask] = na_rep
22892277
values = values.astype(object, copy=False)
22902278
return values
2279+
2280+
2281+
def external_values(values: ArrayLike) -> ArrayLike:
2282+
"""
2283+
The array that Series.values returns (public attribute).
2284+
2285+
This has some historical constraints, and is overridden in block
2286+
subclasses to return the correct array (e.g. period returns
2287+
object ndarray and datetimetz a datetime64[ns] ndarray instead of
2288+
proper extension array).
2289+
"""
2290+
if isinstance(values, (PeriodArray, IntervalArray)):
2291+
return values.astype(object)
2292+
elif isinstance(values, (DatetimeArray, TimedeltaArray)):
2293+
# NB: for datetime64tz this is different from np.asarray(values), since
2294+
# that returns an object-dtype ndarray of Timestamps.
2295+
# Avoid FutureWarning in .astype in casting from dt64tz to dt64
2296+
return values._data
2297+
else:
2298+
return values

0 commit comments

Comments
 (0)