diff --git a/doc/source/whatsnew/v0.22.0.txt b/doc/source/whatsnew/v0.22.0.txt index 44d8844795d3c..2c0ccd377492a 100644 --- a/doc/source/whatsnew/v0.22.0.txt +++ b/doc/source/whatsnew/v0.22.0.txt @@ -54,7 +54,7 @@ Other API Changes Deprecations ~~~~~~~~~~~~ -- +- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`). - - diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 9e0fd3464ab98..4d8fb9d03db0c 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2099,10 +2099,8 @@ def _ixs(self, i, axis=0): if index_len and not len(values): values = np.array([np.nan] * index_len, dtype=object) - result = self._constructor_sliced.from_array(values, - index=self.index, - name=label, - fastpath=True) + result = self._constructor_sliced._from_array( + values, index=self.index, name=label, fastpath=True) # this is a cached value, mark it so result._set_as_cached(label, self) @@ -2497,8 +2495,8 @@ def _box_item_values(self, key, values): def _box_col_values(self, values, items): """ provide boxed values for a column """ - return self._constructor_sliced.from_array(values, index=self.index, - name=items, fastpath=True) + return self._constructor_sliced._from_array(values, index=self.index, + name=items, fastpath=True) def __setitem__(self, key, value): key = com._apply_if_callable(key, self) @@ -4939,8 +4937,8 @@ def _apply_standard(self, func, axis, ignore_failures=False, reduce=True): res_index = self.index res_columns = self.columns values = self.values - series_gen = (Series.from_array(arr, index=res_columns, name=name, - dtype=dtype) + series_gen = (Series._from_array(arr, index=res_columns, name=name, + dtype=dtype) for i, (arr, name) in enumerate(zip(values, res_index))) else: # pragma : no cover diff --git a/pandas/core/series.py b/pandas/core/series.py index dd86e51ee8154..6142ccdd2f2ac 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -273,6 +273,25 @@ def __init__(self, data=None, index=None, dtype=None, name=None, @classmethod def from_array(cls, arr, index=None, name=None, dtype=None, copy=False, fastpath=False): + """ + DEPRECATED: use the pd.Series(..) constructor instead. + + """ + warnings.warn("'from_array' is deprecated and will be removed in a " + "future version. Please use the pd.Series(..) " + "constructor instead.", FutureWarning, stacklevel=2) + return cls._from_array(arr, index=index, name=name, dtype=dtype, + copy=copy, fastpath=fastpath) + + @classmethod + def _from_array(cls, arr, index=None, name=None, dtype=None, copy=False, + fastpath=False): + """ + Internal method used in DataFrame.__setitem__/__getitem__. + Difference with Series(..) is that this method checks if a sparse + array is passed. + + """ # return a sparse series here if isinstance(arr, ABCSparseArray): from pandas.core.sparse.series import SparseSeries diff --git a/pandas/core/sparse/series.py b/pandas/core/sparse/series.py index 17d0737ba7c63..b5ce3efe9f85d 100644 --- a/pandas/core/sparse/series.py +++ b/pandas/core/sparse/series.py @@ -256,8 +256,18 @@ def npoints(self): def from_array(cls, arr, index=None, name=None, copy=False, fill_value=None, fastpath=False): """ - Simplified alternate constructor + DEPRECATED: use the pd.SparseSeries(..) constructor instead. + """ + warnings.warn("'from_array' is deprecated and will be removed in a " + "future version. Please use the pd.SparseSeries(..) " + "constructor instead.", FutureWarning, stacklevel=2) + return cls._from_array(arr, index=index, name=name, copy=copy, + fill_value=fill_value, fastpath=fastpath) + + @classmethod + def _from_array(cls, arr, index=None, name=None, copy=False, + fill_value=None, fastpath=False): return cls(arr, index=index, name=name, copy=copy, fill_value=fill_value, fastpath=fastpath) diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index c1e4189283928..9aae40e1b8dbb 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -195,6 +195,11 @@ def test_constructor_dict_timedelta_index(self): ) self._assert_series_equal(result, expected) + def test_from_array_deprecated(self): + + with tm.assert_produces_warning(FutureWarning): + self.series_klass.from_array([1, 2, 3]) + class TestSeriesMisc(TestData, SharedWithSparse): diff --git a/pandas/tests/series/test_timeseries.py b/pandas/tests/series/test_timeseries.py index e782293d98ead..c1ef70bba8634 100644 --- a/pandas/tests/series/test_timeseries.py +++ b/pandas/tests/series/test_timeseries.py @@ -935,8 +935,9 @@ def test_from_M8_structured(self): assert isinstance(s[0], Timestamp) assert s[0] == dates[0][0] - s = Series.from_array(arr['Date'], Index([0])) - assert s[0] == dates[0][0] + with pytest.warns(FutureWarning): + s = Series.from_array(arr['Date'], Index([0])) + assert s[0] == dates[0][0] def test_get_level_values_box(self): from pandas import MultiIndex