Skip to content

Commit 21e884f

Browse files
jorisvandenbosscheTomAugspurger
authored andcommitted
DEPR: Series ndarray properties (strides, data, base, itemsize, flags) (#20721)
* DEPR: Series ndarray properties (strides, data, base, itemsize, flags)
1 parent 4aac0c8 commit 21e884f

File tree

12 files changed

+51
-12
lines changed

12 files changed

+51
-12
lines changed

doc/source/whatsnew/v0.23.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,9 @@ Deprecations
886886
- The ``convert_datetime64`` parameter in :func:`DataFrame.to_records` has been deprecated and will be removed in a future version. The NumPy bug motivating this parameter has been resolved. The default value for this parameter has also changed from ``True`` to ``None`` (:issue:`18160`).
887887
- :func:`Series.rolling().apply() <pandas.core.window.Rolling.apply>`, :func:`DataFrame.rolling().apply() <pandas.core.window.Rolling.apply>`,
888888
:func:`Series.expanding().apply() <pandas.core.window.Expanding.apply>`, and :func:`DataFrame.expanding().apply() <pandas.core.window.Expanding.apply>` have deprecated passing an ``np.array`` by default. One will need to pass the new ``raw`` parameter to be explicit about what is passed (:issue:`20584`)
889+
- The ``data``, ``base``, ``strides``, ``flags`` and ``itemsize`` properties
890+
of the ``Series`` and ``Index`` classes have been deprecated and will be
891+
removed in a future version (:issue:`20419`).
889892
- ``DatetimeIndex.offset`` is deprecated. Use ``DatetimeIndex.freq`` instead (:issue:`20716`)
890893
- ``Index.get_duplicates()`` is deprecated and will be removed in a future version (:issue:`20239`)
891894

pandas/core/algorithms.py

+2
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,8 @@ def take_nd(arr, indexer, axis=0, out=None, fill_value=np.nan, mask_info=None,
14981498
if is_sparse(arr):
14991499
arr = arr.get_values()
15001500

1501+
arr = np.asarray(arr)
1502+
15011503
if indexer is None:
15021504
indexer = np.arange(arr.shape[axis], dtype=np.int64)
15031505
dtype, fill_value = arr.dtype, arr.dtype.type()

pandas/core/base.py

+15
Original file line numberDiff line numberDiff line change
@@ -737,11 +737,17 @@ def item(self):
737737
@property
738738
def data(self):
739739
""" return the data pointer of the underlying data """
740+
warnings.warn("{obj}.data is deprecated and will be removed "
741+
"in a future version".format(obj=type(self).__name__),
742+
FutureWarning, stacklevel=2)
740743
return self.values.data
741744

742745
@property
743746
def itemsize(self):
744747
""" return the size of the dtype of the item of the underlying data """
748+
warnings.warn("{obj}.itemsize is deprecated and will be removed "
749+
"in a future version".format(obj=type(self).__name__),
750+
FutureWarning, stacklevel=2)
745751
return self._ndarray_values.itemsize
746752

747753
@property
@@ -752,6 +758,9 @@ def nbytes(self):
752758
@property
753759
def strides(self):
754760
""" return the strides of the underlying data """
761+
warnings.warn("{obj}.strudes is deprecated and will be removed "
762+
"in a future version".format(obj=type(self).__name__),
763+
FutureWarning, stacklevel=2)
755764
return self._ndarray_values.strides
756765

757766
@property
@@ -762,13 +771,19 @@ def size(self):
762771
@property
763772
def flags(self):
764773
""" return the ndarray.flags for the underlying data """
774+
warnings.warn("{obj}.flags is deprecated and will be removed "
775+
"in a future version".format(obj=type(self).__name__),
776+
FutureWarning, stacklevel=2)
765777
return self.values.flags
766778

767779
@property
768780
def base(self):
769781
""" return the base object if the memory of the underlying data is
770782
shared
771783
"""
784+
warnings.warn("{obj}.base is deprecated and will be removed "
785+
"in a future version".format(obj=type(self).__name__),
786+
FutureWarning, stacklevel=2)
772787
return self.values.base
773788

774789
@property

pandas/core/indexes/base.py

+3
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,9 @@ def _deepcopy_if_needed(self, orig, copy=False):
550550
"""
551551
if copy:
552552
# Retrieve the "base objects", i.e. the original memory allocations
553+
if not isinstance(orig, np.ndarray):
554+
# orig is a DatetimeIndex
555+
orig = orig.values
553556
orig = orig if orig.base is None else orig.base
554557
new = self._data if self._data.base is None else self._data.base
555558
if orig is new:

pandas/core/internals.py

+6
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,12 @@ def _maybe_coerce_values(self, values, dtype=None):
28162816

28172817
return values
28182818

2819+
@property
2820+
def is_view(self):
2821+
""" return a boolean if I am possibly a view """
2822+
# check the ndarray values of the DatetimeIndex values
2823+
return self.values.values.base is not None
2824+
28192825
def copy(self, deep=True, mgr=None):
28202826
""" copy constructor """
28212827
values = self.values

pandas/tests/groupby/aggregate/test_other.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def test_series_agg_multi_pure_python():
328328
'F': np.random.randn(11)})
329329

330330
def bad(x):
331-
assert (len(x.base) > 0)
331+
assert (len(x.values.base) > 0)
332332
return 'foo'
333333

334334
result = data.groupby(['A', 'B']).agg(bad)

pandas/tests/groupby/test_groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ def convert_fast(x):
10661066

10671067
def convert_force_pure(x):
10681068
# base will be length 0
1069-
assert (len(x.base) > 0)
1069+
assert (len(x.values.base) > 0)
10701070
return Decimal(str(x.mean()))
10711071

10721072
grouped = s.groupby(labels)

pandas/tests/indexes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class Base(object):
2525
""" base class for index sub-class tests """
2626
_holder = None
27-
_compat_props = ['shape', 'ndim', 'size', 'itemsize', 'nbytes']
27+
_compat_props = ['shape', 'ndim', 'size', 'nbytes']
2828

2929
def setup_indices(self):
3030
for name, idx in self.indices.items():

pandas/tests/indexes/test_multi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
class TestMultiIndex(Base):
3232
_holder = MultiIndex
33-
_compat_props = ['shape', 'ndim', 'size', 'itemsize']
33+
_compat_props = ['shape', 'ndim', 'size']
3434

3535
def setup_method(self, method):
3636
major_axis = Index(['foo', 'bar', 'baz', 'qux'])

pandas/tests/indexes/test_range.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
class TestRangeIndex(Numeric):
2424
_holder = RangeIndex
25-
_compat_props = ['shape', 'ndim', 'size', 'itemsize']
25+
_compat_props = ['shape', 'ndim', 'size']
2626

2727
def setup_method(self, method):
2828
self.indices = dict(index=RangeIndex(0, 20, 2, name='foo'),

pandas/tests/internals/test_internals.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -466,16 +466,20 @@ def test_copy(self, mgr):
466466

467467
# view assertion
468468
assert cp_blk.equals(blk)
469-
assert cp_blk.values.base is blk.values.base
469+
if isinstance(blk.values, np.ndarray):
470+
assert cp_blk.values.base is blk.values.base
471+
else:
472+
# DatetimeTZBlock has DatetimeIndex values
473+
assert cp_blk.values.values.base is blk.values.values.base
470474

471475
cp = mgr.copy(deep=True)
472476
for blk, cp_blk in zip(mgr.blocks, cp.blocks):
473477

474478
# copy assertion we either have a None for a base or in case of
475479
# some blocks it is an array (e.g. datetimetz), but was copied
476480
assert cp_blk.equals(blk)
477-
if cp_blk.values.base is not None and blk.values.base is not None:
478-
assert cp_blk.values.base is not blk.values.base
481+
if not isinstance(cp_blk.values, np.ndarray):
482+
assert cp_blk.values.values.base is not blk.values.values.base
479483
else:
480484
assert cp_blk.values.base is None and blk.values.base is None
481485

pandas/tests/test_base.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -316,16 +316,22 @@ def test_ndarray_compat_properties(self):
316316

317317
for o in self.objs:
318318
# Check that we work.
319-
for p in ['shape', 'dtype', 'flags', 'T',
320-
'strides', 'itemsize', 'nbytes']:
319+
for p in ['shape', 'dtype', 'T', 'nbytes']:
321320
assert getattr(o, p, None) is not None
322321

323-
assert hasattr(o, 'base')
322+
# deprecated properties
323+
for p in ['flags', 'strides', 'itemsize']:
324+
with tm.assert_produces_warning(FutureWarning):
325+
assert getattr(o, p, None) is not None
326+
327+
with tm.assert_produces_warning(FutureWarning):
328+
assert hasattr(o, 'base')
324329

325330
# If we have a datetime-like dtype then needs a view to work
326331
# but the user is responsible for that
327332
try:
328-
assert o.data is not None
333+
with tm.assert_produces_warning(FutureWarning):
334+
assert o.data is not None
329335
except ValueError:
330336
pass
331337

0 commit comments

Comments
 (0)