Skip to content

Commit b45b26e

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

29 files changed

+116
-96
lines changed

asv_bench/benchmarks/index_object.py

+1-1
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

+1-1
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`` and ``Date'timeIndex.asobject`` have been deprecated. Use 'astype(object).values' instead (:issue:`18477`)
8888
-
8989

9090
.. _whatsnew_0220.prior_deprecations:

pandas/core/algorithms.py

+1-1
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

+2-2
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

+2-2
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

+13-6
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,22 @@ def _isnan(self):
421421
return (self.asi8 == iNaT)
422422

423423
@property
424-
def asobject(self):
424+
def _asobject(self):
425425
"""
426426
return object Index which contains boxed values
427-
428-
*this is an internal non-public method*
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+
__doc__ += self._asobject.__doc__
436+
warnings.warn("'._asobject' is deprecated. Use 'astype(object).values'"
437+
" instead", FutureWarning, stacklevel=2)
438+
return self._asobject
439+
433440
def _convert_tolerance(self, tolerance, target):
434441
tolerance = np.asarray(to_timedelta(tolerance, box=False))
435442
if target.size != tolerance.size and tolerance.size > 1:
@@ -466,7 +473,7 @@ def tolist(self):
466473
"""
467474
return a list of the underlying data
468475
"""
469-
return list(self.asobject)
476+
return list(self._asobject)
470477

471478
def min(self, axis=None, *args, **kwargs):
472479
"""
@@ -744,7 +751,7 @@ def isin(self, values):
744751
try:
745752
values = type(self)(values)
746753
except ValueError:
747-
return self.asobject.isin(values)
754+
return self._asobject.isin(values)
748755

749756
return algorithms.isin(self.asi8, values.asi8)
750757

pandas/core/indexes/datetimes.py

+3-3
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

+7-7
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

+2-2
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

+1-1
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

pandas/core/internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,7 @@ def _try_coerce_args(self, values, other):
21902190

21912191
if isinstance(other, ABCDatetimeIndex):
21922192
# to store DatetimeTZBlock as object
2193-
other = other.asobject.values
2193+
other = other._asobject.values
21942194

21952195
return values, False, other, False
21962196

pandas/core/ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ def wrapper(self, other, axis=None):
850850
# tested in test_nat_comparisons
851851
# (pandas.tests.series.test_operators.TestSeriesOperators)
852852
return self._constructor(na_op(self.values,
853-
other.asobject.values),
853+
other._asobject.values),
854854
index=self.index)
855855

856856
return self._constructor(na_op(self.values, np.asarray(other)),

pandas/core/series.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
209209
# coerce back to datetime objects for lookup
210210
data = _dict_compat(data)
211211
data = lib.fast_multiget(data,
212-
index.asobject.values,
212+
index._asobject.values,
213213
default=np.nan)
214214
else:
215215
data = np.nan
@@ -433,13 +433,21 @@ def get_values(self):
433433
return self._data.get_values()
434434

435435
@property
436-
def asobject(self):
436+
def _asobject(self):
437437
"""
438438
return object Series which contains boxed values
439+
"""
440+
return self._data._asobject
439441

440-
*this is an internal non-public method*
442+
@property
443+
def asobject(self):
444+
"""DEPRECATED: '.asobject' is deprecated. Use 'astype(object).values'
445+
instead.
441446
"""
442-
return self._data.asobject
447+
__doc__ += self._asobject.__doc__
448+
warnings.warn("asobject is deprecated. Use 'astype(object).values'"
449+
" instead", FutureWarning, stacklevel=2)
450+
return self._asobject
443451

444452
# ops
445453
def ravel(self, order='C'):
@@ -1307,7 +1315,7 @@ def unique(self):
13071315
# to return an object array of tz-aware Timestamps
13081316

13091317
# TODO: it must return DatetimeArray with tz in pandas 2.0
1310-
result = result.asobject.values
1318+
result = result._asobject.values
13111319

13121320
return result
13131321

@@ -2345,7 +2353,7 @@ def map(self, arg, na_action=None):
23452353
raise NotImplementedError
23462354
map_f = lambda values, f: values.map(f)
23472355
else:
2348-
values = self.asobject
2356+
values = self._asobject
23492357

23502358
if na_action == 'ignore':
23512359
def map_f(values, f):
@@ -2567,7 +2575,7 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
25672575
if is_extension_type(self.dtype):
25682576
mapped = self._values.map(f)
25692577
else:
2570-
values = self.asobject
2578+
values = self._asobject
25712579
mapped = lib.map_infer(values, f, convert=convert_dtype)
25722580

25732581
if len(mapped) and isinstance(mapped[0], Series):
@@ -3143,7 +3151,7 @@ def _sanitize_index(data, index, copy=False):
31433151
if isinstance(data, ABCIndexClass) and not copy:
31443152
pass
31453153
elif isinstance(data, PeriodIndex):
3146-
data = data.asobject
3154+
data = data._asobject
31473155
elif isinstance(data, DatetimeIndex):
31483156
data = data._to_embed(keep_tz=True)
31493157
elif isinstance(data, np.ndarray):

pandas/io/formats/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,7 @@ class Datetime64TZFormatter(Datetime64Formatter):
22312231
def _format_strings(self):
22322232
""" we by definition have a TZ """
22332233

2234-
values = self.values.asobject
2234+
values = self.values._asobject
22352235
is_dates_only = _is_dates_only(values)
22362236
formatter = (self.formatter or
22372237
_get_format_datetime64(is_dates_only,

pandas/plotting/_converter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def __call__(self):
363363
tz = self.tz.tzname(None)
364364
st = _from_ordinal(dates.date2num(dmin)) # strip tz
365365
ed = _from_ordinal(dates.date2num(dmax))
366-
all_dates = date_range(start=st, end=ed, freq=freq, tz=tz).asobject
366+
all_dates = date_range(start=st, end=ed, freq=freq, tz=tz)._asobject
367367

368368
try:
369369
if len(all_dates) > 0:

pandas/tests/frame/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@ def test_constructor_period(self):
500500
assert df['b'].dtype == 'object'
501501

502502
# list of periods
503-
df = pd.DataFrame({'a': a.asobject.tolist(),
504-
'b': b.asobject.tolist()})
503+
df = pd.DataFrame({'a': a._asobject.tolist(),
504+
'b': b._asobject.tolist()})
505505
assert df['a'].dtype == 'object'
506506
assert df['b'].dtype == 'object'
507507

0 commit comments

Comments
 (0)