Skip to content

Commit f1ad983

Browse files
author
tp
committed
change deprecation of .asobject according to comments
1 parent 9ec47cb commit f1ad983

File tree

11 files changed

+30
-38
lines changed

11 files changed

+30
-38
lines changed

doc/source/whatsnew/v0.22.0.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ Deprecations
124124

125125
- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`).
126126
- ``DataFrame.as_matrix`` is deprecated. Use ``DataFrame.values`` instead (:issue:`18458`).
127-
- ``DatetimeIndex.asobject``, ``PeriodIndex.asobject`` and ``TimeDeltaIndex.asobject`` have been deprecated. Use '.astype(object)' instead (:issue:`18572`)
128-
- ``Series.asobject`` has been deprecated. Use '.astype(object).values' instead (:issue:`18572`)
127+
- ``Series.asobject``, ``DatetimeIndex.asobject``, ``PeriodIndex.asobject`` and ``TimeDeltaIndex.asobject`` have been deprecated. Use '.astype(object)' instead (:issue:`18572`)
129128

130129
.. _whatsnew_0220.prior_deprecations:
131130

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
@@ -907,7 +907,7 @@ def to_datetime(self, dayfirst=False):
907907
def astype(self, dtype, copy=True):
908908
dtype = pandas_dtype(dtype)
909909
if is_object_dtype(dtype):
910-
return self._asobject
910+
return self._box_values_as_index()
911911
elif is_integer_dtype(dtype):
912912
return Index(self.values.astype('i8', copy=copy), name=self.name,
913913
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

+2-9
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,8 @@ def test_map_dictlike(self, mapper):
7878
tm.assert_index_equal(result, expected)
7979

8080
def test_asobject_deprecated(self):
81-
d = pd.date_range('2010-01-1', periods=3)
81+
# GH18572
82+
d = self.create_index()
8283
with tm.assert_produces_warning(FutureWarning):
8384
i = d.asobject
8485
assert isinstance(i, pd.Index)
85-
p = pd.period_range('2010-01-1', periods=3)
86-
with tm.assert_produces_warning(FutureWarning):
87-
i = p.asobject
88-
assert isinstance(i, pd.Index)
89-
t = pd.timedelta_range('1 day', periods=3)
90-
with tm.assert_produces_warning(FutureWarning):
91-
i = t.asobject
92-
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'),

pandas/tests/indexes/period/test_ops.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -290,27 +290,27 @@ def test_comp_nat(self):
290290
pd.Period('2011-01-03')])
291291
right = pd.PeriodIndex([pd.NaT, pd.NaT, pd.Period('2011-01-03')])
292292

293-
for l, r in [(left, right),
293+
for lhs, rhs in [(left, right),
294294
(left.astype(object), right.astype(object))]:
295-
result = r == l
295+
result = lhs == rhs
296296
expected = np.array([False, False, True])
297297
tm.assert_numpy_array_equal(result, expected)
298298

299-
result = l != r
299+
result = lhs != rhs
300300
expected = np.array([True, True, False])
301301
tm.assert_numpy_array_equal(result, expected)
302302

303303
expected = np.array([False, False, False])
304-
tm.assert_numpy_array_equal(l == pd.NaT, expected)
305-
tm.assert_numpy_array_equal(pd.NaT == r, expected)
304+
tm.assert_numpy_array_equal(lhs == pd.NaT, expected)
305+
tm.assert_numpy_array_equal(pd.NaT == rhs, expected)
306306

307307
expected = np.array([True, True, True])
308-
tm.assert_numpy_array_equal(l != pd.NaT, expected)
309-
tm.assert_numpy_array_equal(pd.NaT != l, expected)
308+
tm.assert_numpy_array_equal(lhs != pd.NaT, expected)
309+
tm.assert_numpy_array_equal(pd.NaT != lhs, expected)
310310

311311
expected = np.array([False, False, False])
312-
tm.assert_numpy_array_equal(l < pd.NaT, expected)
313-
tm.assert_numpy_array_equal(pd.NaT > l, expected)
312+
tm.assert_numpy_array_equal(lhs < pd.NaT, expected)
313+
tm.assert_numpy_array_equal(pd.NaT > lhs, expected)
314314

315315
def test_value_counts_unique(self):
316316
# GH 7735

0 commit comments

Comments
 (0)