Skip to content

Commit 2ee2c65

Browse files
committed
Merge pull request #746 from SixtyCapital/period-index-reset-on-transpose
BUG: Don't transpose variables of one dimension
2 parents dcf2b5b + 0c6dcf3 commit 2ee2c65

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

doc/whats-new.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ Enhancements
2121
- ``Dataset.rename`` and ``DataArray.rename`` support the old and new names being the same. This had been supported prior to v0.7.0 but was broken in 0.7.0.
2222
- ``DataArray.reindex_like`` now maintains the dtype of complex numbers when reindexing leads to na values.
2323

24+
25+
Bug fixes
26+
~~~~~~~~~
27+
28+
- Single dimension variables no longer transpose as part of a broader ``.transpose``. This behavior
29+
was causing ``pandas.PeriodIndex`` dimensions to lose their type
30+
2431
.. _whats-new.0.7.0:
2532

2633
v0.7.0 (21 January 2016)

xarray/core/variable.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,8 @@ def transpose(self, *dims):
648648
if len(dims) == 0:
649649
dims = self.dims[::-1]
650650
axes = self.get_axis_num(dims)
651+
if len(dims) < 2: # no need to transpose if only one dimension
652+
return self.copy(deep=False)
651653
data = ops.transpose(self.data, axes)
652654
return type(self)(dims, data, self._attrs, self._encoding, fastpath=True)
653655

xarray/test/test_dataset.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,15 @@ def test_dataset_transpose(self):
22592259
with self.assertRaisesRegexp(ValueError, 'arguments to transpose'):
22602260
ds.transpose('dim1', 'dim2', 'dim3', 'time', 'extra_dim')
22612261

2262+
def test_dataset_retains_period_index_on_transpose(self):
2263+
2264+
ds = create_test_data()
2265+
ds['time'] = pd.period_range('2000-01-01', periods=20)
2266+
2267+
transposed = ds.transpose()
2268+
2269+
self.assertIsInstance(transposed.time.to_index(), pd.PeriodIndex)
2270+
22622271
def test_dataset_diff_n1_simple(self):
22632272
ds = Dataset({'foo': ('x', [5, 5, 6, 6])})
22642273
actual = ds.diff('x')

0 commit comments

Comments
 (0)