-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
core: try coerce result back to DatetimeBlock #22008
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -63,7 +63,9 @@ | |||
ABCSeries, | ||||
ABCDatetimeIndex, | ||||
ABCExtensionArray, | ||||
ABCIndexClass) | ||||
ABCIndexClass, | ||||
ABCDateOffset, | ||||
) | ||||
import pandas.core.common as com | ||||
import pandas.core.algorithms as algos | ||||
|
||||
|
@@ -2737,7 +2739,7 @@ def _try_coerce_args(self, values, other): | |||
|
||||
def _try_coerce_result(self, result): | ||||
""" reverse of try_coerce_args """ | ||||
if isinstance(result, np.ndarray): | ||||
if isinstance(result, (np.ndarray, Block)): | ||||
if result.dtype.kind in ['i', 'f', 'O']: | ||||
try: | ||||
result = result.astype('M8[ns]') | ||||
|
@@ -2785,6 +2787,17 @@ def set(self, locs, values, check=False): | |||
|
||||
self.values[locs] = values | ||||
|
||||
def eval(self, func, other, try_cast=False, **kwargs): | ||||
block = super(DatetimeBlock, self).eval(func, other, try_cast=try_cast, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens here if instead of calling super eval you delegate to self.holder? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In In fact, what should be blamed is the last line in
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There has been an effort recently to fix mismatches between the behaviors of arithmetic ops in Index vs Series vs DataFrame. For the timedelta and datetime cases, the canonical behavior is defined in TimedeltaIndex/DatetimeIndex. The best case here to to avoid making this fix in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That will be great, but it may need a big rewrite of Is there an issue to xfer? This PR would depand on it. |
||||
**kwargs)[0] | ||||
if try_cast: | ||||
if isinstance(other, (np.datetime64, date)): | ||||
block = TimeDeltaBlock(block.values, block.mgr_locs, | ||||
ndim=block.ndim) | ||||
elif isinstance(other, ABCDateOffset): | ||||
block = self._try_coerce_result(block) | ||||
return [block] | ||||
|
||||
|
||||
class DatetimeTZBlock(NonConsolidatableMixIn, DatetimeBlock): | ||||
""" implement a datetime64 block with a tz attribute """ | ||||
|
@@ -2920,6 +2933,8 @@ def _try_coerce_result(self, result): | |||
if isinstance(result, np.ndarray): | ||||
if result.dtype.kind in ['i', 'f', 'O']: | ||||
result = result.astype('M8[ns]') | ||||
elif isinstance(result, Block): | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same, NEVER do this |
||||
result = self.make_block_same_class(result.values.flat) | ||||
elif isinstance(result, (np.integer, np.float, np.datetime64)): | ||||
result = tslibs.Timestamp(result, tz=self.values.tz) | ||||
if isinstance(result, np.ndarray): | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1238,7 +1238,6 @@ class TestCanHoldElement(object): | |
(2**63, 'complex128'), | ||
(True, 'bool'), | ||
(np.timedelta64(20, 'ns'), '<m8[ns]'), | ||
(np.datetime64(20, 'ns'), '<M8[ns]'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why removed? (Besides no is exempt sub working)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR lead to The assumption |
||
]) | ||
@pytest.mark.parametrize('op', [ | ||
operator.add, | ||
|
@@ -1255,7 +1254,6 @@ def test_binop_other(self, op, value, dtype): | |
(operator.truediv, 'bool'), | ||
(operator.mod, 'i8'), | ||
(operator.mod, 'complex128'), | ||
(operator.mod, '<M8[ns]'), | ||
(operator.mod, '<m8[ns]'), | ||
(operator.pow, 'bool')} | ||
if (op, dtype) in skip: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not a good change, please remove. the point is that these are scalars/arrays NEVER blocks.