Skip to content

feat: Improved time-string parsing and inference (generally, and via the SQL interface) #22606

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented May 5, 2025

Addresses an Issue raised on the Discord (in the #sql channel) by @etrotta.

  • Generic time-string parsing improvement (adds %R chrono hour-minute format to the set of recognised time specifiers - which we already identify in datetime strings).
  • Brings SQL temporal literal string inference in-line with the underlying to_datetime and to_time functions so that hour-minute format (%R, aka:%H:%M) is also recognised in the SQL interface (both standalone, as a time, and as part of a larger datetime/timestamp string). A subsequent/follow-up PR will bring a similar improvement to SQL CAST ops.

Example

Use of '%H:%M' literal strings in WHERE clauses:

import polars as pl

df = pl.DataFrame({
    "tm": [time(0,45,10), time(10,30,59), time(18,5,20), time(12,25,5)],
    "val": ["aaa", "bbb", "ccc" ,"ddd"],
})
df.sql("SELECT * FROM self WHERE tm > '12:00' ORDER BY tm")
# shape: (2, 2)
# ┌──────────┬─────┐
# │ tm       ┆ val │
# │ ---      ┆ --- │
# │ time     ┆ str │
# ╞══════════╪═════╡
# │ 12:25:05 ┆ ddd │
# │ 18:05:20 ┆ ccc │
# └──────────┴─────┘

Properly inferred by str.to_time():

pl.DataFrame({
    "tmstr": ["00:01", "23:00", "14:30"],
}).with_columns(
    tm = pl.col("tmstr").str.to_time(),
)
# shape: (3, 2)
# ┌───────┬──────────┐
# │ tmstr ┆ tm       │
# │ ---   ┆ ---      │
# │ str   ┆ time     │
# ╞═══════╪══════════╡
# │ 00:01 ┆ 00:01:00 │
# │ 23:00 ┆ 23:00:00 │
# │ 14:30 ┆ 14:30:00 │
# └───────┴──────────┘

@alexander-beedie alexander-beedie changed the title feat: Improved time-string parsing/inference (additionally recognise "HH:MM" by default) feat: Improved time-string parsing and inference (additionally recognise "HH:MM" by default) May 5, 2025
@alexander-beedie alexander-beedie changed the title feat: Improved time-string parsing and inference (additionally recognise "HH:MM" by default) feat: Improved time-string parsing and inference (generally, and via the SQL interface) May 5, 2025
@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars and removed title needs formatting labels May 5, 2025
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label May 5, 2025
Copy link

codecov bot commented May 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.89%. Comparing base (0d5fdef) to head (ba6b5f3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #22606      +/-   ##
==========================================
- Coverage   80.90%   80.89%   -0.02%     
==========================================
  Files        1656     1656              
  Lines      234036   234036              
  Branches     2752     2752              
==========================================
- Hits       189349   189323      -26     
- Misses      44027    44053      +26     
  Partials      660      660              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql Area: Polars SQL functionality enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant