Skip to content

Commit 1d11d8a

Browse files
committed
Merge branch 'master' of https://github.com/goyodiaz/pandas into goyodiaz-master
Conflicts: doc/source/release.rst
2 parents 2f3672e + 89a65d2 commit 1d11d8a

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

doc/source/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ Bug Fixes
513513
- Fix a bug when indexing with ``np.nan`` via ``iloc/loc`` (:issue:`5016`)
514514
- Fixed a bug where low memory c parser could create different types in different
515515
chunks of the same file. Now coerces to numerical type or raises warning. (:issue:`3866`)
516+
- Fix a bug where reshaping a ``Series`` to its own shape raised ``TypeError`` (:issue:`4554`)
517+
and other reshaping issues.
516518

517519
pandas 0.12.0
518520
-------------

pandas/core/series.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -697,18 +697,21 @@ def repeat(self, reps):
697697
new_values = self.values.repeat(reps)
698698
return self._constructor(new_values, index=new_index, name=self.name)
699699

700-
def reshape(self, newshape, order='C'):
700+
def reshape(self, *args, **kwargs):
701701
"""
702702
See numpy.ndarray.reshape
703703
"""
704-
if order not in ['C', 'F']:
705-
raise TypeError(
706-
"must specify a tuple / singular length to reshape")
707-
708-
if isinstance(newshape, tuple) and len(newshape) > 1:
709-
return self.values.reshape(newshape, order=order)
704+
if len(args) == 1 and hasattr(args[0], '__iter__'):
705+
shape = args[0]
710706
else:
711-
return ndarray.reshape(self, newshape, order)
707+
shape = args
708+
709+
if tuple(shape) == self.shape:
710+
# XXX ignoring the "order" keyword.
711+
return self
712+
713+
return self.values.reshape(shape, **kwargs)
714+
712715

713716
def get(self, label, default=None):
714717
"""

pandas/tests/test_series.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,12 +1045,16 @@ def test_basic_getitem_setitem_corner(self):
10451045
[5, slice(None, None)], 2)
10461046

10471047
def test_reshape_non_2d(self):
1048+
# GH 4554
10481049
x = Series(np.random.random(201), name='x')
1049-
self.assertRaises(TypeError, x.reshape, (len(x),))
1050+
self.assertTrue(x.reshape(x.shape,) is x)
10501051

10511052
# GH 2719
10521053
a = Series([1, 2, 3, 4])
1053-
self.assertRaises(TypeError, a.reshape, 2, 2)
1054+
result = a.reshape(2, 2)
1055+
expected = a.values.reshape(2, 2)
1056+
np.testing.assert_array_equal(result, expected)
1057+
self.assertTrue(type(result) is type(expected))
10541058

10551059
def test_reshape_2d_return_array(self):
10561060
x = Series(np.random.random(201), name='x')

0 commit comments

Comments
 (0)