Skip to content

Commit f93f999

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

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

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

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

@@ -420,14 +427,6 @@ def _isnan(self):
420427
""" return if each value is nan"""
421428
return (self.asi8 == iNaT)
422429

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-
431430
@property
432431
def asobject(self):
433432
"""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

+4-3
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ 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',
154+
'from_csv'])
154155
_allow_index_ops = True
155156

156157
def __init__(self, data=None, index=None, dtype=None, name=None,
@@ -420,13 +421,13 @@ def get_values(self):
420421

421422
@property
422423
def asobject(self):
423-
"""DEPRECATED: Use ``astype(object).values`` instead.
424+
"""DEPRECATED: Use ``astype(object)`` instead.
424425
425426
return object Series which contains boxed values
426427
427428
*this is an internal non-public method*
428429
"""
429-
warnings.warn("'asobject' is deprecated. Use 'astype(object).values'"
430+
warnings.warn("'asobject' is deprecated. Use 'astype(object)'"
430431
" instead", FutureWarning, stacklevel=2)
431432
return self.astype(object).values
432433

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)