Skip to content
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

.transpose() to object dtype #16240

Open
Noghpu opened this issue May 15, 2024 · 1 comment
Open

.transpose() to object dtype #16240

Noghpu opened this issue May 15, 2024 · 1 comment
Labels
enhancement New feature or an improvement of an existing feature

Comments

@Noghpu
Copy link

Noghpu commented May 15, 2024

Description

Right now df.transpose() infers the resulting dtype, which for mixed types usually ends up being strings or lists of strings. Would it be possible to add a parameter to .transpose() to allow for object dtypes, where the rows are not casted at all? Just as an example with something like as_object:

df = pl.DataFrame(
    dict(
        int=[1],
        string=["one"],
        bool=[True],
        list=[[1]],
    ),
)

df_transposed = df.transpose(
    include_header=True,
    header_name="type",
    column_names=["value"],
)

df_object = df.transpose(
    include_header=True,
    header_name="type",
    column_names=["value"],
    as_object=True,
)

df =
shape: (1, 4)

int string bool list
i64 str bool list[i64]
----- -------- ------ -----------
1 one true [1]

df_transposed =
shape: (4, 2)

column column_0
str list[str]
-------- -----------
int ["1"]
string ["one"]
bool ["true"]
list ["1"]

df_object=
shape: (4, 2)

type value
str object
-------- ----------
int 1
string one
bool True
list [1]

As for the use case, personally, I was asked to create a table of a transposed df using great-tables. The automatic casting to strings of mixed columns means that I can't make use of great-tables built in number formatting methods. So instead I need to either recast the rows back to their original dtype or format the df using polars before transposing.

@Noghpu Noghpu added the enhancement New feature or an improvement of an existing feature label May 15, 2024
@geoffwright240
Copy link

Can I add another use case? Many financial reports have the dates flow horizontally across the worksheet. All manipulation etc can be done in polars but final report being pushed to Excel. So please add my vote for this enhancement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants