|
96 | 96 | ExtensionArray,
|
97 | 97 | FloatingArray,
|
98 | 98 | IntegerArray,
|
| 99 | + IntervalArray, |
99 | 100 | PandasArray,
|
| 101 | + PeriodArray, |
100 | 102 | TimedeltaArray,
|
101 | 103 | )
|
102 | 104 | from pandas.core.base import PandasObject
|
@@ -227,16 +229,9 @@ def _can_hold_na(self) -> bool:
|
227 | 229 | def is_categorical(self) -> bool:
|
228 | 230 | return self._holder is Categorical
|
229 | 231 |
|
| 232 | + @final |
230 | 233 | 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) |
240 | 235 |
|
241 | 236 | def internal_values(self):
|
242 | 237 | """
|
@@ -1775,8 +1770,7 @@ class ObjectValuesExtensionBlock(HybridMixin, ExtensionBlock):
|
1775 | 1770 | Series[T].values is an ndarray of objects.
|
1776 | 1771 | """
|
1777 | 1772 |
|
1778 |
| - def external_values(self): |
1779 |
| - return self.values.astype(object) |
| 1773 | + pass |
1780 | 1774 |
|
1781 | 1775 |
|
1782 | 1776 | class NumericBlock(Block):
|
@@ -1951,12 +1945,6 @@ def is_view(self) -> bool:
|
1951 | 1945 | # check the ndarray values of the DatetimeIndex values
|
1952 | 1946 | return self.values._data.base is not None
|
1953 | 1947 |
|
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 |
| - |
1960 | 1948 |
|
1961 | 1949 | class TimeDeltaBlock(DatetimeLikeBlockMixin):
|
1962 | 1950 | __slots__ = ()
|
@@ -2288,3 +2276,23 @@ def to_native_types(
|
2288 | 2276 | values[mask] = na_rep
|
2289 | 2277 | values = values.astype(object, copy=False)
|
2290 | 2278 | 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