Skip to content

fix mean for datetime-like using the respective time resolution unit #9977

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 9 commits into from
Jan 29, 2025
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Spencer Clark <spencerkclark@gmail.com>
  • Loading branch information
kmuehlbauer and spencerkclark authored Jan 26, 2025
commit 295d8167ff5a45701f06b219ec5dac0a8f3c87ba
3 changes: 1 addition & 2 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
xp = get_array_namespace(data)
if xp == np:
# numpy currently doesn't have a astype:
return data.astype(dtype, **kwargs)

Check warning on line 234 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.10

invalid value encountered in cast

Check warning on line 234 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.10

invalid value encountered in cast

Check warning on line 234 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.12

invalid value encountered in cast

Check warning on line 234 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / macos-latest py3.12

invalid value encountered in cast

Check warning on line 234 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10

invalid value encountered in cast

Check warning on line 234 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10

invalid value encountered in cast

Check warning on line 234 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.12

invalid value encountered in cast

Check warning on line 234 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.12

invalid value encountered in cast

Check warning on line 234 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / windows-latest py3.10

invalid value encountered in cast

Check warning on line 234 in xarray/core/duck_array_ops.py

View workflow job for this annotation

GitHub Actions / windows-latest py3.10

invalid value encountered in cast
return xp.astype(data, dtype, **kwargs)
return data.astype(dtype, **kwargs)

Expand Down Expand Up @@ -713,8 +713,7 @@
# is one of "s", "ms", "us", "ns".
# To not have to worry about the resolution, we just convert the output
# to "timedelta64" (without unit) and let the dtype of offset take precedence.
# This is fully backwards compatible with datetime64[ns]. For other
# this might lead to imprecise output, where fractional parts are truncated.
# This is fully backwards compatible with datetime64[ns].
return (
_mean(
datetime_to_numeric(array, offset), axis=axis, skipna=skipna, **kwargs
Expand Down
4 changes: 0 additions & 4 deletions xarray/tests/test_duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,8 +970,6 @@ def test_np_timedelta64_to_float(
# tests any combination of source np.timedelta64 (NPDatetimeUnitOptions) with
# np_timedelta_to_float with dedicated target unit (PDDatetimeUnitOptions)
td = np.timedelta64(1, np_dt_unit)
if _NS_PER_TIME_DELTA[np_dt_unit] < _NS_PER_TIME_DELTA[time_unit]:
pytest.skip("combination cannot be calculated without precision loss")
expected = _NS_PER_TIME_DELTA[np_dt_unit] / _NS_PER_TIME_DELTA[time_unit]

out = np_timedelta64_to_float(td, datetime_unit=time_unit)
Expand All @@ -989,8 +987,6 @@ def test_pd_timedelta_to_float(
# tests any combination of source pd.Timedelta (NPDatetimeUnitOptions) with
# np_timedelta_to_float with dedicated target unit (PDDatetimeUnitOptions)
td = pd.Timedelta(1, np_dt_unit)
if _NS_PER_TIME_DELTA[np_dt_unit] < _NS_PER_TIME_DELTA[time_unit]:
pytest.skip("combination cannot be calculated without precision loss")
expected = _NS_PER_TIME_DELTA[np_dt_unit] / _NS_PER_TIME_DELTA[time_unit]

out = pd_timedelta_to_float(td, datetime_unit=time_unit)
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy