pola-rs / polars-xdt

Polars plugin offering eXtra stuff for DateTimes
https://marcogorelli.github.io/polars-xdt-docs/
MIT License
187 stars 8 forks source link

[NEW] `month_delta` function #57

Closed akmalsoliev closed 8 months ago

akmalsoliev commented 9 months ago

The month_diff function computes the integer representation of the month difference between two dates. The resulting values can be both positive and negative, signifying whether the comparative shift from the target date is to the past or the future, respectively.

akmalsoliev commented 9 months ago

Work In Progress

Issues:

MarcoGorelli commented 9 months ago

31st of January - 28th of February is still not considered as one whole month.

this looks fine to be honest

say you want to count the whole months between x and y, where y > x

the result is the smallest n such that x.dt.offset_by(f'{n}mo') is greater or equal to y

akmalsoliev commented 9 months ago

is this correct? shouldn't it be 11?

(Pdb) pl.select(xdt.month_delta(dt.date(1970, 1, 2), dt.date(1971, 1, 1)))
shape: (1, 1)
┌─────────┐
│ literal │
│ ---     │
│ i32     │
╞═════════╡
│ 12      │
└─────────┘

Fixed, there was unnecessary month comparison subtraction_condition that has there from previous commits.

NOTE: Added this to test section too.

MarcoGorelli commented 9 months ago

thanks for updating!

OK I think the results are correct. Or at least, I wasn't able to break it, even with hypothesis :)

I'll do another check of the code, and of the performance, as I'd like to think this can be simplified

MarcoGorelli commented 8 months ago

https://github.com/pola-rs/polars-xdt/pull/66 just fyi (now that we've got a hypothesis test in, we can confidently optimise away 😎 )