Skip to content

Commit 7c9ccc7

Browse files
author
tp
committed
change deprecation of .asobject according to comments
1 parent 4cb8776 commit 7c9ccc7

File tree

10 files changed

+22
-29
lines changed

10 files changed

+22
-29
lines changed

doc/source/whatsnew/v0.22.0.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ Deprecations
8989

9090
- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`).
9191
- ``DataFrame.as_matrix`` is deprecated. Use ``DataFrame.values`` instead (:issue:`18458`).
92-
- ``DatetimeIndex.asobject``, ``PeriodIndex.asobject`` and ``TimeDeltaIndex.asobject`` have been deprecated. Use '.astype(object)' instead (:issue:`18572`)
93-
- ``Series.asobject`` has been deprecated. Use '.astype(object).values' instead (:issue:`18572`)
92+
- ``Series.asobject``, ``DatetimeIndex.asobject``, ``PeriodIndex.asobject`` and ``TimeDeltaIndex.asobject`` have been deprecated. Use '.astype(object)' instead (:issue:`18572`)
9493

9594
.. _whatsnew_0220.prior_deprecations:
9695

pandas/_libs/algos_common_helper.pxi.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,8 @@ cpdef ensure_object(object arr):
552552
return arr
553553
else:
554554
return arr.astype(np.object_)
555-
elif hasattr(arr, '_asobject'):
556-
return arr._asobject
555+
elif hasattr(arr, '_box_values_as_index'):
556+
return arr._box_values_as_index
557557
else:
558558
return np.array(arr, dtype=np.object_)
559559

pandas/core/accessor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class DirNamesMixin(object):
1212
_accessors = frozenset([])
13-
_deprecations = frozenset([])
13+
_deprecations = frozenset(['asobject'])
1414

1515
def _dir_deletions(self):
1616
""" delete unwanted __dir__ for this object """

pandas/core/indexes/datetimelike.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ def _box_values(self, values):
242242
"""
243243
return lib.map_infer(values, self._box_func)
244244

245+
@property
246+
def _box_values_as_index(self):
247+
"""
248+
return object Index which contains boxed values
249+
"""
250+
from pandas.core.index import Index
251+
return Index(self._box_values(self.asi8), name=self.name, dtype=object)
252+
245253
def _format_with_header(self, header, **kwargs):
246254
return header + list(self._format_native_types(**kwargs))
247255

@@ -420,14 +428,6 @@ def _isnan(self):
420428
""" return if each value is nan"""
421429
return (self.asi8 == iNaT)
422430

423-
@property
424-
def _asobject(self):
425-
"""
426-
return object Index which contains boxed values
427-
"""
428-
from pandas.core.index import Index
429-
return Index(self._box_values(self.asi8), name=self.name, dtype=object)
430-
431431
@property
432432
def asobject(self):
433433
"""DEPRECATED: Use ``astype(object)`` instead.

pandas/core/indexes/datetimes.py

+1-1
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._box_values_as_index
910910
elif is_integer_dtype(dtype):
911911
return Index(self.values.astype('i8', copy=copy), name=self.name,
912912
dtype='i8')

pandas/core/indexes/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -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._box_values_as_index
510510
elif is_integer_dtype(dtype):
511511
if copy:
512512
return self._int64index.copy()

pandas/core/indexes/timedeltas.py

+1-1
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._box_values_as_index
486486
elif is_timedelta64_ns_dtype(dtype):
487487
if copy is True:
488488
return self.copy()

pandas/core/series.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
150150
_metadata = ['name']
151151
_accessors = frozenset(['dt', 'cat', 'str'])
152152
_deprecations = generic.NDFrame._deprecations | frozenset(
153-
['sortlevel', 'reshape', 'get_value', 'set_value', 'from_csv'])
153+
['asobject', 'sortlevel', 'reshape', 'get_value', 'set_value', 'from_csv'])
154154
_allow_index_ops = True
155155

156156
def __init__(self, data=None, index=None, dtype=None, name=None,
@@ -420,13 +420,13 @@ def get_values(self):
420420

421421
@property
422422
def asobject(self):
423-
"""DEPRECATED: Use ``astype(object).values`` instead.
423+
"""DEPRECATED: Use ``astype(object)`` instead.
424424
425425
return object Series which contains boxed values
426426
427427
*this is an internal non-public method*
428428
"""
429-
warnings.warn("'asobject' is deprecated. Use 'astype(object).values'"
429+
warnings.warn("'asobject' is deprecated. Use 'astype(object)'"
430430
" instead", FutureWarning, stacklevel=2)
431431
return self.astype(object).values
432432

pandas/tests/indexes/datetimelike.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,9 @@ def test_map_dictlike(self, mapper):
7777
tm.assert_index_equal(result, expected)
7878

7979
def test_asobject_deprecated(self):
80-
d = pd.date_range('2010-01-1', periods=3)
80+
# GH18572
81+
d = self.create_index()
82+
print(d)
8183
with tm.assert_produces_warning(FutureWarning):
8284
i = d.asobject
8385
assert isinstance(i, pd.Index)
84-
p = pd.period_range('2010-01-1', periods=3)
85-
with tm.assert_produces_warning(FutureWarning):
86-
i = p.asobject
87-
assert isinstance(i, pd.Index)
88-
t = pd.timedelta_range('1 day', periods=3)
89-
with tm.assert_produces_warning(FutureWarning):
90-
i = t.asobject
91-
assert isinstance(i, pd.Index)

pandas/tests/indexes/datetimes/test_ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_ops_properties_basic(self):
5151
assert s.day == 10
5252
pytest.raises(AttributeError, lambda: s.weekday)
5353

54-
def test_astype(self):
54+
def test_astype_object(self):
5555
idx = pd.date_range(start='2013-01-01', periods=4, freq='M',
5656
name='idx')
5757
expected_list = [Timestamp('2013-01-31'),

0 commit comments

Comments
 (0)