Skip to content

BUG: Stack/unstack do not return subclassed objects (GH15563) #18929

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 1 commit into from
Jan 12, 2018

Conversation

rockg
Copy link
Contributor

@rockg rockg commented Dec 24, 2017

Basically picked from #15655 to support stack/unstack in subclassed DataFrames and Series.

@codecov
Copy link

codecov bot commented Dec 24, 2017

Codecov Report

Merging #18929 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #18929      +/-   ##
==========================================
+ Coverage   91.52%   91.54%   +0.02%     
==========================================
  Files         147      147              
  Lines       48775    48775              
==========================================
+ Hits        44639    44651      +12     
+ Misses       4136     4124      -12
Flag Coverage Δ
#multiple 89.91% <100%> (+0.02%) ⬆️
#single 41.62% <8.33%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/core/reshape/melt.py 97.19% <100%> (-0.06%) ⬇️
pandas/core/reshape/reshape.py 100% <100%> (ø) ⬆️
pandas/plotting/_converter.py 66.95% <0%> (+1.73%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8acdf80...16d82c5. Read the comment docs.

@sinhrks sinhrks added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Dec 25, 2017
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

looks pretty good.

@@ -345,7 +345,8 @@ Reshaping
- Fixed construction of a :class:`Series` from a ``dict`` containing ``NaN`` as key (:issue:`18480`)
- Bug in :func:`Series.rank` where ``Series`` containing ``NaT`` modifies the ``Series`` inplace (:issue:`18521`)
- Bug in :func:`Dataframe.pivot_table` which fails when the ``aggfunc`` arg is of type string. The behavior is now consistent with other methods like ``agg`` and ``apply`` (:issue:`18713`)

- Bug in :func:`DataFrame.stack`, `DataFrame.unstack`, `Series.unstack` which were not returning subclasses (:issue:`15563`)
Copy link
Contributor

Choose a reason for hiding this comment

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

need to have the :func: for each one

the default fill value for that data type, NaN for float, NaT for
datetimelike, etc. For integer types, by default data will converted to
float and missing values will be set to NaN.
constructor : object, default DataFrame
Copy link
Contributor

Choose a reason for hiding this comment

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

thanks!

@@ -85,6 +100,13 @@ def __init__(self, values, index, level=-1, value_columns=None,
self.values = values
self.value_columns = value_columns
self.fill_value = fill_value
if constructor is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm I hate if then's like this. any better way?

@@ -476,12 +499,13 @@ def _unstack_frame(obj, level, fill_value=None):
unstacker = partial(_Unstacker, index=obj.index,
level=level, fill_value=fill_value)
blocks = obj._data.unstack(unstacker)
klass = type(obj)
klass = obj._constructor
return klass(blocks)
else:
unstacker = _Unstacker(obj.values, obj.index, level=level,
Copy link
Contributor

Choose a reason for hiding this comment

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

might be a slightly bigger change, but could pass obj to _Unstacker directly and might be simpler.

@jreback jreback added Compat pandas objects compatability with Numpy or Python functions Bug labels Dec 26, 2017
@pep8speaks
Copy link

pep8speaks commented Dec 30, 2017

Hello @rockg! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on January 11, 2018 at 14:08 Hours UTC

@rockg
Copy link
Contributor Author

rockg commented Dec 30, 2017

Tried to incorporate obj into _Unstacker. Take a look. When there's a mixed frame being unstacked the underlying blocks are unstacked and so passing obj doesn't really work. However, for that case the constructors aren't used but applied later.

@jreback
Copy link
Contributor

jreback commented Dec 30, 2017

can you revert back to what you had. that was ok for now. its not as clear this way.

@rockg
Copy link
Contributor Author

rockg commented Jan 12, 2018

All green.

@jreback jreback added this to the 0.23.0 milestone Jan 12, 2018
@jreback jreback merged commit 8347ff8 into pandas-dev:master Jan 12, 2018
@jreback
Copy link
Contributor

jreback commented Jan 12, 2018

thanks @rockg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Compat pandas objects compatability with Numpy or Python functions Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preserve subclass family on reshape operations
4 participants