Skip to content

DEPR: deprecate (Sparse)Series.from_array #18258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -51,7 +51,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 @@ -2496,8 +2494,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 @@ -4915,8 +4913,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)
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its prob easier just to elminate this and deal with SparseSeries directly in the Series constructor. Do we have an issue for this?

Copy link
Contributor

@jreback jreback Nov 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

annoyingly we'd actually have to do this in __new__ (but not that big of a deal)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be cleaner. But it is also an API change that Series(..) would no longer always return a Series but depending on the input also SparseSeries.
So the question is if we actually want this. I can open an issue about it though.

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 @@ -919,8 +919,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