Skip to content

Commit a610a0c

Browse files
author
tp
committed
change deprecation of .asobject according to comments
1 parent c70a0ca commit a610a0c

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
@@ -121,8 +121,7 @@ Deprecations
121121

122122
- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`).
123123
- ``DataFrame.as_matrix`` is deprecated. Use ``DataFrame.values`` instead (:issue:`18458`).
124-
- ``DatetimeIndex.asobject``, ``PeriodIndex.asobject`` and ``TimeDeltaIndex.asobject`` have been deprecated. Use '.astype(object)' instead (:issue:`18572`)
125-
- ``Series.asobject`` has been deprecated. Use '.astype(object).values' instead (:issue:`18572`)
124+
- ``Series.asobject``, ``DatetimeIndex.asobject``, ``PeriodIndex.asobject`` and ``TimeDeltaIndex.asobject`` have been deprecated. Use '.astype(object)' instead (:issue:`18572`)
126125

127126
.. _whatsnew_0220.prior_deprecations:
128127

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

@@ -422,14 +429,6 @@ def _isnan(self):
422429
""" return if each value is nan"""
423430
return (self.asi8 == iNaT)
424431

425-
@property
426-
def _asobject(self):
427-
"""
428-
return object Index which contains boxed values
429-
"""
430-
from pandas.core.index import Index
431-
return Index(self._box_values(self.asi8), name=self.name, dtype=object)
432-
433432
@property
434433
def asobject(self):
435434
"""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
@@ -149,7 +149,8 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
149149
_metadata = ['name']
150150
_accessors = frozenset(['dt', 'cat', 'str'])
151151
_deprecations = generic.NDFrame._deprecations | frozenset(
152-
['sortlevel', 'reshape', 'get_value', 'set_value', 'from_csv'])
152+
['asobject', 'sortlevel', 'reshape', 'get_value', 'set_value',
153+
'from_csv'])
153154
_allow_index_ops = True
154155

155156
def __init__(self, data=None, index=None, dtype=None, name=None,
@@ -449,13 +450,13 @@ def get_values(self):
449450

450451
@property
451452
def asobject(self):
452-
"""DEPRECATED: Use ``astype(object).values`` instead.
453+
"""DEPRECATED: Use ``astype(object)`` instead.
453454
454455
return object Series which contains boxed values
455456
456457
*this is an internal non-public method*
457458
"""
458-
warnings.warn("'asobject' is deprecated. Use 'astype(object).values'"
459+
warnings.warn("'asobject' is deprecated. Use 'astype(object)'"
459460
" instead", FutureWarning, stacklevel=2)
460461
return self.astype(object).values
461462

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)