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

Additional info regarding the questions raised in your article. #1

Open
cmdlineluser opened this issue Oct 25, 2023 · 0 comments
Open

Comments

@cmdlineluser
Copy link

cmdlineluser commented Oct 25, 2023

Hey there, wasn't sure where best to respond but I just thought I'd share some info I've learned as a Polars user which may be of interest:

For to the convert a string to a datetime example:

df.with_columns(dt = pl.col('string_date').str.to_datetime().cast(pl.Datetime))

Perhaps the .cast() is a stray from previous examples, but you already have a datetime so it can be omitted.

df.with_columns(dt = pl.col('string_date').str.to_datetime())
  • dt.date()

dt.date() can also be used to get a date from a datetime

df.with_columns(
    pl.col('string_date').str.to_datetime().dt.date()
)
  • pl.col("foo").cast(pl.Date)

I’m curious what happens if I simply just .cast('date')

This has been requested, there is a pending PR: pola-rs/polars#10517

[Update]: This was added in pola-rs/polars#12072

  • .strptime()

I’ll be buggered if I know the difference between to_datetime() and strptime()

They are essentially aliases, to_date() / to_datetime() were added later and str.strptime() remains for legacy reasons.



This is why .to_datetime() lives under .str but you do bring up an interesting point as the new name doesn't imply it is tied to strings.

It should be simply pl.col('column').to_datetime() and error if the data types are wrong

  • .dt.to_string()

Similar to the strptime case, .dt.to_string() was added as an alias in favour of .dt.strftime()

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

No branches or pull requests

1 participant