From 71bb9ba1373aecea606f4c71e71eb73433d50691 Mon Sep 17 00:00:00 2001 From: Skipper Seabold Date: Sat, 4 Jan 2014 22:28:04 -0500 Subject: [PATCH 1/2] ENH: Refactor code to add is_view method for Series. --- pandas/core/series.py | 21 +++++++++++++++------ pandas/tests/test_series.py | 7 +++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index f147eb87d7480..ec4fd3aa18b94 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1645,6 +1645,20 @@ def update(self, other): #---------------------------------------------------------------------- # Reindexing, sorting + def is_view(self): + """ + Return True if series is a view of some other array, False otherwise. + """ + true_base = self.values + while true_base.base is not None: + true_base = true_base.base + + if (true_base is not None and + (true_base.ndim != 1 or true_base.shape != self.shape)): + return True + return False + + def sort(self, axis=0, kind='quicksort', order=None, ascending=True): """ Sort values and index labels by value, in place. For compatibility with @@ -1667,12 +1681,7 @@ def sort(self, axis=0, kind='quicksort', order=None, ascending=True): sortedSeries = self.order(na_last=True, kind=kind, ascending=ascending) - true_base = self.values - while true_base.base is not None: - true_base = true_base.base - - if (true_base is not None and - (true_base.ndim != 1 or true_base.shape != self.shape)): + if self.is_view(): raise TypeError('This Series is a view of some other array, to ' 'sort in-place you must create a copy') diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index e8b421608fc0a..281e88b7bdba4 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -5548,6 +5548,13 @@ def test_unique_data_ownership(self): # it works! #1807 Series(Series(["a", "c", "b"]).unique()).sort() +def test_is_view(): + df = tm.makeDataFrame() + view = df['A'].is_view() + tm.assert_equal(view, True) + ser = tm.makeStringSeries() + view = ser.is_view() + tm.assert_equal(view, False) if __name__ == '__main__': nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], From ce0f0f7136deb0654f09c9737345304455df8a4a Mon Sep 17 00:00:00 2001 From: Skipper Seabold Date: Sat, 4 Jan 2014 23:22:59 -0500 Subject: [PATCH 2/2] DOC: Add is_view to release notes --- doc/source/v0.13.1.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/source/v0.13.1.txt b/doc/source/v0.13.1.txt index 250adffdadbca..0ea6b161107e7 100644 --- a/doc/source/v0.13.1.txt +++ b/doc/source/v0.13.1.txt @@ -29,6 +29,8 @@ Deprecations Enhancements ~~~~~~~~~~~~ +Added an ``is_view`` method to Series. + Experimental ~~~~~~~~~~~~