Skip to content

Commit 774030c

Browse files
jorisvandenbosschejreback
authored andcommitted
DEPR: deprecate (Sparse)Series.from_array (#18258)
1 parent d5c4908 commit 774030c

File tree

6 files changed

+45
-12
lines changed

6 files changed

+45
-12
lines changed

doc/source/whatsnew/v0.22.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Other API Changes
5454
Deprecations
5555
~~~~~~~~~~~~
5656

57-
-
57+
- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`).
5858
-
5959
-
6060

pandas/core/frame.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -2099,10 +2099,8 @@ def _ixs(self, i, axis=0):
20992099

21002100
if index_len and not len(values):
21012101
values = np.array([np.nan] * index_len, dtype=object)
2102-
result = self._constructor_sliced.from_array(values,
2103-
index=self.index,
2104-
name=label,
2105-
fastpath=True)
2102+
result = self._constructor_sliced._from_array(
2103+
values, index=self.index, name=label, fastpath=True)
21062104

21072105
# this is a cached value, mark it so
21082106
result._set_as_cached(label, self)
@@ -2497,8 +2495,8 @@ def _box_item_values(self, key, values):
24972495

24982496
def _box_col_values(self, values, items):
24992497
""" provide boxed values for a column """
2500-
return self._constructor_sliced.from_array(values, index=self.index,
2501-
name=items, fastpath=True)
2498+
return self._constructor_sliced._from_array(values, index=self.index,
2499+
name=items, fastpath=True)
25022500

25032501
def __setitem__(self, key, value):
25042502
key = com._apply_if_callable(key, self)
@@ -4939,8 +4937,8 @@ def _apply_standard(self, func, axis, ignore_failures=False, reduce=True):
49394937
res_index = self.index
49404938
res_columns = self.columns
49414939
values = self.values
4942-
series_gen = (Series.from_array(arr, index=res_columns, name=name,
4943-
dtype=dtype)
4940+
series_gen = (Series._from_array(arr, index=res_columns, name=name,
4941+
dtype=dtype)
49444942
for i, (arr, name) in enumerate(zip(values,
49454943
res_index)))
49464944
else: # pragma : no cover

pandas/core/series.py

+19
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,25 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
273273
@classmethod
274274
def from_array(cls, arr, index=None, name=None, dtype=None, copy=False,
275275
fastpath=False):
276+
"""
277+
DEPRECATED: use the pd.Series(..) constructor instead.
278+
279+
"""
280+
warnings.warn("'from_array' is deprecated and will be removed in a "
281+
"future version. Please use the pd.Series(..) "
282+
"constructor instead.", FutureWarning, stacklevel=2)
283+
return cls._from_array(arr, index=index, name=name, dtype=dtype,
284+
copy=copy, fastpath=fastpath)
285+
286+
@classmethod
287+
def _from_array(cls, arr, index=None, name=None, dtype=None, copy=False,
288+
fastpath=False):
289+
"""
290+
Internal method used in DataFrame.__setitem__/__getitem__.
291+
Difference with Series(..) is that this method checks if a sparse
292+
array is passed.
293+
294+
"""
276295
# return a sparse series here
277296
if isinstance(arr, ABCSparseArray):
278297
from pandas.core.sparse.series import SparseSeries

pandas/core/sparse/series.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,18 @@ def npoints(self):
256256
def from_array(cls, arr, index=None, name=None, copy=False,
257257
fill_value=None, fastpath=False):
258258
"""
259-
Simplified alternate constructor
259+
DEPRECATED: use the pd.SparseSeries(..) constructor instead.
260+
260261
"""
262+
warnings.warn("'from_array' is deprecated and will be removed in a "
263+
"future version. Please use the pd.SparseSeries(..) "
264+
"constructor instead.", FutureWarning, stacklevel=2)
265+
return cls._from_array(arr, index=index, name=name, copy=copy,
266+
fill_value=fill_value, fastpath=fastpath)
267+
268+
@classmethod
269+
def _from_array(cls, arr, index=None, name=None, copy=False,
270+
fill_value=None, fastpath=False):
261271
return cls(arr, index=index, name=name, copy=copy,
262272
fill_value=fill_value, fastpath=fastpath)
263273

pandas/tests/series/test_api.py

+5
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ def test_constructor_dict_timedelta_index(self):
195195
)
196196
self._assert_series_equal(result, expected)
197197

198+
def test_from_array_deprecated(self):
199+
200+
with tm.assert_produces_warning(FutureWarning):
201+
self.series_klass.from_array([1, 2, 3])
202+
198203

199204
class TestSeriesMisc(TestData, SharedWithSparse):
200205

pandas/tests/series/test_timeseries.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -935,8 +935,9 @@ def test_from_M8_structured(self):
935935
assert isinstance(s[0], Timestamp)
936936
assert s[0] == dates[0][0]
937937

938-
s = Series.from_array(arr['Date'], Index([0]))
939-
assert s[0] == dates[0][0]
938+
with pytest.warns(FutureWarning):
939+
s = Series.from_array(arr['Date'], Index([0]))
940+
assert s[0] == dates[0][0]
940941

941942
def test_get_level_values_box(self):
942943
from pandas import MultiIndex

0 commit comments

Comments
 (0)