Skip to content

Commit

Permalink
DEPR: deprecate (Sparse)Series.from_array (pandas-dev#18258)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche authored and jreback committed Nov 17, 2017
1 parent d5c4908 commit 774030c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
-
-

Expand Down
14 changes: 6 additions & 8 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion pandas/core/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 774030c

Please sign in to comment.