8000 ENH: Refactor code to add is_view method for Series. by jseabold · Pull Request #5853 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: Refactor code to add is_view method for Series. #5853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Jan 5, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ENH: Refactor code to add is_view method for Series.
  • Loading branch information
jseabold committed Jan 5, 2014
commit 71bb9ba1373aecea606f4c71e71eb73433d50691
21 changes: 15 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -1667,12 +1681, 8000 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')

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
0