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
1 change: 1 addition & 0 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ def apply_standard(self):
and not self.dtypes.apply(is_extension_array_dtype).any()
# Disallow complex_internals since libreduction shortcut
# cannot handle MultiIndex
and not self.dtypes.apply(lambda x: x.kind in ["m", "M"]).any()
and not isinstance(self.agg_axis, ABCMultiIndex)
):

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class PandasDtype(ExtensionDtype):
def __init__(self, dtype):
dtype = np.dtype(dtype)
self._dtype = dtype
self._name = dtype.name
self._type = dtype.type

def __repr__(self) -> str:
Expand All @@ -56,7 +55,7 @@ def numpy_dtype(self):

@property
def name(self):
return self._name
return self._dtype.name

@property
def type(self):
Expand Down
17 changes: 4 additions & 13 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
from pandas.core.dtypes.cast import is_nested_object
from pandas.core.dtypes.common import (
is_categorical_dtype,
is_datetime64_ns_dtype,
is_dict_like,
is_extension_array_dtype,
is_list_like,
is_object_dtype,
is_scalar,
is_timedelta64_ns_dtype,
needs_i8_conversion,
)
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
Expand Down Expand Up @@ -749,17 +747,7 @@ def array(self) -> ExtensionArray:
# Special mixin syntax may be developed in the future:
# https://github.com/python/typing/issues/246
result = self._values # type: ignore

if is_datetime64_ns_dtype(result.dtype):
from pandas.arrays import DatetimeArray

result = DatetimeArray(result)
elif is_timedelta64_ns_dtype(result.dtype):
from pandas.arrays import TimedeltaArray

result = TimedeltaArray(result)

elif not is_extension_array_dtype(result.dtype):
if isinstance(result, np.ndarray):
from pandas.core.arrays.numpy_ import PandasArray

result = PandasArray(result)
Expand Down Expand Up @@ -1270,6 +1258,9 @@ def unique(self):
if hasattr(values, "unique"):

result = values.unique()
if self.dtype.kind in ["m", "M"]:
if getattr(self.dtype, "tz", None) is None:
result = np.asarray(result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here on why this is needed

else:
result = unique1d(values)

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,10 @@ def extract_array(obj, extract_numpy: bool = False):
array([1, 2, 3])
"""
if isinstance(obj, (ABCIndexClass, ABCSeries)):
obj = obj.array
arr = obj._values
if not extract_numpy and isinstance(arr, np.ndarray):
return obj.array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was also just thinking while looking above at the .array implementation, that we could do the same here instead of going through the ".array -> wrap in PandasArray -> extract the numpy array again" route, that will further reduce some overhead of extract_array(..., extract_numpy=True).

Could also do a arr = PandasArray(arr) here for being explicit (it's not that it duplicates a lot from .array)

return arr

if extract_numpy and isinstance(obj, ABCPandasArray):
obj = obj.to_numpy()
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pandas._libs.internals as libinternals
from pandas._libs.tslibs import Timedelta, conversion
from pandas._libs.tslibs.timezones import tz_compare
from pandas.util._decorators import cache_readonly
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.cast import (
Expand Down Expand Up @@ -2112,6 +2113,13 @@ def get_values(self, dtype=None):
return result.reshape(self.values.shape)
return self.values

def internal_values(self):
return self._ea_values

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


class DatetimeBlock(DatetimeLikeBlockMixin, Block):
__slots__ = ()
Expand Down Expand Up @@ -2148,6 +2156,7 @@ 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 Expand Up @@ -2242,6 +2251,7 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeBlock):
is_datetimetz = True
is_extension = True

internal_values = Block.internal_values
_can_hold_element = DatetimeBlock._can_hold_element
to_native_types = DatetimeBlock.to_native_types
fill_value = np.datetime64("NaT", "ns")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,7 @@ def test_string_na_nat_conversion(self, cache):
for i in range(5):
x = series[i]
if isna(x):
expected[i] = iNaT
expected[i] = pd.NaT
else:
expected[i] = to_datetime(x, cache=cache)

Expand Down
15 changes: 13 additions & 2 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,12 @@ def test_invalid_td64_reductions(self, opname):
)
td = s.diff()

msg = "reduction operation '{op}' not allowed for this dtype"
msg = "|".join(
[
"reduction operation '{op}' not allowed for this dtype",
r"cannot perform {op} with type timedelta64\[ns\]",
]
)
msg = msg.format(op=opname)

with pytest.raises(TypeError, match=msg):
Expand Down Expand Up @@ -648,7 +653,13 @@ def test_ops_consistency_on_empty(self, method):
# timedelta64[ns]
tdser = Series([], dtype="m8[ns]")
if method == "var":
with pytest.raises(TypeError, match="operation 'var' not allowed"):
msg = "|".join(
[
"operation 'var' not allowed",
r"cannot perform var with type timedelta64\[ns\]",
]
)
with pytest.raises(TypeError, match=msg):
getattr(tdser, method)()
else:
result = getattr(tdser, method)()
Expand Down