Skip to content

Commit b2f1438

Browse files
author
tp
committed
deprecate .asobject
1 parent aaee541 commit b2f1438

29 files changed

+120
-98
lines changed

asv_bench/benchmarks/index_object.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def setup(self):
1212
if (self.rng.dtype == object):
1313
self.idx_rng = self.rng.view(Index)
1414
else:
15-
self.idx_rng = self.rng.asobject
15+
self.idx_rng = self.rng._asobject
1616
self.idx_rng2 = self.idx_rng[:(-1)]
1717

1818
# other datetime

doc/source/whatsnew/v0.22.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Deprecations
8484
~~~~~~~~~~~~
8585

8686
- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`).
87-
-
87+
- ``Series.asobject``, ``DatetimeIndex.asobject`` and ``PeriodIndex.asobject``have been deprecated. Use '.astype(object).values' instead (:issue:`18477`)
8888
-
8989

9090
.. _whatsnew_0220.prior_deprecations:

pandas/core/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def unique(values):
369369
# to return an object array of tz-aware Timestamps
370370

371371
# TODO: it must return DatetimeArray with tz in pandas 2.0
372-
uniques = uniques.asobject.values
372+
uniques = uniques._asobject.values
373373

374374
return uniques
375375

pandas/core/dtypes/concat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def convert_to_pydatetime(x, axis):
401401
# if dtype is of datetimetz or timezone
402402
if x.dtype.kind == _NS_DTYPE.kind:
403403
if getattr(x, 'tz', None) is not None:
404-
x = x.asobject.values
404+
x = x._asobject.values
405405
else:
406406
shape = x.shape
407407
x = tslib.ints_to_pydatetime(x.view(np.int64).ravel(),
@@ -479,7 +479,7 @@ def _concat_index_asobject(to_concat, name=None):
479479
"""
480480

481481
klasses = ABCDatetimeIndex, ABCTimedeltaIndex, ABCPeriodIndex
482-
to_concat = [x.asobject if isinstance(x, klasses) else x
482+
to_concat = [x._asobject if isinstance(x, klasses) else x
483483
for x in to_concat]
484484

485485
from pandas import Index

pandas/core/frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3305,7 +3305,7 @@ class max type
33053305

33063306
def _maybe_casted_values(index, labels=None):
33073307
if isinstance(index, PeriodIndex):
3308-
values = index.asobject.values
3308+
values = index._asobject.values
33093309
elif isinstance(index, DatetimeIndex) and index.tz is not None:
33103310
values = index
33113311
else:
@@ -5052,7 +5052,7 @@ def applymap(self, func):
50525052
def infer(x):
50535053
if x.empty:
50545054
return lib.map_infer(x, func)
5055-
return lib.map_infer(x.asobject, func)
5055+
return lib.map_infer(x._asobject, func)
50565056

50575057
return self.apply(infer)
50585058

pandas/core/indexes/datetimelike.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def map(self, f):
358358
raise TypeError('The map function must return an Index object')
359359
return result
360360
except Exception:
361-
return self.asobject.map(f)
361+
return self._asobject.map(f)
362362

363363
def sort_values(self, return_indexer=False, ascending=True):
364364
"""
@@ -421,15 +421,23 @@ def _isnan(self):
421421
return (self.asi8 == iNaT)
422422

423423
@property
424-
def asobject(self):
424+
def _asobject(self):
425425
"""
426-
return object Index which contains boxed values
427-
428-
*this is an internal non-public method*
426+
Return object Index which contains boxed values
429427
"""
430428
from pandas.core.index import Index
431429
return Index(self._box_values(self.asi8), name=self.name, dtype=object)
432430

431+
@property
432+
def asobject(self):
433+
"""DEPRECATED: Use 'astype(object).values' instead.
434+
435+
Return object Index which contains boxed values
436+
"""
437+
warnings.warn("'.asobject' is deprecated. Use 'astype(object).values'"
438+
" instead", FutureWarning, stacklevel=2)
439+
return self._asobject
440+
433441
def _convert_tolerance(self, tolerance, target):
434442
tolerance = np.asarray(to_timedelta(tolerance, box=False))
435443
if target.size != tolerance.size and tolerance.size > 1:
@@ -466,7 +474,7 @@ def tolist(self):
466474
"""
467475
return a list of the underlying data
468476
"""
469-
return list(self.asobject)
477+
return list(self._asobject)
470478

471479
def min(self, axis=None, *args, **kwargs):
472480
"""
@@ -744,7 +752,7 @@ def isin(self, values):
744752
try:
745753
values = type(self)(values)
746754
except ValueError:
747-
return self.asobject.isin(values)
755+
return self._asobject.isin(values)
748756

749757
return algorithms.isin(self.asi8, values.asi8)
750758

pandas/core/indexes/datetimes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ def to_datetime(self, dayfirst=False):
906906
def astype(self, dtype, copy=True):
907907
dtype = pandas_dtype(dtype)
908908
if is_object_dtype(dtype):
909-
return self.asobject
909+
return self._asobject
910910
elif is_integer_dtype(dtype):
911911
return Index(self.values.astype('i8', copy=copy), name=self.name,
912912
dtype='i8')
@@ -1678,7 +1678,7 @@ def time(self):
16781678
Returns numpy array of datetime.time. The time part of the Timestamps.
16791679
"""
16801680
return self._maybe_mask_results(libalgos.arrmap_object(
1681-
self.asobject.values,
1681+
self._asobject.values,
16821682
lambda x: np.nan if x is libts.NaT else x.time()))
16831683

16841684
@property
@@ -1785,7 +1785,7 @@ def insert(self, loc, item):
17851785

17861786
# fall back to object index
17871787
if isinstance(item, compat.string_types):
1788-
return self.asobject.insert(loc, item)
1788+
return self._asobject.insert(loc, item)
17891789
raise TypeError(
17901790
"cannot insert DatetimeIndex with incompatible label")
17911791

pandas/core/indexes/period.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def _int64index(self):
418418

419419
@property
420420
def values(self):
421-
return self.asobject.values
421+
return self._asobject.values
422422

423423
@property
424424
def _values(self):
@@ -428,7 +428,7 @@ def __array__(self, dtype=None):
428428
if is_integer_dtype(dtype):
429429
return self.asi8
430430
else:
431-
return self.asobject.values
431+
return self._asobject.values
432432

433433
def __array_wrap__(self, result, context=None):
434434
"""
@@ -476,7 +476,7 @@ def _to_embed(self, keep_tz=False, dtype=None):
476476
if dtype is not None:
477477
return self.astype(dtype)._to_embed(keep_tz=keep_tz)
478478

479-
return self.asobject.values
479+
return self._asobject.values
480480

481481
@property
482482
def _formatter_func(self):
@@ -506,7 +506,7 @@ def asof_locs(self, where, mask):
506506
def astype(self, dtype, copy=True, how='start'):
507507
dtype = pandas_dtype(dtype)
508508
if is_object_dtype(dtype):
509-
return self.asobject
509+
return self._asobject
510510
elif is_integer_dtype(dtype):
511511
if copy:
512512
return self._int64index.copy()
@@ -656,7 +656,7 @@ def end_time(self):
656656

657657
def _mpl_repr(self):
658658
# how to represent ourselves to matplotlib
659-
return self.asobject.values
659+
return self._asobject.values
660660

661661
def to_timestamp(self, freq=None, how='start'):
662662
"""
@@ -971,7 +971,7 @@ def _convert_tolerance(self, tolerance, target):
971971

972972
def insert(self, loc, item):
973973
if not isinstance(item, Period) or self.freq != item.freq:
974-
return self.asobject.insert(loc, item)
974+
return self._asobject.insert(loc, item)
975975

976976
idx = np.concatenate((self[:loc].asi8, np.array([item.ordinal]),
977977
self[loc:].asi8))
@@ -1018,7 +1018,7 @@ def _apply_meta(self, rawarr):
10181018
def _format_native_types(self, na_rep=u('NaT'), date_format=None,
10191019
**kwargs):
10201020

1021-
values = self.asobject.values
1021+
values = self._asobject.values
10221022

10231023
if date_format:
10241024
formatter = lambda dt: dt.strftime(date_format)

pandas/core/indexes/timedeltas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ def astype(self, dtype, copy=True):
482482
dtype = np.dtype(dtype)
483483

484484
if is_object_dtype(dtype):
485-
return self.asobject
485+
return self._asobject
486486
elif is_timedelta64_ns_dtype(dtype):
487487
if copy is True:
488488
return self.copy()
@@ -881,7 +881,7 @@ def insert(self, loc, item):
881881

882882
# fall back to object index
883883
if isinstance(item, compat.string_types):
884-
return self.asobject.insert(loc, item)
884+
return self._asobject.insert(loc, item)
885885
raise TypeError(
886886
"cannot insert TimedeltaIndex with incompatible label")
887887

pandas/core/indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def _setitem_with_indexer(self, indexer, value):
405405
new_values = np.concatenate([self.obj._values,
406406
new_values])
407407
except TypeError:
408-
new_values = np.concatenate([self.obj.asobject,
408+
new_values = np.concatenate([self.obj._asobject,
409409
new_values])
410410
self.obj._data = self.obj._constructor(
411411
new_values, index=new_index, name=self.obj.name)._data

0 commit comments

Comments
 (0)