Skip to content
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

Improve non-nanosecond warning #7238

Merged
merged 2 commits into from
Nov 4, 2022

Conversation

spencerkclark
Copy link
Member

@spencerkclark spencerkclark commented Oct 30, 2022

Thanks for the feedback @hmaarrfk. Is this what you had in mind?

For example running this script:

import numpy as np
import xarray as xr

times = [np.datetime64("2000-01-01", "us")]
var = xr.Variable(["time"], times)
da = xr.DataArray(times)

leads to the following warnings:

$ python test_warning.py
test_warning.py:6: UserWarning: Converting non-nanosecond precision datetime values to nanosecond precision. This behavior can eventually be relaxed in xarray, as it is an artifact from pandas which is now beginning to support non-nanosecond precision values. This warning is caused by passing non-nanosecond np.datetime64 or np.timedelta64 values to the DataArray or Variable constructor; it can be silenced by converting the values to nanosecond precision ahead of time.
  var = xr.Variable(["time"], times)
test_warning.py:7: UserWarning: Converting non-nanosecond precision datetime values to nanosecond precision. This behavior can eventually be relaxed in xarray, as it is an artifact from pandas which is now beginning to support non-nanosecond precision values. This warning is caused by passing non-nanosecond np.datetime64 or np.timedelta64 values to the DataArray or Variable constructor; it can be silenced by converting the values to nanosecond precision ahead of time.
  da = xr.DataArray(times)

@spencerkclark
Copy link
Member Author

Hmm...I was kind of hoping we could avoid something like adding a _stacklevel_increment argument, but hardcoding the stacklevel obviously doesn't work in the case that fields are converted to variables within xarray, e.g. in the case of passing coordinates to the DataArray constructor:

import numpy as np
import xarray as xr

times = [np.datetime64("2000-01-01", "us")]
da = xr.DataArray([1], dims=["time"], coords=[times])
$ python test_warning.py
/Users/spencer/software/xarray/xarray/core/dataarray.py:148: UserWarning: Converting non-nanosecond precision datetime values to nanosecond precision. This behavior can eventually be relaxed in xarray, as it is an artifact from pandas which is now beginning to support non-nanosecond precision values. This warning is caused by passing non-nanosecond np.datetime64 or np.timedelta64 values to the DataArray or Variable constructor; it can be silenced by converting the values to nanosecond precision ahead of time.
  var = as_variable(coord, name=dim)

@hmaarrfk
Copy link
Contributor

Hmm...I was kind of hoping we could avoid something like adding a _stacklevel_increment argument.

Right. thank you for finding that example. I was going to try to construct one.

@dcherian
Copy link
Contributor

dcherian commented Nov 1, 2022

.I was kind of hoping we could avoid something like adding a _stacklevel_increment argument,

:( I don't know of any other way.

@mathause
Copy link
Collaborator

mathause commented Nov 3, 2022

Pandas has a find_stack_level function (https://github.com/pandas-dev/pandas/blob/0106c26529900bad0561efb9c9180f7f016365b0/pandas/util/_exceptions.py#L28). But I am not sure if this would fix the problem?

@dcherian dcherian mentioned this pull request Nov 3, 2022
@spencerkclark spencerkclark force-pushed the update-cast-to-nanosecond branch from 8ccbf0e to 7823d04 Compare November 4, 2022 11:38
@spencerkclark
Copy link
Member Author

Nice find @mathause! That seems to work. I added a version of that function to utils. The warning from my example above now looks like:

test_warning.py:5: UserWarning: Converting non-nanosecond precision datetime values to nanosecond precision. This behavior can eventually be relaxed in xarray, as it is an artifact from pandas which is now beginning to support non-nanosecond precision values. This warning is caused by passing non-nanosecond np.datetime64 or np.timedelta64 values to the DataArray or Variable constructor; it can be silenced by converting the values to nanosecond precision ahead of time.
  da = xr.DataArray([1], dims=["time"], coords=[times])

@spencerkclark
Copy link
Member Author

It looks like I've stumbled upon some new upstream test failures related to pandas-dev/pandas#49101.

@keewis
Copy link
Collaborator

keewis commented Nov 4, 2022

It looks like I've stumbled upon some new upstream test failures

it's not really new (the CI has been failing since about 4 days ago), but because so many tests fail we're hitting the size limit for new issues:

{"resource":"Issue","code":"custom","field":"body","message":"body is too long (maximum is 65536 characters)"}

Not sure if we can do something about that in the action?

cc @andersy005

Copy link
Contributor

@dcherian dcherian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The new NON_NANOSECOND_WARNING is not very nice to end users
5 participants