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

pl.datetime does not raise with argument values outside of the specified domain #20977

Open
2 tasks done
baggiponte opened this issue Jan 29, 2025 · 2 comments · May be fixed by #21013
Open
2 tasks done

pl.datetime does not raise with argument values outside of the specified domain #20977

baggiponte opened this issue Jan 29, 2025 · 2 comments · May be fixed by #21013
Labels
A-exceptions Area: exception handling A-temporal Area: date/time functionality bug Something isn't working P-low Priority: low python Related to Python Polars

Comments

@baggiponte
Copy link
Contributor

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

import polars as pl

pl.select(pl.datetime(year=2025, month=1, day=1, hour=24))

Log output

shape: (1, 1)
┌──────────────┐
│ datetime     │
│ ---          │
│ datetime[μs] │
╞══════════════╡
│ null         │
└──────────────┘

Issue description

null

Expected behavior

The code should raise

Installed versions

--------Version info---------
Polars:              1.21.0
Index type:          UInt32
Platform:            macOS-14.4.1-arm64-arm-64bit
Python:              3.12.8 (main, Dec 19 2024, 14:22:58) [Clang 18.1.8 ]
LTS CPU:             False

----Optional dependencies----
Azure CLI            <not installed>
adbc_driver_manager  <not installed>
altair               5.5.0
azure.identity       <not installed>
boto3                <not installed>
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               <not installed>
gevent               <not installed>
google.auth          2.38.0
great_tables         <not installed>
matplotlib           3.10.0
numpy                2.2.2
openpyxl             <not installed>
pandas               2.2.3
pyarrow              19.0.0
pydantic             <not installed>
pyiceberg            <not installed>
sqlalchemy           2.0.37
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           <not installed>
@baggiponte baggiponte added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Jan 29, 2025
@MarcoGorelli MarcoGorelli added A-temporal Area: date/time functionality A-exceptions Area: exception handling P-low Priority: low and removed needs triage Awaiting prioritization by a maintainer labels Jan 29, 2025
@mcrumiller
Copy link
Contributor

mcrumiller commented Jan 29, 2025

This is not specific to hour, any invalid value produces a null:

>>> pl.select(pl.datetime(pl.lit("a"), -20, -25))
shape: (1, 1)
┌──────────────┐
│ datetime     │
│ ---          │
│ datetime[μs] │
╞══════════════╡
│ null         │
└──────────────┘

I wonder if pl.date/pl.datetime should have a strict parameter?

@baggiponte
Copy link
Contributor Author

baggiponte commented Jan 29, 2025

I don't think they should have a strict parameter. if the value is out of the domain, as per the docs, it should raise.

Image

The "silent null value" feels like going against the zen of Python in a way. It's an implicit choice. Raise by default forces you to deal with the error explicitly. By the way, it would also mirror what happens in the standard library:

from datetime import datetime
datetime(2025, 1, 1, 24)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: hour must be in 0..23

@baggiponte baggiponte changed the title pl.datetime with hour=24 does not raise pl.datetime does not raise with argument values outside of the specified domain Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-exceptions Area: exception handling A-temporal Area: date/time functionality bug Something isn't working P-low Priority: low python Related to Python Polars
Projects
None yet
3 participants