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

Can no longer pass pd.Timestamp objects into places/expressions that take datetime64 #18447

Closed
2 tasks done
agossard opened this issue Aug 28, 2024 · 7 comments · Fixed by #18497
Closed
2 tasks done
Labels
bug Something isn't working python Related to Python Polars regression Issue introduced by a new release

Comments

@agossard
Copy link
Contributor

agossard commented Aug 28, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

This code worked in 1.5, but crashes in 1.6

pl.datetime_range(pd.to_datetime('12/31/2020'),
                              pd.to_datetime('12/31/2022'),
                              interval="1d",
                              eager=True)

TypeError: cannot create expression literal for value of type Timestamp: Timestamp('2020-12-31 00:00:00+0000', tz='UTC')
Hint: Pass `allow_object=True` to accept any value and create a literal of type Object.

This fixes it:

pl.datetime_range(pd.to_datetime('12/31/2020').to_datetime64(),
                  pd.to_datetime('12/31/2022').to_datetime64(),
                  interval="1d",
                  eager=True)

Understand the documentation does not explicitly say that you can pass it a pandas Timestamp, but once again, seemed a reasonable user assumption and used to work.

Also, this crashes now with similar error.

df = pl.DataFrame({"dates": pd.date_range(pd.to_datetime('12/31/2020'), pd.to_datetime('12/31/2021'), freq='B')})
df.filter(pl.col('dt') < pd.to_datetime('12/31/2020'))

Log output

No response

Issue description

Cannot pass pd.Timestamp to functions that expect datetime.

Expected behavior

Would be nice to automatically cast the Timestamp to datetime64 on entry to polars

Installed versions

didn't get a chance to do this... it's on my work computer

@agossard agossard added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Aug 28, 2024
@MarcoGorelli MarcoGorelli added regression Issue introduced by a new release and removed needs triage Awaiting prioritization by a maintainer labels Aug 28, 2024
@MarcoGorelli
Copy link
Collaborator

thanks @agossard for the report, much appreciated 🙏

This regressed in #16018 (@mcrumiller fancy taking a look? maybe something similar to #18346 can solve this?)

@mcrumiller
Copy link
Contributor

I'll definitely take a look.

@mcrumiller
Copy link
Contributor

mcrumiller commented Aug 29, 2024

Yep, also for pd.Timedelta/np.datetime64/np.timedelta64/. Working on it.

@mcrumiller
Copy link
Contributor

@MarcoGorelli, should we allow pl.lit(datetime(...), pl.Time)? i.e. extract time of day from a datetime object?

@mcrumiller
Copy link
Contributor

I suppose that should be an enhancement, I'll leave that out of this.

@btschroer
Copy link

Can confirm, the following used to work in 1.5:

import polars as pl
import pandas as pd

t0 = pd.Timestamp("2001-01-03")
frame = pl.from_dict({
    "t": ["2001-01-01", "2001-01-02", "2001-01-03", "2001-01-04"]
}).with_columns(
    pl.col("t").str.to_datetime()
)
frame.filter(pl.col("t") >= t0)

@MarcoGorelli
Copy link
Collaborator

thanks @btschroer !

I think pd.Timestamp should be supported in so far as it's a subclass of datetime.datetime

np.datetime64 I'm less sure about tbh, not sure it's worth the added complexity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Related to Python Polars regression Issue introduced by a new release
Projects
None yet
4 participants