From 7c9ccc7c7cedb65f323eef255dcd927f8cad5bb7 Mon Sep 17 00:00:00 2001 From: tp Date: Thu, 30 Nov 2017 18:34:59 +0000 Subject: [PATCH] change deprecation of .asobject according to comments --- doc/source/whatsnew/v0.22.0.txt | 3 +-- pandas/_libs/algos_common_helper.pxi.in | 4 ++-- pandas/core/accessor.py | 2 +- pandas/core/indexes/datetimelike.py | 16 ++++++++-------- pandas/core/indexes/datetimes.py | 2 +- pandas/core/indexes/period.py | 2 +- pandas/core/indexes/timedeltas.py | 2 +- pandas/core/series.py | 6 +++--- pandas/tests/indexes/datetimelike.py | 12 +++--------- pandas/tests/indexes/datetimes/test_ops.py | 2 +- 10 files changed, 22 insertions(+), 29 deletions(-) diff --git a/doc/source/whatsnew/v0.22.0.txt b/doc/source/whatsnew/v0.22.0.txt index fa384dc6fc6850..2f1095ba0608b3 100644 --- a/doc/source/whatsnew/v0.22.0.txt +++ b/doc/source/whatsnew/v0.22.0.txt @@ -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: diff --git a/pandas/_libs/algos_common_helper.pxi.in b/pandas/_libs/algos_common_helper.pxi.in index 1d93ea2ece1057..8f277235ccd5bc 100644 --- a/pandas/_libs/algos_common_helper.pxi.in +++ b/pandas/_libs/algos_common_helper.pxi.in @@ -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_) diff --git a/pandas/core/accessor.py b/pandas/core/accessor.py index 7a2da9655cc4a0..53ead5e8f74a33 100644 --- a/pandas/core/accessor.py +++ b/pandas/core/accessor.py @@ -10,7 +10,7 @@ class DirNamesMixin(object): _accessors = frozenset([]) - _deprecations = frozenset([]) + _deprecations = frozenset(['asobject']) def _dir_deletions(self): """ delete unwanted __dir__ for this object """ diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index ebe9e01d6be892..ddb420e642856a 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -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)) @@ -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. diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 448dd9f1338f1a..8bd733fc936d7a 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -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') diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index a2201c38ca52a0..1514173d9bf481 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -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() diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index a3d1fa197381ac..37471a474845a3 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -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() diff --git a/pandas/core/series.py b/pandas/core/series.py index 2eb5a0cc2c951f..921c622734644b 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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, @@ -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 diff --git a/pandas/tests/indexes/datetimelike.py b/pandas/tests/indexes/datetimelike.py index 5f8dea56b4830c..b7da0968d3a36d 100644 --- a/pandas/tests/indexes/datetimelike.py +++ b/pandas/tests/indexes/datetimelike.py @@ -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) diff --git a/pandas/tests/indexes/datetimes/test_ops.py b/pandas/tests/indexes/datetimes/test_ops.py index 555da072a890b7..34a43dcc0eabf0 100644 --- a/pandas/tests/indexes/datetimes/test_ops.py +++ b/pandas/tests/indexes/datetimes/test_ops.py @@ -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'),