Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
_get_sliced_frame_result_type
to check type of DataFrame._constructor_sliced
  • Loading branch information
jaumebonet committed Feb 25, 2018
commit 545de1eeee029ae187df4101ac356fa2efec941e
12 changes: 12 additions & 0 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ 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 depending on whether
the data is sparse or not.
"""
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
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

just return, no else needed

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
10 changes: 2 additions & 8 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2562,14 +2562,8 @@ def _box_item_values(self, key, values):

def _box_col_values(self, values, items):
""" provide boxed values for a column """
# This check here was previously performed in Series._from_array
# By doing it here there is no need for that function anymore
# GH#19883.
from pandas.core.dtypes.generic import ABCSparseArray
this_constructor_sliced = self._constructor_sliced
if isinstance(values, ABCSparseArray):
from pandas.core.sparse.series import SparseSeries
this_constructor_sliced = SparseSeries
from pandas.core.dtypes.concat import _get_sliced_frame_result_type
Copy link
Contributor

Choose a reason for hiding this comment

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

this can be imported at the top

this_constructor_sliced = _get_sliced_frame_result_type(values, self)
Copy link
Contributor

Choose a reason for hiding this comment

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

call this klass

return this_constructor_sliced(values, index=self.index,
name=items, fastpath=True)

Expand Down
53 changes: 0 additions & 53 deletions pandas/tests/frame/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,56 +570,3 @@ 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):
# 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