Bug in DataFrame()?

34 views
Skip to first unread message

Richard Stanton

unread,
May 21, 2013, 5:34:56 PM5/21/13
to pyd...@googlegroups.com
When using DataFrame() with a dataframe as input, trying to set the column
names results in very odd behavior. Here's an example:

df = DataFrame({'A' : [1,2,3], 'B' : [1,2,3]})
DataFrame(df, columns=['C','D'])

Result:

C D
0 NaN NaN
1 NaN NaN
2 NaN NaN


Of course I can always reset the column names in a separate statement, but
pandas makes me want to do as much as possible in a single line.





Jeff Reback

unread,
May 21, 2013, 5:42:30 PM5/21/13
to pyd...@googlegroups.com

this is equivalent of

df.reindex(columns=['C','D'])

so it is correct and not a bug

you are reindexing to columns which are empty
> --
> You received this message because you are subscribed to the Google Groups "PyData" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Paul Hobson

unread,
May 28, 2013, 2:59:22 PM5/28/13
to pyd...@googlegroups.com
On Tue, May 21, 2013 at 2:42 PM, Jeff Reback <jeffr...@gmail.com> wrote:

this is equivalent of

df.reindex(columns=['C','D'])

so it is correct and not a bug

you are reindexing to columns which are empty

On May 21, 2013, at 5:34 PM, Richard Stanton <sta...@haas.berkeley.edu> wrote:

> When using DataFrame() with a dataframe as input, trying to set the column
> names results in very odd behavior. Here's an example:
>
> df = DataFrame({'A' : [1,2,3], 'B' : [1,2,3]})
> DataFrame(df, columns=['C','D'])
>
> Result:
>
>    C   D
> 0 NaN NaN
> 1 NaN NaN
> 2 NaN NaN
>
>
> Of course I can always reset the column names in a separate statement, but
> pandas makes me want to do as much as possible in  a single line. 
 

So with version 0.11, I can do this:
import pandas
df = pandas.DataFrame({'A' : [1,2,3], 'B' : [1,2,3]}).rename(columns={'A':'C', 'B':'D'}) 
df
   C  D
0  1  1
1  2  2
2  3  3

Reply all
Reply to author
Forward
0 new messages