Skip to content

CLN: Remove Series._from_array #19893

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 9 commits into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 11 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2166,8 +2166,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 +2562,16 @@ 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)
# This check here was previously performed in Series._from_array
Copy link
Contributor

Choose a reason for hiding this comment

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

remove all of this commentary

# By doing it here there is no need for that function anymore
# GH#19883.
from pandas.core.dtypes.generic import ABCSparseArray
Copy link
Contributor

Choose a reason for hiding this comment

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

make this a function like _get_series_result_type and put it near _get_frame_result_type in pandas/core/dtypes/concat.py, it should just take self and return the type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see the idea.
Wouldn't it make more sense to be in pandas/core/dtypes/commons.py ? As that file is already imported to frame.py?

Copy link
Contributor

Choose a reason for hiding this comment

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

no, common is for introspection, not conversion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok!

this_constructor_sliced = self._constructor_sliced
if isinstance(values, ABCSparseArray):
from pandas.core.sparse.series import SparseSeries
this_constructor_sliced = SparseSeries
return this_constructor_sliced(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
53 changes: 53 additions & 0 deletions pandas/tests/frame/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,56 @@ def strech(row):
result = df.apply(lambda x: [1, 2, 3], axis=1)
assert not isinstance(result, tm.SubclassedDataFrame)
tm.assert_series_equal(result, expected)

def test_frame_subclassing_and_inherit(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the purpose of these tests? this is another issue, yes?

Copy link
Contributor

Choose a reason for hiding this comment

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

this should be in a new PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wrote it to check that that approach would work now. I'll take it out.

# Subclass frame and series and ensure that data can be transfered
# between them on slicing GH#19883

class CustomSeries(Series):

def __init__(self, *args, **kw):
super(CustomSeries, self).__init__(*args, **kw)
self.extra_data = None

@property
def _constructor(self):
return CustomSeries

def __finalize__(self, other, method=None, **kwargs):
if method == "_inherit":
self.extra_data = other.extra_data
return self

class CustomDataFrame(DataFrame):
"""
Subclasses pandas DF, fills DF with simulation results, adds some
custom temporal data.
"""

def __init__(self, *args, **kw):
super(CustomDataFrame, self).__init__(*args, **kw)
self.extra_data = None

@property
def _constructor(self):
return CustomDataFrame

@property
def _constructor_sliced(self):
def f(*args, **kwargs):
return CustomSeries(*args, **kwargs).__finalize__(
self, method='_inherit')
return f

data = {'col1': range(10),
'col2': range(10)}
cdf = CustomDataFrame(data)
cdf.extra_data = range(3)

# column
cdf_series = cdf.col1
assert cdf_series.extra_data == cdf.extra_data

# row
cdf_series = cdf.iloc[0]
assert cdf_series.extra_data == cdf.extra_data