Skip to content

Commit e248340

Browse files
gfyounghexgnu
authored andcommitted
CLN: Drop the .reshape method from classes (pandas-dev#18954)
Remove the method for Series, Categorical, and Index. Deprecated or errored in v0.19.0 xref pandas-devgh-13012
1 parent 253b63e commit e248340

File tree

7 files changed

+1
-168
lines changed

7 files changed

+1
-168
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ Removal of prior version deprecations/changes
241241
- :func:`read_csv` has dropped the ``buffer_lines`` parameter (:issue:`13360`)
242242
- :func:`read_csv` has dropped the ``compact_ints`` and ``use_unsigned`` parameters (:issue:`13323`)
243243
- The ``Timestamp`` class has dropped the ``offset`` attribute in favor of ``freq`` (:issue:`13593`)
244+
- The ``Series``, ``Categorical``, and ``Index`` classes have dropped the ``reshape`` method (:issue:`13012`)
244245

245246
.. _whatsnew_0230.performance:
246247

pandas/core/categorical.py

-26
Original file line numberDiff line numberDiff line change
@@ -471,32 +471,6 @@ def tolist(self):
471471
return [_maybe_box_datetimelike(x) for x in self]
472472
return np.array(self).tolist()
473473

474-
def reshape(self, new_shape, *args, **kwargs):
475-
"""
476-
.. deprecated:: 0.19.0
477-
Calling this method will raise an error in a future release.
478-
479-
An ndarray-compatible method that returns `self` because
480-
`Categorical` instances cannot actually be reshaped.
481-
482-
Parameters
483-
----------
484-
new_shape : int or tuple of ints
485-
A 1-D array of integers that correspond to the new
486-
shape of the `Categorical`. For more information on
487-
the parameter, please refer to `np.reshape`.
488-
"""
489-
warn("reshape is deprecated and will raise "
490-
"in a subsequent release", FutureWarning, stacklevel=2)
491-
492-
nv.validate_reshape(args, kwargs)
493-
494-
# while the 'new_shape' parameter has no effect,
495-
# we should still enforce valid shape parameters
496-
np.reshape(self.codes, new_shape)
497-
498-
return self
499-
500474
@property
501475
def base(self):
502476
""" compat, we are always our own object """

pandas/core/indexes/base.py

-10
Original file line numberDiff line numberDiff line change
@@ -1190,16 +1190,6 @@ def rename(self, name, inplace=False):
11901190
"""
11911191
return self.set_names([name], inplace=inplace)
11921192

1193-
def reshape(self, *args, **kwargs):
1194-
"""
1195-
NOT IMPLEMENTED: do not call this method, as reshaping is not
1196-
supported for Index objects and will raise an error.
1197-
1198-
Reshape an Index.
1199-
"""
1200-
raise NotImplementedError("reshaping is not supported "
1201-
"for Index objects")
1202-
12031193
@property
12041194
def _has_complex_internals(self):
12051195
# to disable groupby tricks in MultiIndex

pandas/core/series.py

-31
Original file line numberDiff line numberDiff line change
@@ -910,37 +910,6 @@ def repeat(self, repeats, *args, **kwargs):
910910
return self._constructor(new_values,
911911
index=new_index).__finalize__(self)
912912

913-
def reshape(self, *args, **kwargs):
914-
"""
915-
.. deprecated:: 0.19.0
916-
Calling this method will raise an error. Please call
917-
``.values.reshape(...)`` instead.
918-
919-
return an ndarray with the values shape
920-
if the specified shape matches exactly the current shape, then
921-
return self (for compat)
922-
923-
See also
924-
--------
925-
numpy.ndarray.reshape
926-
"""
927-
warnings.warn("reshape is deprecated and will raise "
928-
"in a subsequent release. Please use "
929-
".values.reshape(...) instead", FutureWarning,
930-
stacklevel=2)
931-
932-
if len(args) == 1 and hasattr(args[0], '__iter__'):
933-
shape = args[0]
934-
else:
935-
shape = args
936-
937-
if tuple(shape) == self.shape:
938-
# XXX ignoring the "order" keyword.
939-
nv.validate_reshape(tuple(), kwargs)
940-
return self
941-
942-
return self._values.reshape(shape, **kwargs)
943-
944913
def get_value(self, label, takeable=False):
945914
"""
946915
Quickly retrieve single value at passed index label

pandas/tests/indexes/test_base.py

-6
Original file line numberDiff line numberDiff line change
@@ -1684,12 +1684,6 @@ def test_take_fill_value(self):
16841684
with pytest.raises(IndexError):
16851685
idx.take(np.array([1, -5]))
16861686

1687-
def test_reshape_raise(self):
1688-
msg = "reshaping is not supported"
1689-
idx = pd.Index([0, 1, 2])
1690-
tm.assert_raises_regex(NotImplementedError, msg,
1691-
idx.reshape, idx.shape)
1692-
16931687
def test_reindex_preserves_name_if_target_is_list_or_ndarray(self):
16941688
# GH6552
16951689
idx = pd.Index([0, 1, 2])

pandas/tests/reshape/test_reshape.py

-35
Original file line numberDiff line numberDiff line change
@@ -484,41 +484,6 @@ def test_reshaping_panel_categorical(self):
484484
index=p.major_axis.set_names('major'))
485485
tm.assert_frame_equal(result, expected)
486486

487-
def test_reshape_categorical(self):
488-
cat = Categorical([], categories=["a", "b"])
489-
tm.assert_produces_warning(FutureWarning, cat.reshape, 0)
490-
491-
with tm.assert_produces_warning(FutureWarning):
492-
cat = Categorical([], categories=["a", "b"])
493-
tm.assert_categorical_equal(cat.reshape(0), cat)
494-
495-
with tm.assert_produces_warning(FutureWarning):
496-
cat = Categorical([], categories=["a", "b"])
497-
tm.assert_categorical_equal(cat.reshape((5, -1)), cat)
498-
499-
with tm.assert_produces_warning(FutureWarning):
500-
cat = Categorical(["a", "b"], categories=["a", "b"])
501-
tm.assert_categorical_equal(cat.reshape(cat.shape), cat)
502-
503-
with tm.assert_produces_warning(FutureWarning):
504-
cat = Categorical(["a", "b"], categories=["a", "b"])
505-
tm.assert_categorical_equal(cat.reshape(cat.size), cat)
506-
507-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
508-
msg = "can only specify one unknown dimension"
509-
cat = Categorical(["a", "b"], categories=["a", "b"])
510-
tm.assert_raises_regex(ValueError, msg, cat.reshape, (-2, -1))
511-
512-
def test_reshape_categorical_numpy(self):
513-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
514-
cat = Categorical(["a", "b"], categories=["a", "b"])
515-
tm.assert_categorical_equal(np.reshape(cat, cat.shape), cat)
516-
517-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
518-
msg = "the 'order' parameter is not supported"
519-
tm.assert_raises_regex(ValueError, msg, np.reshape,
520-
cat, cat.shape, order='F')
521-
522487

523488
class TestMakeAxisDummies(object):
524489

pandas/tests/series/test_analytics.py

-60
Original file line numberDiff line numberDiff line change
@@ -1542,66 +1542,6 @@ def test_shift_categorical(self):
15421542
assert_index_equal(s.values.categories, sp1.values.categories)
15431543
assert_index_equal(s.values.categories, sn2.values.categories)
15441544

1545-
def test_reshape_deprecate(self):
1546-
x = Series(np.random.random(10), name='x')
1547-
tm.assert_produces_warning(FutureWarning, x.reshape, x.shape)
1548-
1549-
def test_reshape_non_2d(self):
1550-
# see gh-4554
1551-
with tm.assert_produces_warning(FutureWarning):
1552-
x = Series(np.random.random(201), name='x')
1553-
assert x.reshape(x.shape, ) is x
1554-
1555-
# see gh-2719
1556-
with tm.assert_produces_warning(FutureWarning):
1557-
a = Series([1, 2, 3, 4])
1558-
result = a.reshape(2, 2)
1559-
expected = a.values.reshape(2, 2)
1560-
tm.assert_numpy_array_equal(result, expected)
1561-
assert isinstance(result, type(expected))
1562-
1563-
def test_reshape_2d_return_array(self):
1564-
x = Series(np.random.random(201), name='x')
1565-
1566-
with tm.assert_produces_warning(FutureWarning):
1567-
result = x.reshape((-1, 1))
1568-
assert not isinstance(result, Series)
1569-
1570-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1571-
result2 = np.reshape(x, (-1, 1))
1572-
assert not isinstance(result2, Series)
1573-
1574-
with tm.assert_produces_warning(FutureWarning):
1575-
result = x[:, None]
1576-
expected = x.reshape((-1, 1))
1577-
tm.assert_almost_equal(result, expected)
1578-
1579-
def test_reshape_bad_kwarg(self):
1580-
a = Series([1, 2, 3, 4])
1581-
1582-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1583-
msg = "'foo' is an invalid keyword argument for this function"
1584-
tm.assert_raises_regex(
1585-
TypeError, msg, a.reshape, (2, 2), foo=2)
1586-
1587-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1588-
msg = r"reshape\(\) got an unexpected keyword argument 'foo'"
1589-
tm.assert_raises_regex(
1590-
TypeError, msg, a.reshape, a.shape, foo=2)
1591-
1592-
def test_numpy_reshape(self):
1593-
a = Series([1, 2, 3, 4])
1594-
1595-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1596-
result = np.reshape(a, (2, 2))
1597-
expected = a.values.reshape(2, 2)
1598-
tm.assert_numpy_array_equal(result, expected)
1599-
assert isinstance(result, type(expected))
1600-
1601-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1602-
result = np.reshape(a, a.shape)
1603-
tm.assert_series_equal(result, a)
1604-
16051545
def test_unstack(self):
16061546
from numpy import nan
16071547

0 commit comments

Comments
 (0)