1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Specify string format for numeric during conversion to pl.String

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 19:52.

  1. Stack

    Stack Membro Participativo

    Is there any way to specify a format specifier if, for example, casting a pl.Float32, without resorting to complex searches for the period character? As in something like:

    s = pl.Series([1.2345, 2.3456, 3.4567])

    s.cast(pl.String, format="%0.2f") # format obviously isn't an argument


    My current method is the following:

    n = 2 # number of decimals desired
    expr = pl.concat_str((
    s.floor().cast(pl.Int32).cast(pl.String),
    pl.lit('.'),
    ((s%1)*(10**n)).round(0).cast(pl.Int32).cast(pl.String)
    )).str.pad_end(width)


    i.e separate the pre-decimal and post-decimal, format individually as strings, and concat together. Is there an easier way to do this?

    Expected output:


    shape: (3,)
    Series: '' [str]
    [
    "1.23"
    "2.34"
    "3.45"
    ]

    Continue reading...

Compartilhe esta Página