Skip to content

Conversation

mrkn
Copy link
Contributor

@mrkn mrkn commented Jun 7, 2025

Implements dt.days_in_month() for Date and Datetime columns.

Uses the existing DAYS_PER_MONTH array and is_leap_year() function for performance instead of chrono's num_days_in_month().

>>> df = pl.DataFrame({"date": [date(2001, 1, 1), date(2000, 2, 1)]})
>>> df.with_columns(pl.col("date").dt.days_in_month())
┌────────────┬───────────────┐
│ datedays_in_month │
│ ------           │
│ datei8            │
╞════════════╪═══════════════╡
│ 2001-01-0131            │
│ 2000-02-0129            │
└────────────┴───────────────┘

This is my first contribution to a Rust project. As a Rust beginner, I'd appreciate thorough code review and feedback to help me improve.

Fixes #14193

@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 labels Jun 7, 2025
@mrkn mrkn force-pushed the days_in_month branch 2 times, most recently from 1d5381d to e8f02e8 Compare June 7, 2025 10:20
Copy link

codecov bot commented Jun 7, 2025

Codecov Report

❌ Patch coverage is 93.65079% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.46%. Comparing base (ee338f7) to head (a608607).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
...ates/polars-plan/src/dsl/function_expr/datetime.rs 0.00% 1 Missing ⚠️
...ars-plan/src/plans/aexpr/function_expr/datetime.rs 83.33% 1 Missing ⚠️
.../polars-python/src/lazyframe/visitor/expr_nodes.rs 0.00% 1 Missing ⚠️
crates/polars-time/src/series/mod.rs 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23119      +/-   ##
==========================================
+ Coverage   81.44%   81.46%   +0.01%     
==========================================
  Files        1655     1663       +8     
  Lines      224589   224634      +45     
  Branches     2881     2881              
==========================================
+ Hits       182923   182994      +71     
+ Misses      40949    40923      -26     
  Partials      717      717              

☔ 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.

@MarcoGorelli
Copy link
Collaborator

thanks for your pr

this looks like it's off to a good start, but quite a few lines are reported as uncovered - could you make sure your tests cover them all (or at least, nearly all?) please?

@mrkn
Copy link
Contributor Author

mrkn commented Jun 8, 2025

@MarcoGorelli Thanks for the feedback! I’ll work on improving the test coverage.

@mrkn
Copy link
Contributor Author

mrkn commented Jun 8, 2025

@MarcoGorelli Is the coverage in polars-plan/src/dsl/function_expr/datetime.rs something I should look into? I believe this can be covered by checking the result of repr(pl.col("x").dt.days_in_month()).

@MarcoGorelli
Copy link
Collaborator

thanks for updating!

could you fetch and rebase onto the polars main branch please? there's some unrelated changes in https://github.com/pola-rs/polars/pull/23119/files which make it harder to review

@mrkn mrkn force-pushed the days_in_month branch 2 times, most recently from 4b0cf33 to 77e158e Compare June 19, 2025 14:36
@mrkn
Copy link
Contributor Author

mrkn commented Jun 19, 2025

@MarcoGorelli I've finished rebasing onto the main branch.

@MarcoGorelli
Copy link
Collaborator

thanks! will try to get to this in the coming week

@mrkn mrkn force-pushed the days_in_month branch 2 times, most recently from fc3a4e6 to 13a48d0 Compare July 3, 2025 15:25
Copy link
Collaborator

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @mrkn ! all looks good

my only suggestion is - rather than adding an extra pattern to to_temporal_unit, to make a separate macro. this is partially to keep to_temporal_unit simple, but also because I'm not sure that "days in month" qualifies as a temporal unit

apart from that, this looks good to me, thanks for sticking with it 💪

@mrkn
Copy link
Contributor Author

mrkn commented Jul 7, 2025

@MarcoGorelli Thanks for the review! Understood—I’ll try separating the macro as you suggested. I was also unsure whether days_in_month truly counts as a temporal unit, so I’ll think about a more appropriate name as well.

@mrkn mrkn force-pushed the days_in_month branch 2 times, most recently from d75a7ca to 0b74444 Compare July 16, 2025 09:20
@mrkn
Copy link
Contributor Author

mrkn commented Jul 16, 2025

@MarcoGorelli, I've updated the branch. I extracted the second variant of the to_temporal_unit macro into a separate macro named to_calendar_value. What do you think?

@mrkn mrkn requested a review from MarcoGorelli July 16, 2025 09:27
Copy link
Collaborator

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @mrkn , looks good to me!

@coastalwhite coastalwhite merged commit 52eea50 into pola-rs:main Aug 3, 2025
27 of 29 checks passed
@mrkn mrkn deleted the days_in_month branch August 3, 2025 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

Add .dt.days_in_month()
3 participants