Skip to content

Commit

Permalink
fix safe_cast_to_index (#3001)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0L authored and shoyer committed Jun 10, 2019
1 parent 44011c9 commit 7e649e4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ Bug fixes
By `Deepak Cherian <https://github.com/dcherian`_.
- A deep copy deep-copies the coords (:issue:`1463`)
By `Martin Pletcher <https://github.com/pletchm>`_.
- Removed usages of `pytest.config`, which is deprecated (:issue:`2988`:)
- Removed usages of `pytest.config`, which is deprecated (:issue:`2988`)
By `Maximilian Roos <https://github.com/max-sixty>`_.
- Fixed performance issues with cftime installed (:issue:`3000`)
By `0x0L <https://github.com/0x0L>`_.

.. _whats-new.0.12.1:

Expand Down
4 changes: 2 additions & 2 deletions xarray/coding/cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def get_date_type(self):
def assert_all_valid_date_type(data):
import cftime

if data.size:
if len(data) > 0:
sample = data[0]
date_type = type(sample)
if not isinstance(sample, cftime.datetime):
Expand Down Expand Up @@ -229,12 +229,12 @@ class CFTimeIndex(pd.Index):
date_type = property(get_date_type)

def __new__(cls, data, name=None):
assert_all_valid_date_type(data)
if name is None and hasattr(data, 'name'):
name = data.name

result = object.__new__(cls)
result._data = np.array(data, dtype='O')
assert_all_valid_date_type(result._data)
result.name = name
return result

Expand Down
2 changes: 1 addition & 1 deletion xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ 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:
return CFTimeIndex(index)
except (ImportError, TypeError):
Expand Down
6 changes: 6 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,12 @@ 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, :1].stack(dim=('x', 'y')).dim.to_index()
b = pd.MultiIndex(levels=[pd.Int64Index([]), pd.Int64Index([0])],
labels=[[], []], names=['x', 'y'])
pd.util.testing.assert_index_equal(a, b)

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

Expand Down

0 comments on commit 7e649e4

Please sign in to comment.