Skip to content

Commit

Permalink
change deprecation of .asobject according to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tp committed Dec 1, 2017
1 parent 4cb8776 commit 7c9ccc7
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 29 deletions.
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ Deprecations

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

.. _whatsnew_0220.prior_deprecations:

Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/algos_common_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,8 @@ cpdef ensure_object(object arr):
return arr
else:
return arr.astype(np.object_)
elif hasattr(arr, '_asobject'):
return arr._asobject
elif hasattr(arr, '_box_values_as_index'):
return arr._box_values_as_index
else:
return np.array(arr, dtype=np.object_)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class DirNamesMixin(object):
_accessors = frozenset([])
_deprecations = frozenset([])
_deprecations = frozenset(['asobject'])

def _dir_deletions(self):
""" delete unwanted __dir__ for this object """
Expand Down
16 changes: 8 additions & 8 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ def _box_values(self, values):
"""
return lib.map_infer(values, self._box_func)

@property
def _box_values_as_index(self):
"""
return object Index which contains boxed values
"""
from pandas.core.index import Index
return Index(self._box_values(self.asi8), name=self.name, dtype=object)

def _format_with_header(self, header, **kwargs):
return header + list(self._format_native_types(**kwargs))

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

@property
def _asobject(self):
"""
return object Index which contains boxed values
"""
from pandas.core.index import Index
return Index(self._box_values(self.asi8), name=self.name, dtype=object)

@property
def asobject(self):
"""DEPRECATED: Use ``astype(object)`` instead.
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ def to_datetime(self, dayfirst=False):
def astype(self, dtype, copy=True):
dtype = pandas_dtype(dtype)
if is_object_dtype(dtype):
return self._asobject
return self._box_values_as_index
elif is_integer_dtype(dtype):
return Index(self.values.astype('i8', copy=copy), name=self.name,
dtype='i8')
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ def asof_locs(self, where, mask):
def astype(self, dtype, copy=True, how='start'):
dtype = pandas_dtype(dtype)
if is_object_dtype(dtype):
return self._asobject
return self._box_values_as_index
elif is_integer_dtype(dtype):
if copy:
return self._int64index.copy()
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def astype(self, dtype, copy=True):
dtype = np.dtype(dtype)

if is_object_dtype(dtype):
return self._asobject
return self._box_values_as_index
elif is_timedelta64_ns_dtype(dtype):
if copy is True:
return self.copy()
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
_metadata = ['name']
_accessors = frozenset(['dt', 'cat', 'str'])
_deprecations = generic.NDFrame._deprecations | frozenset(
['sortlevel', 'reshape', 'get_value', 'set_value', 'from_csv'])
['asobject', 'sortlevel', 'reshape', 'get_value', 'set_value', 'from_csv'])
_allow_index_ops = True

def __init__(self, data=None, index=None, dtype=None, name=None,
Expand Down Expand Up @@ -420,13 +420,13 @@ def get_values(self):

@property
def asobject(self):
"""DEPRECATED: Use ``astype(object).values`` instead.
"""DEPRECATED: Use ``astype(object)`` instead.
return object Series which contains boxed values
*this is an internal non-public method*
"""
warnings.warn("'asobject' is deprecated. Use 'astype(object).values'"
warnings.warn("'asobject' is deprecated. Use 'astype(object)'"
" instead", FutureWarning, stacklevel=2)
return self.astype(object).values

Expand Down
12 changes: 3 additions & 9 deletions pandas/tests/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,9 @@ def test_map_dictlike(self, mapper):
tm.assert_index_equal(result, expected)

def test_asobject_deprecated(self):
d = pd.date_range('2010-01-1', periods=3)
# GH18572
d = self.create_index()
print(d)
with tm.assert_produces_warning(FutureWarning):
i = d.asobject
assert isinstance(i, pd.Index)
p = pd.period_range('2010-01-1', periods=3)
with tm.assert_produces_warning(FutureWarning):
i = p.asobject
assert isinstance(i, pd.Index)
t = pd.timedelta_range('1 day', periods=3)
with tm.assert_produces_warning(FutureWarning):
i = t.asobject
assert isinstance(i, pd.Index)
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_ops_properties_basic(self):
assert s.day == 10
pytest.raises(AttributeError, lambda: s.weekday)

def test_astype(self):
def test_astype_object(self):
idx = pd.date_range(start='2013-01-01', periods=4, freq='M',
name='idx')
expected_list = [Timestamp('2013-01-31'),
Expand Down

0 comments on commit 7c9ccc7

Please sign in to comment.