-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
copy the dtypes module to the namedarray package.
#8250
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 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6cce0ea
move dtypes module to namedarray
andersy005 2e5d18c
keep original dtypes
andersy005 5fda4cf
Merge branch 'main' into move-dtypes-to-namedarray
andersy005 54998d6
revert utils changes
andersy005 911ea92
Update xarray/namedarray/dtypes.py
andersy005 a619861
Merge branch 'main' into move-dtypes-to-namedarray
andersy005 c45057a
Apply suggestions from code review
andersy005 f0a65ed
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ffe4a44
fix missing imports
andersy005 f7bbbfb
update typing
andersy005 a9b3420
fix return types
andersy005 823369c
Merge branch 'main' into move-dtypes-to-namedarray
andersy005 f54ee4b
Merge branch 'main' into move-dtypes-to-namedarray
andersy005 6848022
Merge branch 'main' into move-dtypes-to-namedarray
dcherian a719a70
type fixes
Illviljan 432227a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e2de9ce
type fixes
Illviljan f5a74c0
Merge branch 'move-dtypes-to-namedarray' of https://github.com/anders…
Illviljan 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 |
|---|---|---|
| @@ -1,188 +1 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import functools | ||
|
|
||
| import numpy as np | ||
|
|
||
| from xarray.core import utils | ||
|
|
||
| # Use as a sentinel value to indicate a dtype appropriate NA value. | ||
| NA = utils.ReprObject("<NA>") | ||
|
|
||
|
|
||
| @functools.total_ordering | ||
| class AlwaysGreaterThan: | ||
| def __gt__(self, other): | ||
| return True | ||
|
|
||
| def __eq__(self, other): | ||
| return isinstance(other, type(self)) | ||
|
|
||
|
|
||
| @functools.total_ordering | ||
| class AlwaysLessThan: | ||
| def __lt__(self, other): | ||
| return True | ||
|
|
||
| def __eq__(self, other): | ||
| return isinstance(other, type(self)) | ||
|
|
||
|
|
||
| # Equivalence to np.inf (-np.inf) for object-type | ||
| INF = AlwaysGreaterThan() | ||
| NINF = AlwaysLessThan() | ||
|
|
||
|
|
||
| # Pairs of types that, if both found, should be promoted to object dtype | ||
| # instead of following NumPy's own type-promotion rules. These type promotion | ||
| # rules match pandas instead. For reference, see the NumPy type hierarchy: | ||
| # https://numpy.org/doc/stable/reference/arrays.scalars.html | ||
| PROMOTE_TO_OBJECT: tuple[tuple[type[np.generic], type[np.generic]], ...] = ( | ||
| (np.number, np.character), # numpy promotes to character | ||
| (np.bool_, np.character), # numpy promotes to character | ||
| (np.bytes_, np.str_), # numpy promotes to unicode | ||
| ) | ||
|
|
||
|
|
||
| def maybe_promote(dtype): | ||
| """Simpler equivalent of pandas.core.common._maybe_promote | ||
| Parameters | ||
| ---------- | ||
| dtype : np.dtype | ||
| Returns | ||
| ------- | ||
| dtype : Promoted dtype that can hold missing values. | ||
| fill_value : Valid missing value for the promoted dtype. | ||
| """ | ||
| # N.B. these casting rules should match pandas | ||
| if np.issubdtype(dtype, np.floating): | ||
| fill_value = np.nan | ||
| elif np.issubdtype(dtype, np.timedelta64): | ||
| # See https://github.com/numpy/numpy/issues/10685 | ||
| # np.timedelta64 is a subclass of np.integer | ||
| # Check np.timedelta64 before np.integer | ||
| fill_value = np.timedelta64("NaT") | ||
| elif np.issubdtype(dtype, np.integer): | ||
| dtype = np.float32 if dtype.itemsize <= 2 else np.float64 | ||
| fill_value = np.nan | ||
| elif np.issubdtype(dtype, np.complexfloating): | ||
| fill_value = np.nan + np.nan * 1j | ||
| elif np.issubdtype(dtype, np.datetime64): | ||
| fill_value = np.datetime64("NaT") | ||
| else: | ||
| dtype = object | ||
| fill_value = np.nan | ||
|
|
||
| dtype = np.dtype(dtype) | ||
| fill_value = dtype.type(fill_value) | ||
| return dtype, fill_value | ||
|
|
||
|
|
||
| NAT_TYPES = {np.datetime64("NaT").dtype, np.timedelta64("NaT").dtype} | ||
|
|
||
|
|
||
| def get_fill_value(dtype): | ||
| """Return an appropriate fill value for this dtype. | ||
| Parameters | ||
| ---------- | ||
| dtype : np.dtype | ||
| Returns | ||
| ------- | ||
| fill_value : Missing value corresponding to this dtype. | ||
| """ | ||
| _, fill_value = maybe_promote(dtype) | ||
| return fill_value | ||
|
|
||
|
|
||
| def get_pos_infinity(dtype, max_for_int=False): | ||
| """Return an appropriate positive infinity for this dtype. | ||
| Parameters | ||
| ---------- | ||
| dtype : np.dtype | ||
| max_for_int : bool | ||
| Return np.iinfo(dtype).max instead of np.inf | ||
| Returns | ||
| ------- | ||
| fill_value : positive infinity value corresponding to this dtype. | ||
| """ | ||
| if issubclass(dtype.type, np.floating): | ||
| return np.inf | ||
|
|
||
| if issubclass(dtype.type, np.integer): | ||
| if max_for_int: | ||
| return np.iinfo(dtype).max | ||
| else: | ||
| return np.inf | ||
|
|
||
| if issubclass(dtype.type, np.complexfloating): | ||
| return np.inf + 1j * np.inf | ||
|
|
||
| return INF | ||
|
|
||
|
|
||
| def get_neg_infinity(dtype, min_for_int=False): | ||
| """Return an appropriate positive infinity for this dtype. | ||
| Parameters | ||
| ---------- | ||
| dtype : np.dtype | ||
| min_for_int : bool | ||
| Return np.iinfo(dtype).min instead of -np.inf | ||
| Returns | ||
| ------- | ||
| fill_value : positive infinity value corresponding to this dtype. | ||
| """ | ||
| if issubclass(dtype.type, np.floating): | ||
| return -np.inf | ||
|
|
||
| if issubclass(dtype.type, np.integer): | ||
| if min_for_int: | ||
| return np.iinfo(dtype).min | ||
| else: | ||
| return -np.inf | ||
|
|
||
| if issubclass(dtype.type, np.complexfloating): | ||
| return -np.inf - 1j * np.inf | ||
|
|
||
| return NINF | ||
|
|
||
|
|
||
| def is_datetime_like(dtype): | ||
| """Check if a dtype is a subclass of the numpy datetime types""" | ||
| return np.issubdtype(dtype, np.datetime64) or np.issubdtype(dtype, np.timedelta64) | ||
|
|
||
|
|
||
| def result_type( | ||
| *arrays_and_dtypes: np.typing.ArrayLike | np.typing.DTypeLike, | ||
| ) -> np.dtype: | ||
| """Like np.result_type, but with type promotion rules matching pandas. | ||
| Examples of changed behavior: | ||
| number + string -> object (not string) | ||
| bytes + unicode -> object (not unicode) | ||
| Parameters | ||
| ---------- | ||
| *arrays_and_dtypes : list of arrays and dtypes | ||
| The dtype is extracted from both numpy and dask arrays. | ||
| Returns | ||
| ------- | ||
| numpy.dtype for the result. | ||
| """ | ||
| types = {np.result_type(t).type for t in arrays_and_dtypes} | ||
|
|
||
| for left, right in PROMOTE_TO_OBJECT: | ||
| if any(issubclass(t, left) for t in types) and any( | ||
| issubclass(t, right) for t in types | ||
| ): | ||
| return np.dtype(object) | ||
|
|
||
| return np.result_type(*arrays_and_dtypes) | ||
| from xarray.namedarray.dtypes import * # noqa: F401, F403 | ||
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
Oops, something went wrong.
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.