Skip to content

Commit

Permalink
Add type hints to maybe_promote in dtypes.py (#8243)
Browse files Browse the repository at this point in the history
* Add type hints to maybe_promote

* attempt to type hint fill_value

* Update dtypes.py

* Update dtypes.py

* avoid type redefinition

* I give upp with fill_value, pandas mostly do it as well. Only 1 place had the Scalar typing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update types.py

* Update dtypes.py

* Update dtypes.py

* Update variables.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Illviljan and pre-commit-ci[bot] authored Sep 28, 2023
1 parent 0d6cd2a commit dbcf6a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions xarray/coding/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ def decode(self, variable: Variable, name: T_Name = None):
)

# special case DateTime to properly handle NaT
dtype: np.typing.DTypeLike
decoded_fill_value: Any
if _is_time_like(attrs.get("units")) and data.dtype.kind in "iu":
dtype, decoded_fill_value = np.int64, np.iinfo(np.int64).min
else:
Expand Down
19 changes: 13 additions & 6 deletions xarray/core/dtypes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import functools
from typing import Any

import numpy as np

Expand Down Expand Up @@ -44,7 +45,7 @@ def __eq__(self, other):
)


def maybe_promote(dtype):
def maybe_promote(dtype: np.dtype) -> tuple[np.dtype, Any]:
"""Simpler equivalent of pandas.core.common._maybe_promote
Parameters
Expand All @@ -57,27 +58,33 @@ def maybe_promote(dtype):
fill_value : Valid missing value for the promoted dtype.
"""
# N.B. these casting rules should match pandas
dtype_: np.typing.DTypeLike
fill_value: Any
if np.issubdtype(dtype, np.floating):
dtype_ = dtype
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")
dtype_ = dtype
elif np.issubdtype(dtype, np.integer):
dtype = np.float32 if dtype.itemsize <= 2 else np.float64
dtype_ = np.float32 if dtype.itemsize <= 2 else np.float64
fill_value = np.nan
elif np.issubdtype(dtype, np.complexfloating):
dtype_ = dtype
fill_value = np.nan + np.nan * 1j
elif np.issubdtype(dtype, np.datetime64):
dtype_ = dtype
fill_value = np.datetime64("NaT")
else:
dtype = object
dtype_ = object
fill_value = np.nan

dtype = np.dtype(dtype)
fill_value = dtype.type(fill_value)
return dtype, fill_value
dtype_out = np.dtype(dtype_)
fill_value = dtype_out.type(fill_value)
return dtype_out, fill_value


NAT_TYPES = {np.datetime64("NaT").dtype, np.timedelta64("NaT").dtype}
Expand Down

0 comments on commit dbcf6a7

Please sign in to comment.