Skip to content

ENH: add to_frame method to Series #5164

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
Oct 10, 2013
Merged

ENH: add to_frame method to Series #5164

merged 1 commit into from
Oct 10, 2013

Conversation

drasch
Copy link
Contributor

@drasch drasch commented Oct 9, 2013

Adding a method to Series to promote it to a DataFrame. I've wished this existed so that when a method returns a series, and you could easily promote it to a DataFrame so that it's easier to add columns to it.

assert_almost_equal(rs, xp)

df = self.ts.to_dataframe(name = 'rs')
df['xp'] = self.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

use assert_frame_equal to compare the result vs the expected frame. pls add a test if name is passed in to_dataframe but name is different on the Series.

@jreback
Copy link
Contributor

jreback commented Oct 9, 2013

pls also add a release notes entry (you can reference this PR for the issue number), enhancements section.

also add to the docs section in api.

and a 1-liner in v0.13.0.txt

"""
from pandas.core.frame import DataFrame
if name is None:
df = DataFrame(self)
Copy link
Contributor

Choose a reason for hiding this comment

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

change this around a bit. The passed name should substitute for the series name (if it has one). and need a correspodning test for this as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

name = name if name is not None else self.name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll update with feedback. Thanks.

On Wednesday, October 9, 2013, jreback wrote:

In pandas/core/series.py:

  • def to_dataframe(self, name=None):
  •    """
    
  •    Convert Series to DataFrame
    
  •    Parameters
    

  •    name : object, default None
    
  •        The name of the column corresponding to the Series values
    
  •    Returns
    

  •    data_frame : DataFrame
    
  •    """
    
  •    from pandas.core.frame import DataFrame
    
  •    if name is None:
    
  •        df = DataFrame(self)
    

name = name if name is not None else self.name


Reply to this email directly or view it on GitHubhttps://github.com//pull/5164/files#r6869332
.

David Rasch
[email protected]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was copied from above, I'm not sure that

name = name if name is not None else self.name

will work when self.name = None

You end up with 'None' as the column index instead of 0.

I'm adding a test for this case too.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes if name is none then just do Dataframe(self)

the name parameter that u r passing should override the name of the series I think

@jreback
Copy link
Contributor

jreback commented Oct 9, 2013

pls change the name of the method to to_frame....for consistency with others ,e .g.to_panel....

@drasch
Copy link
Contributor Author

drasch commented Oct 10, 2013

See updated commit. Thanks for all the feedback.

@jreback let me know if the tests don't address the case you're concerned about

@drasch
Copy link
Contributor Author

drasch commented Oct 10, 2013

"also add to the docs section in api." how do I do this?

@jreback
Copy link
Contributor

jreback commented Oct 10, 2013

edit docs/source/api.rst

just like other files

@drasch
Copy link
Contributor Author

drasch commented Oct 10, 2013

on another note, how do I generate the docs for testing?

@jreback
Copy link
Contributor

jreback commented Oct 10, 2013

make doc
then view in a browser

@jreback
Copy link
Contributor

jreback commented Oct 10, 2013

@drasch
Copy link
Contributor Author

drasch commented Oct 10, 2013

Updated commit should include docs update.

@jreback
Copy link
Contributor

jreback commented Oct 10, 2013

can u add same line in v0.13 to doc/source/release.rst as well

@cpcloud @jtratner comments?

@cpcloud
Copy link
Member

cpcloud commented Oct 10, 2013

just needs the name changed to to_frame then good 2 go

@jreback jreback merged commit 844bcff into pandas-dev:master Oct 10, 2013
@jreback
Copy link
Contributor

jreback commented Oct 10, 2013

@drasch thanks!

@drasch
Copy link
Contributor Author

drasch commented Oct 10, 2013

doh, I just added the release.rst entry

@jreback
Copy link
Contributor

jreback commented Oct 10, 2013

no worries I added on the merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants