Skip to content

Commit

Permalink
Merge branch 'main' into numbagg-ddof
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty authored Jan 22, 2024
2 parents 4568a7a + 32c1645 commit c2d15e2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7262,6 +7262,7 @@ Breaking changes
such `'DJF'`:

.. ipython:: python
:okwarning:
ds = xray.Dataset({"t": pd.date_range("2000-01-01", periods=12, freq="M")})
ds["t.season"]
Expand Down
28 changes: 21 additions & 7 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
ValuesView,
)
from enum import Enum
from pathlib import Path
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -1224,12 +1225,12 @@ def module_available(module: str, minversion: str | None = None) -> bool:


def find_stack_level(test_mode=False) -> int:
"""Find the first place in the stack that is not inside xarray.
"""Find the first place in the stack that is not inside xarray or the Python standard library.
This is unless the code emanates from a test, in which case we would prefer
to see the xarray source.
This function is taken from pandas.
This function is taken from pandas and modified to exclude standard library paths.
Parameters
----------
Expand All @@ -1240,19 +1241,32 @@ def find_stack_level(test_mode=False) -> int:
Returns
-------
stacklevel : int
First level in the stack that is not part of xarray.
First level in the stack that is not part of xarray or the Python standard library.
"""
import xarray as xr

pkg_dir = os.path.dirname(xr.__file__)
test_dir = os.path.join(pkg_dir, "tests")
pkg_dir = Path(xr.__file__).parent
test_dir = pkg_dir / "tests"

std_lib_init = sys.modules["os"].__file__
# Mostly to appease mypy; I don't think this can happen...
if std_lib_init is None:
return 0

std_lib_dir = Path(std_lib_init).parent

# https://stackoverflow.com/questions/17407119/python-inspect-stack-is-slow
frame = inspect.currentframe()
n = 0
while frame:
fname = inspect.getfile(frame)
if fname.startswith(pkg_dir) and (not fname.startswith(test_dir) or test_mode):
if (
fname.startswith(str(pkg_dir))
and (not fname.startswith(str(test_dir)) or test_mode)
) or (
fname.startswith(str(std_lib_dir))
and "site-packages" not in fname
and "dist-packages" not in fname
):
frame = frame.f_back
n += 1
else:
Expand Down
3 changes: 3 additions & 0 deletions xarray/tests/test_cftime_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,9 @@ def test_date_range_errors() -> None:


@requires_cftime
@pytest.mark.xfail(
reason="https://github.com/pydata/xarray/pull/8636#issuecomment-1902775153"
)
@pytest.mark.parametrize(
"start,freq,cal_src,cal_tgt,use_cftime,exp0,exp_pd",
[
Expand Down
14 changes: 11 additions & 3 deletions xarray/tests/test_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,16 +802,20 @@ def test_to_dask_dataframe(self):
assert isinstance(actual, dd.DataFrame)

# use the .equals from pandas to check dataframes are equivalent
assert_frame_equal(expected.compute(), actual.compute())
assert_frame_equal(actual.compute(), expected.compute())

# test if no index is given
expected = dd.from_pandas(expected_pd.reset_index(drop=False), chunksize=4)

actual = ds.to_dask_dataframe(set_index=False)

assert isinstance(actual, dd.DataFrame)
assert_frame_equal(expected.compute(), actual.compute())
assert_frame_equal(actual.compute(), expected.compute())

@pytest.mark.xfail(
reason="Currently pandas with pyarrow installed will return a `string[pyarrow]` type, "
"which causes the `y` column to have a different type depending on whether pyarrow is installed"
)
def test_to_dask_dataframe_2D(self):
# Test if 2-D dataset is supplied
w = np.random.randn(2, 3)
Expand All @@ -830,7 +834,7 @@ def test_to_dask_dataframe_2D(self):
actual = ds.to_dask_dataframe(set_index=False)

assert isinstance(actual, dd.DataFrame)
assert_frame_equal(expected, actual.compute())
assert_frame_equal(actual.compute(), expected)

@pytest.mark.xfail(raises=NotImplementedError)
def test_to_dask_dataframe_2D_set_index(self):
Expand Down Expand Up @@ -863,6 +867,10 @@ def test_to_dask_dataframe_coordinates(self):
assert isinstance(actual, dd.DataFrame)
assert_frame_equal(expected.compute(), actual.compute())

@pytest.mark.xfail(
reason="Currently pandas with pyarrow installed will return a `string[pyarrow]` type, "
"which causes the index to have a different type depending on whether pyarrow is installed"
)
def test_to_dask_dataframe_not_daskarray(self):
# Test if DataArray is not a dask array
x = np.random.randn(10)
Expand Down

0 comments on commit c2d15e2

Please sign in to comment.