Looking for a way to reliably identify if a numpy object is a view.
Related questions have come up many times before (here, here, here), and people have offered some solutions, but all seem to have problems:
- The test used in
pandas
now is to call something a view ifmy_array.base is not None
. This seems to always catch views, but also offers lots of false positives (situations where it reports something is a view even if it isn't). numpy.may_share_memory()
will check for two specific arrays, but won't answer generically- (@RobertKurn says was best tool as of 2012 -- any changes?)
flags['OWNDATA'])
is reported (third comment first answer) to fail in some cases.
(The reason for my interest is that I'm working on implementing copy-on-write for pandas, and a conservative indicator is leading to over-copying.)