-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API: tighten DTA/TDA _from_sequence signature #37179
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
Closed
Closed
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
eab1030
EA: tighten TimedeltaArray._from_sequence signature
jbrockmendel 137fdf1
Merge branch 'master' of https://github.com/pandas-dev/pandas into st…
jbrockmendel e54ed75
EA: Tighten signature on DatetimeArray._from_sequence
jbrockmendel 3d2a7ab
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 96a8475
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel d49d819
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel f693ed4
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 058338a
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 77e7c21
API: restrict DTA/TDA _from_sequence
jbrockmendel 7e97d03
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel f2a2aaf
test
jbrockmendel 00d19e3
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel bc532be
lint fixup
jbrockmendel 2e612c4
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel b8db5c1
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel cad1104
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 8f1b25d
Use _from_sequence_of_strings where appropriate
jbrockmendel fb41950
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 9f72ad8
workaround for i8 case
jbrockmendel 54c87da
mypy fixup
jbrockmendel d87533b
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 158f2b2
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel b071b5f
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel bffacb1
from_sequence -> from_sequence_strict
jbrockmendel 0153a51
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 5acede8
mypy fixup
jbrockmendel 3b25043
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 8c87760
revert test edits
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
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 |
---|---|---|
|
@@ -43,7 +43,12 @@ | |
pandas_dtype, | ||
) | ||
from pandas.core.dtypes.dtypes import DatetimeTZDtype | ||
from pandas.core.dtypes.generic import ABCIndexClass, ABCPandasArray, ABCSeries | ||
from pandas.core.dtypes.generic import ( | ||
ABCIndexClass, | ||
ABCMultiIndex, | ||
ABCPandasArray, | ||
ABCSeries, | ||
) | ||
from pandas.core.dtypes.missing import isna | ||
|
||
from pandas.core.algorithms import checked_add_with_arr | ||
|
@@ -300,6 +305,39 @@ def _simple_new( | |
result._dtype = dtype | ||
return result | ||
|
||
@classmethod | ||
def _from_sequence_strict(cls, scalars, *, dtype=None, copy: bool = False): | ||
# GH#37179 eventually _from_sequence should be strict | ||
|
||
scalars, copy = dtl.ensure_arraylike(scalars, copy) | ||
|
||
if scalars.dtype.kind == "M": | ||
pass | ||
elif scalars.dtype == object: | ||
if isinstance(scalars, ABCMultiIndex): | ||
raise TypeError("Cannot create a DatetimeArray from MultiIndex") | ||
|
||
inferred = lib.infer_dtype(scalars) | ||
if inferred in ["datetime64", "date", "datetime", "empty"]: | ||
pass | ||
else: | ||
msg = f"{inferred} scalars cannot be converted to datetime64[ns]" | ||
raise TypeError(msg) | ||
elif is_string_dtype(scalars.dtype): | ||
# TODO: should go through from_sequence_of_strings? | ||
pass | ||
elif ( | ||
is_categorical_dtype(scalars.dtype) and scalars.categories.dtype.kind == "M" | ||
): | ||
# TODO: Could also use Categorical[object] | ||
# with inferred_type as above? | ||
pass | ||
else: | ||
msg = f"dtype {scalars.dtype} cannot be converted to datetime64[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. can you ensure we have a test that hits this |
||
raise TypeError(msg) | ||
|
||
return cls._from_sequence_not_strict(scalars, dtype=dtype, copy=copy) | ||
|
||
@classmethod | ||
def _from_sequence(cls, scalars, *, dtype=None, copy: bool = False): | ||
return cls._from_sequence_not_strict(scalars, dtype=dtype, copy=copy) | ||
|
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 |
---|---|---|
|
@@ -217,6 +217,29 @@ def _simple_new( | |
result._dtype = TD64NS_DTYPE | ||
return result | ||
|
||
@classmethod | ||
def _from_sequence_strict( | ||
cls, data, *, dtype=TD64NS_DTYPE, copy: bool = False | ||
) -> "TimedeltaArray": | ||
# GH#37179 eventually we want _from_sequence to be strict | ||
if dtype: | ||
_validate_td64_dtype(dtype) | ||
|
||
data, copy = dtl.ensure_arraylike(data, copy) | ||
|
||
if data.dtype.kind == "m": | ||
pass | ||
elif data.dtype == object: | ||
inferred = lib.infer_dtype(data) | ||
if inferred in ["timedelta64", "timedelta", "empty"]: | ||
pass | ||
else: | ||
raise ValueError(inferred) | ||
else: | ||
raise TypeError(data.dtype) | ||
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 ensure we have a test that hits hits |
||
|
||
return cls._from_sequence(data=data, copy=copy) | ||
|
||
@classmethod | ||
def _from_sequence( | ||
cls, data, *, dtype=TD64NS_DTYPE, copy: bool = False | ||
|
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
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
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
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
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.
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 tested?