Skip to content

Commit

Permalink
fix safe_cast_to_index
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0L committed Jun 5, 2019
1 parent 74e5ff6 commit 8e32c95
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ Bug fixes
By `Martin Pletcher <https://github.com/pletchm>`_.
- Removed usages of `pytest.config`, which is deprecated (:issue:`2988`:)
By `Maximilian Roos <https://github.com/max-sixty>`_.
- Fixed a bug in `utils.safe_cast_to_index` and performance issues when
`cftime` is installed (:issue:`3000`:)
By `0x0L <https://github.com/0x0L>`_.

.. _whats-new.0.12.1:

Expand Down
5 changes: 4 additions & 1 deletion xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ def wrapper(*args, **kwargs):
def _maybe_cast_to_cftimeindex(index: pd.Index) -> pd.Index:
from ..coding.cftimeindex import CFTimeIndex

if index.dtype == 'O':
if len(index) > 0 and index.dtype == 'O':
try:
import cftime
if not isinstance(index[0], cftime.datetime):
return index
return CFTimeIndex(index)
except (ImportError, TypeError):
return index
Expand Down
5 changes: 5 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,11 @@ def test_stack_unstack(self):
orig = DataArray([[0, 1], [2, 3]], dims=['x', 'y'], attrs={'foo': 2})
assert_identical(orig, orig.unstack())

# test GH3000
a = orig[:0].stack(dim=('x', 'y')).dim.to_index()
b = pd.MultiIndex(levels=[[], [0, 1]], codes=[[], []], names=['x', 'y'])
assert a.equals(b)

actual = orig.stack(z=['x', 'y']).unstack('z').drop(['x', 'y'])
assert_identical(orig, actual)

Expand Down

0 comments on commit 8e32c95

Please sign in to comment.