Skip to content

CLN: Make Series._values match Index._values #31182

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 16 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 1 addition & 10 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,16 +752,7 @@ def array(self) -> ExtensionArray:
[a, b, a]
Categories (2, object): [a, b]
"""
# As a mixin, we depend on the mixing class having _values.
# Special mixin syntax may be developed in the future:
# https://github.com/python/typing/issues/246
result = self._values # type: ignore
if isinstance(result, np.ndarray):
from pandas.core.arrays.numpy_ import PandasArray

result = PandasArray(result)

return result
raise AbstractMethodError(self)

def to_numpy(self, dtype=None, copy=False, na_value=lib.no_default, **kwargs):
"""
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,7 @@ def extract_array(obj, extract_numpy: bool = False):
array([1, 2, 3])
"""
if isinstance(obj, (ABCIndexClass, ABCSeries)):
arr = obj._values
if not extract_numpy and isinstance(arr, np.ndarray):
return obj.array
return arr
obj = obj.array

if extract_numpy and isinstance(obj, ABCPandasArray):
obj = obj.to_numpy()
Expand Down
28 changes: 25 additions & 3 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@
)

import pandas.core.algorithms as algos
from pandas.core.arrays import Categorical, DatetimeArray, PandasDtype, TimedeltaArray
from pandas.core.arrays import (
Categorical,
DatetimeArray,
ExtensionArray,
PandasArray,
PandasDtype,
TimedeltaArray,
)
from pandas.core.base import PandasObject
import pandas.core.common as com
from pandas.core.construction import extract_array
Expand Down Expand Up @@ -210,6 +217,16 @@ def internal_values(self):
"""
return self.values

def array_values(self) -> ExtensionArray:
"""
The array that Series.array returns. Always an ExtensionArray.
"""
return self._ea_values

@cache_readonly
def _ea_values(self) -> ExtensionArray:
return PandasArray(self.values)

def get_values(self, dtype=None):
"""
return an internal format, currently just the ndarray
Expand Down Expand Up @@ -1782,6 +1799,9 @@ def get_values(self, dtype=None):
values = values.reshape((1,) + values.shape)
return values

def array_values(self) -> ExtensionArray:
return self.values

def to_dense(self):
return np.asarray(self.values)

Expand Down Expand Up @@ -2123,12 +2143,15 @@ def get_values(self, dtype=None):
return result.reshape(self.values.shape)
return self.values

def array_values(self):
return self._ea_values

def internal_values(self):
return self._ea_values

@cache_readonly
def _ea_values(self):
return self._holder(self.values)
return self._holder._simple_new(self.values)


class DatetimeBlock(DatetimeLikeBlockMixin, Block):
Expand Down Expand Up @@ -2166,7 +2189,6 @@ def _maybe_coerce_values(self, values):
values = values._data

assert isinstance(values, np.ndarray), type(values)
assert values.dtype == _NS_DTYPE, values.dtype
return values

def astype(self, dtype, copy: bool = False, errors: str = "raise"):
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,11 @@ def _values(self):
"""
return self._data.internal_values()

@Appender(base.IndexOpsMixin.array.__doc__) # type: ignore
@property
def array(self) -> ExtensionArray:
return self._data._block.array_values()

def _internal_get_values(self):
"""
Same as values (but handles sparseness conversions); is a view.
Expand Down