-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 5 commits
ae676b8
0c67e5a
3d6a4dd
e27554c
c643922
545de1e
f000fdb
356b02b
e4ec987
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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 | ||
# By doing it here there is no need for that function anymore | ||
# GH#19883. | ||
from pandas.core.dtypes.generic import ABCSparseArray | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make this a function like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see the idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, common is for introspection, not conversion There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the purpose of these tests? this is another issue, yes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be in a new PR There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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