From 11c1f07469fa1201403960ff1a74162a88001b07 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 4 Jan 2019 16:57:52 +0100 Subject: [PATCH 01/14] DEPR: Series.get_values --- pandas/_libs/lib.pyx | 2 +- pandas/core/series.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 6e6d35f00725c..ddf771db4fc3e 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -79,7 +79,7 @@ def values_from_object(obj: object): """ return my values or the object if we are say an ndarray """ func: object - func = getattr(obj, 'get_values', None) + func = getattr(obj, '_internal_get_values', None) if func is not None: obj = func() diff --git a/pandas/core/series.py b/pandas/core/series.py index de34227cda28a..e4f17c4553a51 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -489,10 +489,17 @@ def _formatting_values(self): """ return self._data.formatting_values() + def _internal_get_values(self): + """ + DEPRECATED: Same as values (but handles sparseness conversions); is a view. + """ + return self._data.get_values() + def get_values(self): """ - Same as values (but handles sparseness conversions); is a view. + DEPRECATED: Same as values (but handles sparseness conversions); is a view. """ + warnings.warn("deprecated", FutureWarning, stacklevel=2) return self._data.get_values() @property From 61c32754e54c3360fbdcda04bb52a893d8dc35ab Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 15 May 2019 12:11:43 +0200 Subject: [PATCH 02/14] experiment --- pandas/_libs/lib.pyx | 3 +++ pandas/core/arrays/categorical.py | 2 ++ pandas/core/arrays/sparse.py | 9 +++++++-- pandas/core/groupby/generic.py | 6 +++--- pandas/core/indexes/base.py | 2 ++ pandas/core/indexes/category.py | 2 ++ pandas/core/ops.py | 2 +- pandas/core/series.py | 3 ++- pandas/core/sparse/frame.py | 2 +- pandas/core/sparse/series.py | 2 +- pandas/util/testing.py | 6 +++--- 11 files changed, 27 insertions(+), 12 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 8a72b8ce5ecc3..990ac7c96a73e 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -76,6 +76,9 @@ def values_from_object(obj: object): """ return my values or the object if we are say an ndarray """ func: object + if getattr(obj, '_typ', '') == 'dataframe': + return obj.values + func = getattr(obj, '_internal_get_values', None) if func is not None: obj = func() diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 6eff6d9ae1091..dd416a1037311 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1507,6 +1507,8 @@ def get_values(self): fill_value=np.nan) return np.array(self) + _internal_get_values = get_values + def check_for_ordered(self, op): """ assert that we are ordered """ if not self.ordered: diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index 66ccd6848864d..de05643789f16 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -454,7 +454,7 @@ def _sparse_array_op( if left.sp_index.ngaps == 0 or right.sp_index.ngaps == 0: with np.errstate(all='ignore'): - result = op(left.get_values(), right.get_values()) + result = op(left.to_dense(), right.to_dense()) fill = op(_get_fill(left), _get_fill(right)) if left.sp_index.ngaps == 0: @@ -1457,7 +1457,12 @@ def to_dense(self): return np.asarray(self, dtype=self.sp_values.dtype) # TODO: Look into deprecating this in favor of `to_dense`. - get_values = to_dense + # get_values = to_dense + + def get_values(self): + raise Exception("USING GET_VALUES") + + _internal_get_values = to_dense # ------------------------------------------------------------------------ # IO diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index b5b6553d2ae69..3bd9424e8c75a 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -1006,7 +1006,7 @@ def nunique(self, dropna=True): """ ids, _, _ = self.grouper.group_info - val = self.obj.get_values() + val = self.obj._internal_get_values() try: sorter = np.lexsort((val, ids)) @@ -1080,7 +1080,7 @@ def value_counts(self, normalize=False, sort=True, ascending=False, bins=bins) ids, _, _ = self.grouper.group_info - val = self.obj.get_values() + val = self.obj._internal_get_values() # groupby removes null keys from groupings mask = ids != -1 @@ -1194,7 +1194,7 @@ def count(self): Count of values within each group. """ ids, _, ngroups = self.grouper.group_info - val = self.obj.get_values() + val = self.obj._internal_get_values() mask = (ids != -1) & ~isna(val) ids = ensure_platform_int(ids) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index bb12c2bc43a38..98697681298a4 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3717,6 +3717,8 @@ def get_values(self): """ return self.values + _internal_get_values = get_values + @Appender(IndexOpsMixin.memory_usage.__doc__) def memory_usage(self, deep=False): result = super().memory_usage(deep=deep) diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index acd36b2c9148a..1cede9d138ace 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -357,6 +357,8 @@ def get_values(self): """ return the underlying data as an ndarray """ return self._data.get_values() + _internal_get_values = get_values + def tolist(self): return self._data.tolist() diff --git a/pandas/core/ops.py b/pandas/core/ops.py index ae9ce5ea669e1..5b040b31abb11 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -1894,7 +1894,7 @@ def wrapper(self, other, axis=None): name=res_name, dtype='bool') else: - values = self.get_values() + values = self.to_numpy() with np.errstate(all='ignore'): res = na_op(values, other) diff --git a/pandas/core/series.py b/pandas/core/series.py index 3929045b42382..1581474b9e5eb 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -505,7 +505,8 @@ def get_values(self): numpy.ndarray Data of the Series. """ - warnings.warn("deprecated", FutureWarning, stacklevel=2) + raise Exception("USING GET_VALUES") + #warnings.warn("deprecated", FutureWarning, stacklevel=2) return self._data.get_values() @property diff --git a/pandas/core/sparse/frame.py b/pandas/core/sparse/frame.py index 79b3a622ad72e..8cfd8b7220541 100644 --- a/pandas/core/sparse/frame.py +++ b/pandas/core/sparse/frame.py @@ -683,7 +683,7 @@ def _reindex_with_indexers(self, reindexers, method=None, fill_value=None, if col not in self: continue if row_indexer is not None: - new_arrays[col] = algos.take_1d(self[col].get_values(), + new_arrays[col] = algos.take_1d(self[col]._internal_get_values(), row_indexer, fill_value=fill_value) else: diff --git a/pandas/core/sparse/series.py b/pandas/core/sparse/series.py index fc14b20f0c6fe..0f36e54a634b1 100644 --- a/pandas/core/sparse/series.py +++ b/pandas/core/sparse/series.py @@ -227,7 +227,7 @@ def __unicode__(self): def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None, filter_type=None, **kwds): """ perform a reduction operation """ - return op(self.get_values(), skipna=skipna, **kwds) + return op(self.array.to_dense(), skipna=skipna, **kwds) def __getstate__(self): # pickling diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 9084ebc736599..64a9e6d5886dd 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -1043,7 +1043,7 @@ def assert_series_equal(left, right, check_dtype=True, assert_attr_equal('dtype', left, right) if check_exact: - assert_numpy_array_equal(left.get_values(), right.get_values(), + assert_numpy_array_equal(left._internal_get_values(), right._internal_get_values(), check_dtype=check_dtype, obj='{obj}'.format(obj=obj),) elif check_datetimelike_compat: @@ -1062,7 +1062,7 @@ def assert_series_equal(left, right, check_dtype=True, '{right}.').format(left=left.values, right=right.values) raise AssertionError(msg) else: - assert_numpy_array_equal(left.get_values(), right.get_values(), + assert_numpy_array_equal(left._internal_get_values(), right._internal_get_values(), check_dtype=check_dtype) elif is_interval_dtype(left) or is_interval_dtype(right): assert_interval_array_equal(left.array, right.array) @@ -1077,7 +1077,7 @@ def assert_series_equal(left, right, check_dtype=True, is_extension_array_dtype(right) and not is_categorical_dtype(right)): assert_extension_array_equal(left.array, right.array) else: - _testing.assert_almost_equal(left.get_values(), right.get_values(), + _testing.assert_almost_equal(left._internal_get_values(), right._internal_get_values(), check_less_precise=check_less_precise, check_dtype=check_dtype, obj='{obj}'.format(obj=obj)) From f96b352b3953bf1252150fcfd67dc983f8c381e6 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 15 May 2019 13:13:52 +0200 Subject: [PATCH 03/14] more experiment --- pandas/core/arrays/categorical.py | 8 +++++--- pandas/core/indexes/base.py | 5 +++-- pandas/core/indexes/category.py | 4 +++- pandas/core/indexes/multi.py | 2 +- pandas/core/internals/blocks.py | 2 +- pandas/io/formats/format.py | 4 ++-- pandas/tests/arrays/categorical/test_api.py | 12 ++++++------ pandas/tests/indexes/multi/test_analytics.py | 2 +- pandas/tests/indexes/period/test_period.py | 6 +++--- 9 files changed, 25 insertions(+), 20 deletions(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index dd416a1037311..38701b1e02a54 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1499,6 +1499,10 @@ def get_values(self): A numpy array of the same dtype as categorical.categories.dtype or Index if datetime / periods. """ + raise Exception("USING GET_VALUES") + + def _internal_get_values(self): + # if we are a datetime and period index, return Index to keep metadata if is_datetimelike(self.categories): return self.categories.take(self._codes, fill_value=np.nan) @@ -1507,8 +1511,6 @@ def get_values(self): fill_value=np.nan) return np.array(self) - _internal_get_values = get_values - def check_for_ordered(self, op): """ assert that we are ordered """ if not self.ordered: @@ -1933,7 +1935,7 @@ def __iter__(self): """ Returns an Iterator over the values of this Categorical. """ - return iter(self.get_values().tolist()) + return iter(self._internal_get_values().tolist()) def __contains__(self, key): """ diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 98697681298a4..eb8a2b0fa5934 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3715,9 +3715,10 @@ def get_values(self): >>> midx.get_values().ndim 1 """ - return self.values + raise Exception("USING GET_VALUES") - _internal_get_values = get_values + def _internal_get_values(self): + return self.values @Appender(IndexOpsMixin.memory_usage.__doc__) def memory_usage(self, deep=False): diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 1cede9d138ace..8c0a9d8505865 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -355,9 +355,11 @@ def _wrap_setop_result(self, other, result): def get_values(self): """ return the underlying data as an ndarray """ + raise Exception("USING GET_VALUES") return self._data.get_values() - _internal_get_values = get_values + def _internal_get_values(self): + return self._data._internal_get_values() def tolist(self): return self._data.tolist() diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 7a933bdcb0953..4ba3c2b390f82 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1172,7 +1172,7 @@ def values(self): for i in range(self.nlevels): vals = self._get_level_values(i) if is_categorical_dtype(vals): - vals = vals.get_values() + vals = vals._internal_get_values() if (isinstance(vals.dtype, ExtensionDtype) or hasattr(vals, '_box_values')): vals = vals.astype(object) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 0c49ebb55acdd..e4300fa1cd321 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -2916,7 +2916,7 @@ def to_dense(self): # Categorical.get_values returns a DatetimeIndex for datetime # categories, so we can't simply use `np.asarray(self.values)` like # other types. - return self.values.get_values() + return self.values._internal_get_values() def to_native_types(self, slicer=None, na_rep='', quoting=None, **kwargs): """ convert to our native types format, slicing if desired """ diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 1c1acebb8f8f7..8c28a37d11cf1 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -129,7 +129,7 @@ def _get_footer(self): return str(footer) def _get_formatted_values(self): - return format_array(self.categorical.get_values(), None, + return format_array(self.categorical._internal_get_values(), None, float_format=None, na_rep=self.na_rep) def to_string(self): @@ -1191,7 +1191,7 @@ def _format_strings(self): if is_categorical_dtype(values.dtype): # Categorical is special for now, so that we can preserve tzinfo - array = values.get_values() + array = values._internal_get_values() else: array = np.asarray(values) diff --git a/pandas/tests/arrays/categorical/test_api.py b/pandas/tests/arrays/categorical/test_api.py index 15e4bbab8f649..f10e1a8dca7f1 100644 --- a/pandas/tests/arrays/categorical/test_api.py +++ b/pandas/tests/arrays/categorical/test_api.py @@ -248,7 +248,7 @@ def test_set_categories(self): tm.assert_index_equal(c.categories, Index([1, 2, 3, 4])) exp = np.array([1, 2, 3, 4, 1], dtype=np.int64) - tm.assert_numpy_array_equal(c.get_values(), exp) + tm.assert_numpy_array_equal(c.to_dense(), exp) # all "pointers" to '4' must be changed from 3 to 0,... c = c.set_categories([4, 3, 2, 1]) @@ -262,7 +262,7 @@ def test_set_categories(self): # output is the same exp = np.array([1, 2, 3, 4, 1], dtype=np.int64) - tm.assert_numpy_array_equal(c.get_values(), exp) + tm.assert_numpy_array_equal(c.to_dense(), exp) assert c.min() == 4 assert c.max() == 1 @@ -270,13 +270,13 @@ def test_set_categories(self): c2 = c.set_categories([4, 3, 2, 1], ordered=False) assert not c2.ordered - tm.assert_numpy_array_equal(c.get_values(), c2.get_values()) + tm.assert_numpy_array_equal(c.to_dense(), c2.to_dense()) # set_categories should pass thru the ordering c2 = c.set_ordered(False).set_categories([4, 3, 2, 1]) assert not c2.ordered - tm.assert_numpy_array_equal(c.get_values(), c2.get_values()) + tm.assert_numpy_array_equal(c.to_dense(), c2.to_dense()) @pytest.mark.parametrize('values, categories, new_categories', [ # No NaNs, same cats, same order @@ -383,7 +383,7 @@ def test_remove_unused_categories(self): tm.assert_index_equal(out.categories, Index(['B', 'D', 'F'])) exp_codes = np.array([2, -1, 1, 0, 1, 2, -1], dtype=np.int8) tm.assert_numpy_array_equal(out.codes, exp_codes) - assert out.get_values().tolist() == val + assert out.to_dense().tolist() == val alpha = list('abcdefghijklmnopqrstuvwxyz') val = np.random.choice(alpha[::2], 10000).astype('object') @@ -391,7 +391,7 @@ def test_remove_unused_categories(self): cat = Categorical(values=val, categories=alpha) out = cat.remove_unused_categories() - assert out.get_values().tolist() == val.tolist() + assert out.to_dense().tolist() == val.tolist() class TestCategoricalAPIWithFactor(TestCategorical): diff --git a/pandas/tests/indexes/multi/test_analytics.py b/pandas/tests/indexes/multi/test_analytics.py index f89e082c7b06f..c1aa5f76fe5d6 100644 --- a/pandas/tests/indexes/multi/test_analytics.py +++ b/pandas/tests/indexes/multi/test_analytics.py @@ -21,7 +21,7 @@ def test_shift(idx): def test_groupby(idx): groups = idx.groupby(np.array([1, 1, 1, 2, 2, 2])) - labels = idx.get_values().tolist() + labels = idx.to_numpy().tolist() exp = {1: labels[:3], 2: labels[3:]} tm.assert_dict_equal(groups, exp) diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index 2f3f15101e7ca..02cb2f34f7119 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -162,7 +162,7 @@ def test_values(self): exp = np.array([], dtype=np.object) tm.assert_numpy_array_equal(idx.values, exp) - tm.assert_numpy_array_equal(idx.get_values(), exp) + #tm.assert_numpy_array_equal(idx.get_values(), exp) exp = np.array([], dtype=np.int64) tm.assert_numpy_array_equal(idx._ndarray_values, exp) @@ -170,7 +170,7 @@ def test_values(self): exp = np.array([pd.Period('2011-01', freq='M'), pd.NaT], dtype=object) tm.assert_numpy_array_equal(idx.values, exp) - tm.assert_numpy_array_equal(idx.get_values(), exp) + #tm.assert_numpy_array_equal(idx.get_values(), exp) exp = np.array([492, -9223372036854775808], dtype=np.int64) tm.assert_numpy_array_equal(idx._ndarray_values, exp) @@ -179,7 +179,7 @@ def test_values(self): exp = np.array([pd.Period('2011-01-01', freq='D'), pd.NaT], dtype=object) tm.assert_numpy_array_equal(idx.values, exp) - tm.assert_numpy_array_equal(idx.get_values(), exp) + #tm.assert_numpy_array_equal(idx.get_values(), exp) exp = np.array([14975, -9223372036854775808], dtype=np.int64) tm.assert_numpy_array_equal(idx._ndarray_values, exp) From 4931b9c60f7e1837ea0193a2496d302db5166eb8 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 15 May 2019 13:31:43 +0200 Subject: [PATCH 04/14] more experiment --- pandas/core/algorithms.py | 2 +- pandas/core/dtypes/concat.py | 2 +- pandas/core/frame.py | 4 ++-- pandas/core/internals/blocks.py | 1 + pandas/core/series.py | 8 ++------ pandas/core/sparse/frame.py | 2 +- pandas/tests/frame/test_reshape.py | 2 +- pandas/tests/indexing/multiindex/test_slice.py | 4 ++-- 8 files changed, 11 insertions(+), 14 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 932ac71a23ed0..7e8a7a87074c8 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1582,7 +1582,7 @@ def take_nd(arr, indexer, axis=0, out=None, fill_value=np.nan, mask_info=None, return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill) if is_sparse(arr): - arr = arr.get_values() + arr = arr.to_dense() elif isinstance(arr, (ABCIndexClass, ABCSeries)): arr = arr.values diff --git a/pandas/core/dtypes/concat.py b/pandas/core/dtypes/concat.py index f8488b7a153e3..caa9b46c78a69 100644 --- a/pandas/core/dtypes/concat.py +++ b/pandas/core/dtypes/concat.py @@ -197,7 +197,7 @@ def _concat_categorical(to_concat, axis=0): return union_categoricals(categoricals) # extract the categoricals & coerce to object if needed - to_concat = [x.get_values() if is_categorical_dtype(x.dtype) + to_concat = [x._internal_get_values() if is_categorical_dtype(x.dtype) else np.asarray(x).ravel() if not is_datetime64tz_dtype(x) else np.asarray(x.astype(object)) for x in to_concat] result = _concat_compat(to_concat) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6ec36c62f0be8..4166276cc319c 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1638,7 +1638,7 @@ def to_records(self, index=True, convert_datetime64=None, else: ix_vals = [self.index.values] - arrays = ix_vals + [self[c].get_values() for c in self.columns] + arrays = ix_vals + [self[c]._internal_get_values() for c in self.columns] count = 0 index_names = list(self.index.names) @@ -1654,7 +1654,7 @@ def to_records(self, index=True, convert_datetime64=None, names = [str(name) for name in itertools.chain(index_names, self.columns)] else: - arrays = [self[c].get_values() for c in self.columns] + arrays = [self[c]._internal_get_values() for c in self.columns] names = [str(c) for c in self.columns] index_names = [] diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index e4300fa1cd321..9fbe873ba83da 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -3216,6 +3216,7 @@ def _putmask_preserve(nv, n): dtype, _ = maybe_promote(n.dtype) if is_extension_type(v.dtype) and is_object_dtype(dtype): + # ? v = v.get_values(dtype) else: v = v.astype(dtype) diff --git a/pandas/core/series.py b/pandas/core/series.py index 1581474b9e5eb..55f62af5c455f 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -488,12 +488,6 @@ def _formatting_values(self): """ return self._data.formatting_values() - def _internal_get_values(self): - """ - Same as values (but handles sparseness conversions); is a view. - """ - return self._data.get_values() - def get_values(self): """ Same as values (but handles sparseness conversions); is a view. @@ -507,6 +501,8 @@ def get_values(self): """ raise Exception("USING GET_VALUES") #warnings.warn("deprecated", FutureWarning, stacklevel=2) + + def _internal_get_values(self): return self._data.get_values() @property diff --git a/pandas/core/sparse/frame.py b/pandas/core/sparse/frame.py index 8cfd8b7220541..4d5c2c770ebac 100644 --- a/pandas/core/sparse/frame.py +++ b/pandas/core/sparse/frame.py @@ -482,7 +482,7 @@ def xs(self, key, axis=0, copy=False): return data i = self.index.get_loc(key) - data = self.take([i]).get_values()[0] + data = self.take([i])._internal_get_values()[0] return Series(data, index=self.columns) # ---------------------------------------------------------------------- diff --git a/pandas/tests/frame/test_reshape.py b/pandas/tests/frame/test_reshape.py index a3b9e529431e5..ac8d1557a4c43 100644 --- a/pandas/tests/frame/test_reshape.py +++ b/pandas/tests/frame/test_reshape.py @@ -804,7 +804,7 @@ def _test_stack_with_multiindex(multiindex): else: assert_frame_equal(result, expected) - df.columns = MultiIndex.from_tuples(df.columns.get_values(), + df.columns = MultiIndex.from_tuples(df.columns.to_numpy(), names=df.columns.names) expected = df.stack(level=level, dropna=False) if isinstance(expected, Series): diff --git a/pandas/tests/indexing/multiindex/test_slice.py b/pandas/tests/indexing/multiindex/test_slice.py index 6433a39fe4373..b087a4a82db59 100644 --- a/pandas/tests/indexing/multiindex/test_slice.py +++ b/pandas/tests/indexing/multiindex/test_slice.py @@ -21,7 +21,7 @@ def test_per_axis_per_level_getitem(self): # example test case ix = MultiIndex.from_product([_mklbl('A', 5), _mklbl('B', 7), _mklbl( 'C', 4), _mklbl('D', 2)]) - df = DataFrame(np.arange(len(ix.get_values())), index=ix) + df = DataFrame(np.arange(len(ix.to_numpy())), index=ix) result = df.loc[(slice('A1', 'A3'), slice(None), ['C1', 'C3']), :] expected = df.loc[[tuple([a, b, c, d]) @@ -88,7 +88,7 @@ def test_per_axis_per_level_getitem(self): tm.assert_frame_equal(result, expected) # multi-level series - s = Series(np.arange(len(ix.get_values())), index=ix) + s = Series(np.arange(len(ix.to_numpy())), index=ix) result = s.loc['A1':'A3', :, ['C1', 'C3']] expected = s.loc[[tuple([a, b, c, d]) for a, b, c, d in s.index.values From 10cce4549df2e16d9af2bdb37253da4f5ff086df Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 15 May 2019 14:00:10 +0200 Subject: [PATCH 05/14] skip sparse get_values test --- pandas/tests/arrays/sparse/test_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index e09f4e2ccc59c..c85ff2bb6e83e 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -615,7 +615,7 @@ def test_shape(self, data, shape, dtype): [1, np.nan, np.nan, 3, np.nan], [1, np.nan, 0, 3, 0], ]) - @pytest.mark.parametrize("method", ["to_dense", "get_values"]) + @pytest.mark.parametrize("method", ["to_dense"]) # , "get_values"]) @pytest.mark.parametrize("fill_value", [None, 0]) def test_dense_repr(self, vals, fill_value, method): vals = np.array(vals) From 10f92b1059a2654a1fbe7256f8bddd4505ba504c Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 15 May 2019 17:34:46 +0200 Subject: [PATCH 06/14] fix json --- pandas/_libs/src/ujson/python/objToJSON.c | 18 ++++++++++++------ pandas/core/arrays/categorical.py | 1 - pandas/core/internals/blocks.py | 3 +++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/pandas/_libs/src/ujson/python/objToJSON.c b/pandas/_libs/src/ujson/python/objToJSON.c index 52788f85ff71e..71363eea8566a 100644 --- a/pandas/_libs/src/ujson/python/objToJSON.c +++ b/pandas/_libs/src/ujson/python/objToJSON.c @@ -216,10 +216,6 @@ static PyObject *get_values(PyObject *obj) { if (values && !PyArray_CheckExact(values)) { - if (PyObject_HasAttrString(values, "to_numpy")) { - values = PyObject_CallMethod(values, "to_numpy", NULL); - } - if (PyObject_HasAttrString(values, "values")) { PyObject *subvals = get_values(values); PyErr_Clear(); @@ -249,9 +245,19 @@ static PyObject *get_values(PyObject *obj) { } } - if (!values && PyObject_HasAttrString(obj, "get_values")) { + if (!values && PyObject_HasAttrString(obj, "_internal_get_values")) { + PRINTMARK(); + values = PyObject_CallMethod(obj, "_internal_get_values", NULL); + if (values && !PyArray_CheckExact(values)) { + PRINTMARK(); + Py_DECREF(values); + values = NULL; + } + } + + if (!values && PyObject_HasAttrString(obj, "get_block_values")) { PRINTMARK(); - values = PyObject_CallMethod(obj, "get_values", NULL); + values = PyObject_CallMethod(obj, "get_block_values", NULL); if (values && !PyArray_CheckExact(values)) { PRINTMARK(); Py_DECREF(values); diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 38701b1e02a54..21c0835a0e8ae 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1502,7 +1502,6 @@ def get_values(self): raise Exception("USING GET_VALUES") def _internal_get_values(self): - # if we are a datetime and period index, return Index to keep metadata if is_datetimelike(self.categories): return self.categories.take(self._codes, fill_value=np.nan) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 9fbe873ba83da..51c2009df1166 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -179,6 +179,9 @@ def get_values(self, dtype=None): return self.values.astype(object) return self.values + def get_block_values(self, dtype=None): + return self.get_values(dtype=dtype) + def to_dense(self): return self.values.view() From b32d87b1c2c7da3970ad6f8dd4a7bb99cb40e85c Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 15 May 2019 20:34:21 +0200 Subject: [PATCH 07/14] fix sparse --- pandas/core/generic.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 876465d96e6fe..931f15b31e4f1 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5441,6 +5441,9 @@ def get_values(self): [nan, 2.], [nan, 3.]]) """ + raise Exception("USING GET_VALUES") + + def _internal_get_values(self): return self.values def get_dtype_counts(self): From 19093e30ceca92a286763abf3699d440f0da9160 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 28 Jun 2019 14:09:20 -0500 Subject: [PATCH 08/14] add warnings --- pandas/core/arrays/categorical.py | 4 +++- pandas/core/arrays/sparse.py | 5 ++++- pandas/core/generic.py | 5 ++++- pandas/core/indexes/base.py | 5 ++++- pandas/core/indexes/category.py | 6 ++++-- pandas/core/internals/blocks.py | 3 +-- 6 files changed, 20 insertions(+), 8 deletions(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 21dd0092ae2e1..a65ee664dba2d 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1505,7 +1505,9 @@ def get_values(self): A numpy array of the same dtype as categorical.categories.dtype or Index if datetime / periods. """ - raise Exception("USING GET_VALUES") + warn("The 'get_values' method is deprecated and will be removed in a " + "future version", stacklevel=2) + return self._internal_get_values() def _internal_get_values(self): # if we are a datetime and period index, return Index to keep metadata diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index 8af9d97da6040..59dbf23ca9394 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -1472,7 +1472,10 @@ def to_dense(self): # get_values = to_dense def get_values(self): - raise Exception("USING GET_VALUES") + warnings.warn( + "The 'get_values' method is deprecated and will be removed in a " + "future version", stacklevel=2) + return self._interal_get_values() _internal_get_values = to_dense diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a34f462ca5ffd..3a79a3146434c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5367,7 +5367,10 @@ def get_values(self): [nan, 2.], [nan, 3.]]) """ - raise Exception("USING GET_VALUES") + warnings.warn( + "The 'get_values' method is deprecated and will be removed in a " + "future version", stacklevel=2) + return self._interal_get_values() def _internal_get_values(self): return self.values diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 134ee059371a8..9968f86adcc69 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3810,7 +3810,10 @@ def get_values(self): >>> midx.get_values().ndim 1 """ - raise Exception("USING GET_VALUES") + warnings.warn( + "The 'get_values' method is deprecated and will be removed in a " + "future version", stacklevel=2) + return self._interal_get_values() def _internal_get_values(self): return self.values diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 482b83cb27314..9122983626cf7 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -357,8 +357,10 @@ def _wrap_setop_result(self, other, result): def get_values(self): """ return the underlying data as an ndarray """ - raise Exception("USING GET_VALUES") - return self._data.get_values() + warnings.warn( + "The 'get_values' method is deprecated and will be removed in a " + "future version", stacklevel=2) + return self._interal_get_values() def _internal_get_values(self): return self._data._internal_get_values() diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 3e0ce2a9c314f..0f7ea7b112f2b 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -3216,8 +3216,7 @@ def _putmask_preserve(nv, n): dtype, _ = maybe_promote(n.dtype) if is_extension_type(v.dtype) and is_object_dtype(dtype): - # ? - v = v.get_values(dtype) + v = v._internal_get_values(dtype) else: v = v.astype(dtype) From ec370afbc40dd8a9d24f05fbb64b57c7c6ed38c6 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 28 Jun 2019 14:29:11 -0500 Subject: [PATCH 09/14] add deprecation notes --- pandas/core/arrays/categorical.py | 2 ++ pandas/core/arrays/sparse.py | 9 ++++++++- pandas/core/generic.py | 2 ++ pandas/core/indexes/base.py | 2 ++ pandas/core/indexes/category.py | 7 ++++++- pandas/core/series.py | 6 ++++-- 6 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index a65ee664dba2d..673afd5f49d8c 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1497,6 +1497,8 @@ def get_values(self): """ Return the values. + .. deprecated:: 0.25.0 + For internal compatibility with pandas formatting. Returns diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index 59dbf23ca9394..c929193b504df 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -1472,9 +1472,16 @@ def to_dense(self): # get_values = to_dense def get_values(self): + """ + Convert SparseArray to a NumPy array. + + .. deprecated:: 0.25.0 + Use `to_dense` instead. + + """ warnings.warn( "The 'get_values' method is deprecated and will be removed in a " - "future version", stacklevel=2) + "future version. Use the 'to_dense' method instead.", stacklevel=2) return self._interal_get_values() _internal_get_values = to_dense diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3a79a3146434c..568d56447e344 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5328,6 +5328,8 @@ def get_values(self): """ Return an ndarray after converting sparse values to dense. + .. deprecated:: 0.25.0 + This is the same as ``.values`` for non-sparse data. For sparse data contained in a `SparseArray`, the data are first converted to a dense representation. diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 9968f86adcc69..a0a31e2ed1d3d 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3772,6 +3772,8 @@ def get_values(self): """ Return `Index` data as an `numpy.ndarray`. + .. deprecated:: 0.25.0 + Returns ------- numpy.ndarray diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 9122983626cf7..8ec1e5b08c25c 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -356,7 +356,12 @@ def _wrap_setop_result(self, other, result): return self._shallow_copy(result, name=name) def get_values(self): - """ return the underlying data as an ndarray """ + """ + Return the underlying data as an ndarray + + .. deprecated:: 0.25.0 + + """ warnings.warn( "The 'get_values' method is deprecated and will be removed in a " "future version", stacklevel=2) diff --git a/pandas/core/series.py b/pandas/core/series.py index 88e4f9d058779..4bb8c1ecc92d9 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -513,8 +513,10 @@ def get_values(self): numpy.ndarray Data of the Series. """ - raise Exception("USING GET_VALUES") - #warnings.warn("deprecated", FutureWarning, stacklevel=2) + warnings.warn( + "The 'get_values' method is deprecated and will be removed in a " + "future version", stacklevel=2) + return self._interal_get_values() def _internal_get_values(self): return self._data.get_values() From 1a7f6444ac6ab368400f151475589258fc50aebd Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 28 Jun 2019 14:49:23 -0500 Subject: [PATCH 10/14] fix typo --- pandas/core/indexes/base.py | 2 +- pandas/core/indexes/category.py | 2 +- pandas/core/internals/blocks.py | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index a0a31e2ed1d3d..5f92715c289a3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3815,7 +3815,7 @@ def get_values(self): warnings.warn( "The 'get_values' method is deprecated and will be removed in a " "future version", stacklevel=2) - return self._interal_get_values() + return self._internal_get_values() def _internal_get_values(self): return self.values diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 8ec1e5b08c25c..a8b8b5548fa78 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -365,7 +365,7 @@ def get_values(self): warnings.warn( "The 'get_values' method is deprecated and will be removed in a " "future version", stacklevel=2) - return self._interal_get_values() + return self._internal_get_values() def _internal_get_values(self): return self._data._internal_get_values() diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 0f7ea7b112f2b..d8278167f94ab 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -178,6 +178,9 @@ def get_values(self, dtype=None): return self.values def get_block_values(self, dtype=None): + """ + This is used in the JSON C code + """ return self.get_values(dtype=dtype) def to_dense(self): From d6a9542c4b8b5768d621692f31d034ee17e280e6 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 28 Jun 2019 14:53:23 -0500 Subject: [PATCH 11/14] clean: remove CategoricalIndex.get_values (inherit from base) --- pandas/core/indexes/category.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index a8b8b5548fa78..db4778f5e375f 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -355,19 +355,9 @@ def _wrap_setop_result(self, other, result): name = get_op_result_name(self, other) return self._shallow_copy(result, name=name) - def get_values(self): - """ - Return the underlying data as an ndarray - - .. deprecated:: 0.25.0 - - """ - warnings.warn( - "The 'get_values' method is deprecated and will be removed in a " - "future version", stacklevel=2) - return self._internal_get_values() - def _internal_get_values(self): + # override base Index version to get the numpy array representation of + # the underlying Categorical return self._data._internal_get_values() def tolist(self): From ae22440a240e956b965e31f3c591df619a4ed776 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 28 Jun 2019 17:03:18 -0500 Subject: [PATCH 12/14] fix style issues --- pandas/core/arrays/sparse.py | 7 ++----- pandas/core/frame.py | 3 ++- pandas/core/series.py | 2 +- pandas/core/sparse/frame.py | 7 ++++--- pandas/util/testing.py | 10 ++++++---- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index c929193b504df..f2e4bcab8beb2 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -1468,13 +1468,10 @@ def to_dense(self): """ return np.asarray(self, dtype=self.sp_values.dtype) - # TODO: Look into deprecating this in favor of `to_dense`. - # get_values = to_dense - def get_values(self): """ Convert SparseArray to a NumPy array. - + .. deprecated:: 0.25.0 Use `to_dense` instead. @@ -1482,7 +1479,7 @@ def get_values(self): warnings.warn( "The 'get_values' method is deprecated and will be removed in a " "future version. Use the 'to_dense' method instead.", stacklevel=2) - return self._interal_get_values() + return self._internal_get_values() _internal_get_values = to_dense diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c974bd60d385a..fc10912172a96 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1616,7 +1616,8 @@ def to_records(self, index=True, convert_datetime64=None, else: ix_vals = [self.index.values] - arrays = ix_vals + [self[c]._internal_get_values() for c in self.columns] + arrays = ix_vals + [self[c]._internal_get_values() + for c in self.columns] count = 0 index_names = list(self.index.names) diff --git a/pandas/core/series.py b/pandas/core/series.py index 4bb8c1ecc92d9..3e9d76cbcf6c6 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -516,7 +516,7 @@ def get_values(self): warnings.warn( "The 'get_values' method is deprecated and will be removed in a " "future version", stacklevel=2) - return self._interal_get_values() + return self._internal_get_values() def _internal_get_values(self): return self._data.get_values() diff --git a/pandas/core/sparse/frame.py b/pandas/core/sparse/frame.py index a2210739ae2f6..6a0ba5f93c509 100644 --- a/pandas/core/sparse/frame.py +++ b/pandas/core/sparse/frame.py @@ -694,9 +694,10 @@ def _reindex_with_indexers(self, reindexers, method=None, fill_value=None, if col not in self: continue if row_indexer is not None: - new_arrays[col] = algos.take_1d(self[col]._internal_get_values(), - row_indexer, - fill_value=fill_value) + new_arrays[col] = algos.take_1d( + self[col]._internal_get_values(), + row_indexer, + fill_value=fill_value) else: new_arrays[col] = self[col] diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 449918dc6c604..c7cba85e979a3 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -1052,7 +1052,8 @@ def assert_series_equal(left, right, check_dtype=True, assert_attr_equal('dtype', left, right) if check_exact: - assert_numpy_array_equal(left._internal_get_values(), right._internal_get_values(), + assert_numpy_array_equal(left._internal_get_values(), + right._internal_get_values(), check_dtype=check_dtype, obj='{obj}'.format(obj=obj),) elif check_datetimelike_compat: @@ -1071,11 +1072,11 @@ def assert_series_equal(left, right, check_dtype=True, '{right}.').format(left=left.values, right=right.values) raise AssertionError(msg) else: - assert_numpy_array_equal(left._internal_get_values(), right._internal_get_values(), + assert_numpy_array_equal(left._internal_get_values(), + right._internal_get_values(), check_dtype=check_dtype) elif is_interval_dtype(left) or is_interval_dtype(right): assert_interval_array_equal(left.array, right.array) - elif (is_extension_array_dtype(left.dtype) and is_datetime64tz_dtype(left.dtype)): # .values is an ndarray, but ._values is the ExtensionArray. @@ -1086,7 +1087,8 @@ def assert_series_equal(left, right, check_dtype=True, is_extension_array_dtype(right) and not is_categorical_dtype(right)): assert_extension_array_equal(left.array, right.array) else: - _testing.assert_almost_equal(left._internal_get_values(), right._internal_get_values(), + _testing.assert_almost_equal(left._internal_get_values(), + right._internal_get_values(), check_less_precise=check_less_precise, check_dtype=check_dtype, obj='{obj}'.format(obj=obj)) From 1a32d54c6dc3e7f6c282726b5e3dcec65b0d3fd0 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sat, 29 Jun 2019 09:58:53 -0500 Subject: [PATCH 13/14] tests --- pandas/core/arrays/categorical.py | 2 +- pandas/core/arrays/sparse.py | 3 ++- pandas/core/generic.py | 4 ++-- pandas/core/indexes/base.py | 2 +- pandas/core/series.py | 2 +- pandas/tests/arrays/categorical/test_api.py | 10 ++++++++-- pandas/tests/arrays/sparse/test_array.py | 11 +++++++---- pandas/tests/frame/test_api.py | 6 ++++++ pandas/tests/indexes/multi/test_analytics.py | 2 +- pandas/tests/indexes/period/test_period.py | 8 +++++--- pandas/tests/series/test_api.py | 6 ++++++ 11 files changed, 40 insertions(+), 16 deletions(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 673afd5f49d8c..7d5c2a172607f 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1508,7 +1508,7 @@ def get_values(self): Index if datetime / periods. """ warn("The 'get_values' method is deprecated and will be removed in a " - "future version", stacklevel=2) + "future version", FutureWarning, stacklevel=2) return self._internal_get_values() def _internal_get_values(self): diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index f2e4bcab8beb2..97ab6ec8235ef 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -1478,7 +1478,8 @@ def get_values(self): """ warnings.warn( "The 'get_values' method is deprecated and will be removed in a " - "future version. Use the 'to_dense' method instead.", stacklevel=2) + "future version. Use the 'to_dense' method instead.", + FutureWarning, stacklevel=2) return self._internal_get_values() _internal_get_values = to_dense diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 568d56447e344..874663235fa1e 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5371,8 +5371,8 @@ def get_values(self): """ warnings.warn( "The 'get_values' method is deprecated and will be removed in a " - "future version", stacklevel=2) - return self._interal_get_values() + "future version", FutureWarning, stacklevel=2) + return self._internal_get_values() def _internal_get_values(self): return self.values diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 5f92715c289a3..0d78da712cbf5 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3814,7 +3814,7 @@ def get_values(self): """ warnings.warn( "The 'get_values' method is deprecated and will be removed in a " - "future version", stacklevel=2) + "future version", FutureWarning, stacklevel=2) return self._internal_get_values() def _internal_get_values(self): diff --git a/pandas/core/series.py b/pandas/core/series.py index 3e9d76cbcf6c6..35b83ef5a0f79 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -515,7 +515,7 @@ def get_values(self): """ warnings.warn( "The 'get_values' method is deprecated and will be removed in a " - "future version", stacklevel=2) + "future version", FutureWarning, stacklevel=2) return self._internal_get_values() def _internal_get_values(self): diff --git a/pandas/tests/arrays/categorical/test_api.py b/pandas/tests/arrays/categorical/test_api.py index f10e1a8dca7f1..757cc979535a5 100644 --- a/pandas/tests/arrays/categorical/test_api.py +++ b/pandas/tests/arrays/categorical/test_api.py @@ -383,7 +383,7 @@ def test_remove_unused_categories(self): tm.assert_index_equal(out.categories, Index(['B', 'D', 'F'])) exp_codes = np.array([2, -1, 1, 0, 1, 2, -1], dtype=np.int8) tm.assert_numpy_array_equal(out.codes, exp_codes) - assert out.to_dense().tolist() == val + assert out.tolist() == val alpha = list('abcdefghijklmnopqrstuvwxyz') val = np.random.choice(alpha[::2], 10000).astype('object') @@ -391,7 +391,7 @@ def test_remove_unused_categories(self): cat = Categorical(values=val, categories=alpha) out = cat.remove_unused_categories() - assert out.to_dense().tolist() == val.tolist() + assert out.tolist() == val.tolist() class TestCategoricalAPIWithFactor(TestCategorical): @@ -504,3 +504,9 @@ def test_recode_to_categories_large(self): new = Index(expected) result = _recode_for_categories(codes, old, new) tm.assert_numpy_array_equal(result, expected) + + def test_deprecated_get_values(self): + cat = Categorical(["a", "b", "c", "a"]) + with tm.assert_produces_warning(FutureWarning): + res = cat.get_values() + tm.assert_numpy_array_equal(res, np.array(cat)) diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index 87c267098956a..8a51704732d7f 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -615,16 +615,19 @@ def test_shape(self, data, shape, dtype): [1, np.nan, np.nan, 3, np.nan], [1, np.nan, 0, 3, 0], ]) - @pytest.mark.parametrize("method", ["to_dense"]) # , "get_values"]) @pytest.mark.parametrize("fill_value", [None, 0]) - def test_dense_repr(self, vals, fill_value, method): + def test_dense_repr(self, vals, fill_value): vals = np.array(vals) arr = SparseArray(vals, fill_value=fill_value) - dense_func = getattr(arr, method) - res = dense_func() + res = arr.to_dense() tm.assert_numpy_array_equal(res, vals) + with tm.assert_produces_warning(FutureWarning): + res2 = arr.get_values() + + tm.assert_numpy_array_equal(res2, vals) + def test_getitem(self): def _checkit(i): assert_almost_equal(self.arr[i], self.arr.to_dense()[i]) diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index ce841b302a037..ed224e23fbe20 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -547,3 +547,9 @@ def test_tab_complete_warning(self, ip): with tm.assert_produces_warning(None): with provisionalcompleter('ignore'): list(ip.Completer.completions('df.', 1)) + + def test_get_values_deprecated(self): + df = DataFrame({'a': [1, 2], 'b': [.1, .2]}) + with tm.assert_produces_warning(FutureWarning): + res = df.get_values() + tm.assert_numpy_array_equal(res, df.values) diff --git a/pandas/tests/indexes/multi/test_analytics.py b/pandas/tests/indexes/multi/test_analytics.py index 715ad7377b83b..f886d78da6da2 100644 --- a/pandas/tests/indexes/multi/test_analytics.py +++ b/pandas/tests/indexes/multi/test_analytics.py @@ -20,7 +20,7 @@ def test_shift(idx): def test_groupby(idx): groups = idx.groupby(np.array([1, 1, 1, 2, 2, 2])) - labels = idx.to_numpy().tolist() + labels = idx.tolist() exp = {1: labels[:3], 2: labels[3:]} tm.assert_dict_equal(groups, exp) diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index 02cb2f34f7119..34dab750306e8 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -162,7 +162,9 @@ def test_values(self): exp = np.array([], dtype=np.object) tm.assert_numpy_array_equal(idx.values, exp) - #tm.assert_numpy_array_equal(idx.get_values(), exp) + tm.assert_numpy_array_equal(idx.to_numpy(), exp) + with tm.assert_produces_warning(FutureWarning): + tm.assert_numpy_array_equal(idx.get_values(), exp) exp = np.array([], dtype=np.int64) tm.assert_numpy_array_equal(idx._ndarray_values, exp) @@ -170,7 +172,7 @@ def test_values(self): exp = np.array([pd.Period('2011-01', freq='M'), pd.NaT], dtype=object) tm.assert_numpy_array_equal(idx.values, exp) - #tm.assert_numpy_array_equal(idx.get_values(), exp) + tm.assert_numpy_array_equal(idx.to_numpy(), exp) exp = np.array([492, -9223372036854775808], dtype=np.int64) tm.assert_numpy_array_equal(idx._ndarray_values, exp) @@ -179,7 +181,7 @@ def test_values(self): exp = np.array([pd.Period('2011-01-01', freq='D'), pd.NaT], dtype=object) tm.assert_numpy_array_equal(idx.values, exp) - #tm.assert_numpy_array_equal(idx.get_values(), exp) + tm.assert_numpy_array_equal(idx.to_numpy(), exp) exp = np.array([14975, -9223372036854775808], dtype=np.int64) tm.assert_numpy_array_equal(idx._ndarray_values, exp) diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index fac796fbf325a..06eaea5ede235 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -501,6 +501,12 @@ def test_integer_series_size(self): s = Series(range(9), dtype="Int64") assert s.size == 9 + def test_get_values_deprecation(self): + s = Series(range(9)) + with tm.assert_produces_warning(FutureWarning): + res = s.get_values() + tm.assert_numpy_array_equal(res, s.values) + class TestCategoricalSeries: From 990c72cbf6dadad8b276bf5abbee9591b1a168c6 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 1 Jul 2019 09:48:51 -0500 Subject: [PATCH 14/14] add whatsnew note --- doc/source/whatsnew/v0.25.0.rst | 3 +++ pandas/core/generic.py | 4 +++- pandas/core/indexes/base.py | 4 +++- pandas/core/series.py | 4 +++- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 1fd0257d93f45..3b30e5a1e06bc 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -602,6 +602,9 @@ Other deprecations Use the public attributes :attr:`~RangeIndex.start`, :attr:`~RangeIndex.stop` and :attr:`~RangeIndex.step` instead (:issue:`26581`). - The :meth:`Series.ftype`, :meth:`Series.ftypes` and :meth:`DataFrame.ftypes` methods are deprecated and will be removed in a future version. Instead, use :meth:`Series.dtype` and :meth:`DataFrame.dtypes` (:issue:`26705`). +- The :meth:`Series.get_values`, :meth:`DataFrame.get_values`, :meth:`Index.get_values`, + :meth:`SparseArray.get_values` and :meth:`Categorical.get_values` methods are deprecated. + One of ``np.asarray(..)`` or :meth:`~Series.to_numpy` can be used instead (:issue:`19617`). - :meth:`Timedelta.resolution` is deprecated and replaced with :meth:`Timedelta.resolution_string`. In a future version, :meth:`Timedelta.resolution` will be changed to behave like the standard library :attr:`timedelta.resolution` (:issue:`21344`) .. _whatsnew_0250.prior_deprecations: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 874663235fa1e..73b191409c0c1 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5329,6 +5329,7 @@ def get_values(self): Return an ndarray after converting sparse values to dense. .. deprecated:: 0.25.0 + Use ``np.asarray(..)`` or :meth:`DataFrame.values` instead. This is the same as ``.values`` for non-sparse data. For sparse data contained in a `SparseArray`, the data are first @@ -5371,7 +5372,8 @@ def get_values(self): """ warnings.warn( "The 'get_values' method is deprecated and will be removed in a " - "future version", FutureWarning, stacklevel=2) + "future version. Use '.values' or 'np.asarray(..)' instead.", + FutureWarning, stacklevel=2) return self._internal_get_values() def _internal_get_values(self): diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 0d78da712cbf5..9e065e2d81187 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3773,6 +3773,7 @@ def get_values(self): Return `Index` data as an `numpy.ndarray`. .. deprecated:: 0.25.0 + Use :meth:`Index.to_numpy` or :attr:`Index.array` instead. Returns ------- @@ -3814,7 +3815,8 @@ def get_values(self): """ warnings.warn( "The 'get_values' method is deprecated and will be removed in a " - "future version", FutureWarning, stacklevel=2) + "future version. Use '.to_numpy()' or '.array' instead.", + FutureWarning, stacklevel=2) return self._internal_get_values() def _internal_get_values(self): diff --git a/pandas/core/series.py b/pandas/core/series.py index 35b83ef5a0f79..47efc44e736de 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -507,6 +507,7 @@ def get_values(self): Same as values (but handles sparseness conversions); is a view. .. deprecated:: 0.25.0 + Use :meth:`Series.to_numpy` or :attr:`Series.array` instead. Returns ------- @@ -515,7 +516,8 @@ def get_values(self): """ warnings.warn( "The 'get_values' method is deprecated and will be removed in a " - "future version", FutureWarning, stacklevel=2) + "future version. Use '.to_numpy()' or '.array' instead.", + FutureWarning, stacklevel=2) return self._internal_get_values() def _internal_get_values(self):