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

Fix to _get_nearest_indexer for pydata/xarray#3751 #32905

Merged
merged 18 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/deps/azure-36-locale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:

# pandas dependencies
- beautifulsoup4
- cftime # Needed for downstream xarray.CFTimeIndex test
Copy link
Contributor

Choose a reason for hiding this comment

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

pls move these near xarray

- gcsfs
- html5lib
- ipython
Expand Down
1 change: 1 addition & 0 deletions ci/deps/azure-37-locale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:

# pandas dependencies
- beautifulsoup4
- cftime # Needed for downstream xarray.CFTimeIndex test
- html5lib
- ipython
- jinja2
Expand Down
1 change: 1 addition & 0 deletions ci/deps/azure-macos-36.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
# pandas dependencies
- beautifulsoup4
- bottleneck
- cftime # Needed for downstream xarray.CFTimeIndex test
Copy link
Contributor

Choose a reason for hiding this comment

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

these should not be in every CI yaml
rather u skip the test if if it’s not installed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I understand; I was just following @jbrockmendel's recommendation. Which CI yaml should I put this in?

Copy link
Contributor

Choose a reason for hiding this comment

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

remove from all
except the environment.yaml

- html5lib
- jinja2
- lxml
Expand Down
1 change: 1 addition & 0 deletions ci/deps/travis-36-cov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:
# pandas dependencies
- beautifulsoup4
- botocore>=1.11
- cftime # Needed for downstream xarray.CFTimeIndex test
- cython>=0.29.13
- dask
- fastparquet>=0.3.2
Expand Down
14 changes: 9 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def _outer_indexer(self, left, right):
# Constructors

def __new__(
cls, data=None, dtype=None, copy=False, name=None, tupleize_cols=True, **kwargs,
cls, data=None, dtype=None, copy=False, name=None, tupleize_cols=True, **kwargs
) -> "Index":

from pandas.core.indexes.range import RangeIndex
Expand Down Expand Up @@ -3049,8 +3049,9 @@ def _get_nearest_indexer(self, target: "Index", limit, tolerance) -> np.ndarray:
left_indexer = self.get_indexer(target, "pad", limit=limit)
right_indexer = self.get_indexer(target, "backfill", limit=limit)

left_distances = np.abs(self[left_indexer] - target)
right_distances = np.abs(self[right_indexer] - target)
target_values = target._values
left_distances = np.abs(self._values[left_indexer] - target_values)
right_distances = np.abs(self._values[right_indexer] - target_values)

op = operator.lt if self.is_monotonic_increasing else operator.le
indexer = np.where(
Expand All @@ -3059,11 +3060,14 @@ def _get_nearest_indexer(self, target: "Index", limit, tolerance) -> np.ndarray:
right_indexer,
)
if tolerance is not None:
indexer = self._filter_indexer_tolerance(target, indexer, tolerance)
indexer = self._filter_indexer_tolerance(target_values, indexer, tolerance)
return indexer

def _filter_indexer_tolerance(
self, target: "Index", indexer: np.ndarray, tolerance
self,
target: Union["Index", np.ndarray, ExtensionArray],
jbrockmendel marked this conversation as resolved.
Show resolved Hide resolved
indexer: np.ndarray,
tolerance,
) -> np.ndarray:
distance = abs(self.values[indexer] - target)
indexer = np.where(distance <= tolerance, indexer, -1)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_downstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ def test_xarray(df):
assert df.to_xarray() is not None


def test_xarray_cftimeindex_nearest():
jreback marked this conversation as resolved.
Show resolved Hide resolved
# https://github.com/pydata/xarray/issues/3751
cftime = import_module("cftime")
xarray = import_module("xarray")

times = xarray.cftime_range("0001", periods=2)
da = xarray.DataArray(range(2), coords=[("time", times)])
result = da.sel(time=cftime.DatetimeGregorian(2000, 1, 1), method="nearest")
expected = xarray.DataArray(1, coords={"time": cftime.DatetimeGregorian(1, 1, 2)})
xarray.testing.assert_identical(result, expected)


def test_oo_optimizable():
# GH 21071
subprocess.check_call([sys.executable, "-OO", "-c", "import pandas"])
Expand Down