Skip to content
21 changes: 21 additions & 0 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ def _get_frame_result_type(result, objs):
ABCSparseDataFrame))


def _get_sliced_frame_result_type(data, obj):
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

add a full doc-string

return appropriate class of Series. When data is sparse
it will return a SparseSeries, otherwise it will return
the Series.

Parameters
----------
data : ndarray
Copy link
Contributor

Choose a reason for hiding this comment

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

array-like

obj : DataFrame

Returns
-------
Series or SparseSeries
"""
if is_sparse(data):
from pandas.core.sparse.api import SparseSeries
Copy link
Contributor

Choose a reason for hiding this comment

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

from pandas import

Copy link
Contributor

Choose a reason for hiding this comment

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

from pandas import SparseSeries

return SparseSeries
return obj._constructor_sliced


def _concat_compat(to_concat, axis=0):
"""
provide concatenation of an array of arrays each of which is a single
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
is_iterator,
is_sequence,
is_named_tuple)
from pandas.core.dtypes.concat import _get_sliced_frame_result_type
from pandas.core.dtypes.missing import isna, notna


Expand Down Expand Up @@ -2166,8 +2167,7 @@ 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._box_col_values(values, label)

# this is a cached value, mark it so
result._set_as_cached(label, self)
Expand Down Expand Up @@ -2563,8 +2563,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)
klass = _get_sliced_frame_result_type(values, self)
return klass(values, index=self.index, name=items, fastpath=True)

def __setitem__(self, key, value):
key = com._apply_if_callable(key, self)
Expand Down
18 changes: 2 additions & 16 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,25 +305,11 @@ def from_array(cls, arr, index=None, name=None, dtype=None, copy=False,
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
cls = SparseSeries

return cls(arr, index=index, name=name, dtype=dtype, copy=copy,
fastpath=fastpath)
return cls(arr, index=index, name=name, dtype=dtype,
copy=copy, fastpath=fastpath)

@property
def _constructor(self):
Expand Down
6 changes: 0 additions & 6 deletions pandas/core/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,6 @@ def from_array(cls, arr, index=None, name=None, copy=False,
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