-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Fix _can_hold_element for datetimelike blocks #27347
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
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0b5adeb
make _can_hold_element more accurate
jbrockmendel 9a79b37
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel e1f8703
Correctly accept None, np.nan, timedelta64(NaT)
jbrockmendel 5c6ccd3
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel 96e666f
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel 3663e22
revert m8 change
jbrockmendel 5a63cf4
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel 36c3b58
use is_valid_nat_for_dtype
jbrockmendel e0f76a6
test_roundtrip_thru_setitem fix
jbrockmendel c26cd12
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel 06b54ef
use is_valid_nat_for_dtype
jbrockmendel 7a54804
allow iNaT through
jbrockmendel 6ae4ee1
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel 07be36c
scalar check
jbrockmendel f4d381f
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel 1b1d4d8
avoid np deprecation warnings that raise in npdev
jbrockmendel 178dde6
implement is_integer_strict
jbrockmendel 060998e
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel a126c3b
revert is_integer_strict
jbrockmendel aec6a6c
Merge branch 'master' of https://github.com/pandas-dev/pandas into ca…
jbrockmendel 2f3a0ed
update post is_integer fix
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
from pandas._libs import NaT, lib, tslib, tslibs | ||
import pandas._libs.internals as libinternals | ||
from pandas._libs.tslibs import Timedelta, conversion, is_null_datetimelike | ||
from pandas._libs.tslibs.timezones import tz_compare | ||
from pandas.util._validators import validate_bool_kwarg | ||
|
||
from pandas.core.dtypes.cast import ( | ||
|
@@ -2248,14 +2249,17 @@ def _astype(self, dtype, **kwargs): | |
def _can_hold_element(self, element): | ||
tipo = maybe_infer_dtype_type(element) | ||
if tipo is not None: | ||
return tipo == _NS_DTYPE or tipo == np.int64 | ||
return is_dtype_equal(tipo, self.dtype) | ||
elif element is NaT: | ||
return True | ||
elif isinstance(element, datetime): | ||
if self.is_datetimetz: | ||
return tz_compare(element.tzinfo, self.dtype.tz) | ||
return element.tzinfo is None | ||
elif is_integer(element): | ||
return element == tslibs.iNaT | ||
|
||
# TODO: shouldnt we exclude timedelta64("NaT")? See GH#27297 | ||
return isna(element) | ||
return isna(element) and not isinstance(element, np.timedelta64) | ||
|
||
def _coerce_values(self, values): | ||
return values.view("i8") | ||
|
@@ -2359,6 +2363,8 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeBlock): | |
is_datetimetz = True | ||
is_extension = True | ||
|
||
_can_hold_element = DatetimeBlock._can_hold_element | ||
|
||
@property | ||
def _holder(self): | ||
return DatetimeArray | ||
|
@@ -2606,12 +2612,14 @@ def _box_func(self): | |
def _can_hold_element(self, element): | ||
tipo = maybe_infer_dtype_type(element) | ||
if tipo is not None: | ||
return issubclass(tipo.type, (np.timedelta64, np.int64)) | ||
return issubclass(tipo.type, np.timedelta64) | ||
elif element is NaT: | ||
return True | ||
return is_integer(element) or isinstance( | ||
element, (timedelta, np.timedelta64, np.int64) | ||
) | ||
elif isinstance(element, (timedelta, np.timedelta64)): | ||
return True | ||
elif is_integer(element): | ||
return element == tslibs.iNaT | ||
return isna(element) and not isinstance(element, np.datetime64) | ||
|
||
def fillna(self, value, **kwargs): | ||
|
||
|
@@ -2647,7 +2655,7 @@ def _try_coerce_args(self, other): | |
base-type other | ||
""" | ||
|
||
if is_null_datetimelike(other): | ||
if is_null_datetimelike(other) and not isinstance(other, np.datetime64): | ||
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. can you handle this case in is_null_datetimelike |
||
other = tslibs.iNaT | ||
elif isinstance(other, (timedelta, np.timedelta64)): | ||
other = Timedelta(other).value | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.