Skip to content

add to_frame method to DataFrame #24336

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

Closed
wants to merge 1 commit into from

Conversation

letalvoj
Copy link

@letalvoj letalvoj commented Dec 18, 2018

Add convenience method which makes sure that we can safely call to_frame on any method returning both Series and DataFrame instances, such as groupby etc.

Add convenience method which makes sure that we can safely call to_frame
on any method returning both Series and DataFrame instances, such as
groupby etc.
@pep8speaks
Copy link

Hello @letalvoj! Thanks for submitting the PR.

@jreback
Copy link
Contributor

jreback commented Dec 18, 2018

-0 on this. just cluttering the API. semantically this is alway wrong, it should copy.

@letalvoj
Copy link
Author

letalvoj commented Dec 18, 2018

Sometimes I find people struggling to use groupby + apply / loc. As these methods can easily return both DataFrame / Series given not only the params but the instance on which you call it as well. IMO this is more elegant then isinstanceing.

ad. api
I get the argument regarding the API. yet in a sense all the other classes have the method

  • Series.to_frame
  • Panel.to_frame

even some indices

  • Index.to_frame
  • MultiIndex.to_frame
  • DatetimeIndex.to_frame
  • TimedeltaIndex.to_frame

Therefore I feel like it's not just some other random method. Or is it?

ad. copying
Oh, right. The implementation is copy-on-write, right? Is it going to be a great overhead then?
What is the idiomatic way to copy the frame in this case?

@codecov
Copy link

codecov bot commented Dec 18, 2018

Codecov Report

Merging #24336 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #24336      +/-   ##
==========================================
+ Coverage   92.28%   92.28%   +<.01%     
==========================================
  Files         162      162              
  Lines       51833    51835       +2     
==========================================
+ Hits        47833    47835       +2     
  Misses       4000     4000
Flag Coverage Δ
#multiple 90.68% <100%> (ø) ⬆️
#single 42.99% <50%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/core/frame.py 96.91% <100%> (ø) ⬆️
pandas/core/indexes/period.py 93.06% <0%> (-0.04%) ⬇️
pandas/core/reshape/tile.py 94.82% <0%> (+0.06%) ⬆️

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 6c8aabc...7038a79. Read the comment docs.

1 similar comment
@codecov
Copy link

codecov bot commented Dec 18, 2018

Codecov Report

Merging #24336 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #24336      +/-   ##
==========================================
+ Coverage   92.28%   92.28%   +<.01%     
==========================================
  Files         162      162              
  Lines       51833    51835       +2     
==========================================
+ Hits        47833    47835       +2     
  Misses       4000     4000
Flag Coverage Δ
#multiple 90.68% <100%> (ø) ⬆️
#single 42.99% <50%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/core/frame.py 96.91% <100%> (ø) ⬆️
pandas/core/indexes/period.py 93.06% <0%> (-0.04%) ⬇️
pandas/core/reshape/tile.py 94.82% <0%> (+0.06%) ⬆️

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 6c8aabc...7038a79. Read the comment docs.

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.

needs some discussion

@TomAugspurger
Copy link
Contributor

I don't have a strong preference here. Probably +0.5 since it makes writing generic code for Series / Frame easier (although, you won't be able to use the name keyword).

FWIW, Series.to_frame doesn't copy.

In [6]: s = pd.Series([1, 2])

In [7]: r = s.to_frame()

In [8]: s[0] = 10

In [9]: r
Out[9]:
    0
0  10
1   2

though it arguably should (as an option).

@datapythonista
Copy link
Member

@letalvoj do you mind writing a real case example on when this would be useful. I understand your point, but would be useful for the discussion to see how a real example would look with and without DataFrame.to_frame()

@gfyoung gfyoung added Enhancement DataFrame DataFrame data structure Needs Discussion Requires discussion from core team before further action labels Dec 22, 2018
@jreback
Copy link
Contributor

jreback commented Jan 14, 2019

@letalvoj I think this needs an issue for discussion. If you'd like to open one with a realistic usecase we can see if there is appetite. closing this for now.

@jreback jreback closed this Jan 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DataFrame DataFrame data structure Enhancement Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Export to excel for multiindex columns
6 participants