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

month_delta return incorrect value if passed a date object #84

Open
pounde opened this issue Oct 5, 2024 · 3 comments
Open

month_delta return incorrect value if passed a date object #84

pounde opened this issue Oct 5, 2024 · 3 comments

Comments

@pounde
Copy link

pounde commented Oct 5, 2024

I am attempting to replicate the functionality of relativedelta. The month_delta meets my current purposes but it return incorrect values if passed a date object.

This provided incorrect results. I would expect correct values or a ValueError

    df_ = df.filter(pl.col("date") <= end_date).with_columns(
        yr_for_calc=xdt.month_delta(pl.col("date"), end_date) / 12
    )

The following was my workaround:

    df_ = (
        df.filter(pl.col("date") <= end_date)
        .with_columns(end_date=end_date)
        .with_columns(
            yr_for_calc=xdt.month_delta(pl.col("date"), pl.col("end_date")) / 12
        )
    ).drop("end_date")
@MarcoGorelli
Copy link
Collaborator

thanks @pounde for the report

could you include a reproducible example please?

If I run

# ruff: noqa
from datetime import date, datetime
import polars as pl
import polars_xdt as xdt
df = pl.DataFrame(
    {
        "start_date": [
            date(2024, 3, 1),
            date(2024, 3, 31),
            date(2022, 2, 28),
            date(2023, 1, 31),
            date(2019, 12, 31),
        ],
        "end_date": [
            date(2023, 2, 28),
            date(2023, 2, 28),
            date(2023, 2, 28),
            date(2023, 1, 31),
            date(2023, 1, 1),
        ],
    },
)
print(df.with_columns(
    xdt.month_delta("start_date", date(2023, 2, 28)).alias("month_delta")
))
print(df.with_columns(
    xdt.month_delta("start_date", datetime(2023, 2, 28)).alias("month_delta")
))

then I do indeed get an error

polars.exceptions.ComputeError: the plugin failed with message: polars_xdt.month_delta only works on Date type. Please cast to Date first.

@pounde pounde changed the title month_delta return incorrect value if passed a datetime month_delta return incorrect value if passed a date object Oct 10, 2024
@pounde
Copy link
Author

pounde commented Oct 10, 2024

Apologies for not doing that from the start. I can't seem to replicate my code, nor can I replicate the code you posted. It is also worth noting that the object passed in the original issue was a 'date' object, not 'datetime'. I have updated the title to reflect that.

While attempting to run the code you provided, I get:

[line 22](vscode-notebook-cell:?execution_count=36&line=22)InvalidOperationError: Series month_delta, length 1 doesn't match the DataFrame height of 5

If you want expression: col("start_date")./usr/local/lib/python3.10/site-packages/polars_xdt/_internal.abi3.so:month_delta([2023-02-28]) to be broadcasted, ensure it is a scalar (for instance by adding '.first()').

Not sure why I didn't get this before, nor why it can't be broadcast.

@MarcoGorelli
Copy link
Collaborator

While attempting to run the code you provided, I get:

are you sure you got that while running the code I posted? could you post the full traceback please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants