-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Subclassed reshape #15564
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
Subclassed reshape #15564
Changes from all commits
d8d8b7f
bab9091
7c05bf1
e930200
ff7ac1a
3cba84a
3b6bf41
68f6d15
4c8b58d
bfd146d
f0be4c8
1c2535d
5826158
523d767
dacdf40
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 |
---|---|---|
|
@@ -371,8 +371,11 @@ def pivot(self, index=None, columns=None, values=None): | |
index = self.index | ||
else: | ||
index = self[index] | ||
indexed = Series(self[values].values, | ||
index=MultiIndex.from_arrays([index, self[columns]])) | ||
|
||
indexed = self._constructor_sliced( | ||
self[values].values, | ||
index=MultiIndex.from_arrays([index, self[columns]])) | ||
|
||
return indexed.unstack(columns) | ||
|
||
|
||
|
@@ -448,13 +451,24 @@ def unstack(obj, level, fill_value=None): | |
|
||
if isinstance(obj, DataFrame): | ||
if isinstance(obj.index, MultiIndex): | ||
return _unstack_frame(obj, level, fill_value=fill_value) | ||
unstacked = _unstack_frame(obj, level, fill_value=fill_value) | ||
else: | ||
unstacked = obj.T.stack(dropna=False) | ||
|
||
if len(unstacked.shape) == 1: | ||
return obj._constructor_sliced(unstacked) | ||
else: | ||
return obj.T.stack(dropna=False) | ||
return obj._constructor(unstacked) | ||
|
||
else: | ||
unstacker = _Unstacker(obj.values, obj.index, level=level, | ||
fill_value=fill_value) | ||
return unstacker.get_result() | ||
unstacked = unstacker.get_result() | ||
|
||
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. same here, this now has logic in 2 places, the creation itself of the unstacked result, then the return class. should be a part of |
||
if len(unstacked.shape) == 1: | ||
return obj._constructor(unstacked) | ||
else: | ||
return obj._constructor_expanddim(unstacked) | ||
|
||
|
||
def _unstack_frame(obj, level, fill_value=None): | ||
|
@@ -553,7 +567,7 @@ def factorize(index): | |
mask = notnull(new_values) | ||
new_values = new_values[mask] | ||
new_index = new_index[mask] | ||
return Series(new_values, index=new_index) | ||
return frame._constructor_sliced(new_values, index=new_index) | ||
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. so this is good |
||
|
||
|
||
def stack_multiple(frame, level, dropna=True): | ||
|
@@ -692,7 +706,7 @@ def _convert_level_number(level_num, columns): | |
new_index = MultiIndex(levels=new_levels, labels=new_labels, | ||
names=new_names, verify_integrity=False) | ||
|
||
result = DataFrame(new_data, index=new_index, columns=new_columns) | ||
result = frame._constructor(new_data, index=new_index, columns=new_columns) | ||
|
||
# more efficient way to go about this? can do the whole masking biz but | ||
# will only save a small amount of time... | ||
|
@@ -852,7 +866,7 @@ def melt(frame, id_vars=None, value_vars=None, var_name=None, | |
mdata[col] = np.asanyarray(frame.columns | ||
._get_level_values(i)).repeat(N) | ||
|
||
return DataFrame(mdata, columns=mcolumns) | ||
return frame._constructor(mdata, columns=mcolumns) | ||
|
||
|
||
def lreshape(data, groups, dropna=True, label=None): | ||
|
@@ -921,7 +935,7 @@ def lreshape(data, groups, dropna=True, label=None): | |
if not mask.all(): | ||
mdata = dict((k, v[mask]) for k, v in compat.iteritems(mdata)) | ||
|
||
return DataFrame(mdata, columns=id_cols + pivot_cols) | ||
return data._constructor(mdata, columns=id_cols + pivot_cols) | ||
|
||
|
||
def wide_to_long(df, stubnames, i, j, sep="", suffix='\d+'): | ||
|
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.
hmm I think this should be done inside the
.stack
and_unstack_frame
functions no?