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

Add .dt.replace #8879

Open
mcrumiller opened this issue May 16, 2023 · 9 comments · May be fixed by #19708
Open

Add .dt.replace #8879

mcrumiller opened this issue May 16, 2023 · 9 comments · May be fixed by #19708
Labels
A-temporal Area: date/time functionality accepted Ready for implementation enhancement New feature or an improvement of an existing feature

Comments

@mcrumiller
Copy link
Contributor

mcrumiller commented May 16, 2023

Problem description

datetime.date has a replace function that lets you immediately set the day/month/year:

>>> from datetime import date
>>> date(2023, 1, 1).replace(month=3)
datetime.date(2023, 3, 1)

This would be a nice feature for series/expressions, especially if the input arg could also be an expression:

from datetime import date
import polars as pl
from polars import col

df = pl.DataFrame({
     "year": [2015, 2016, 2017],
    "my_date": [date(2023, 1, 1), date(2023, 1, 1), date(2023, 1, 1)],
})

df.select(
    col("my_date").dt.replace(year=col("year"))
)
shape: (3, 2)
┌──────┬────────────┐
│ year ┆ my_date    │
│ ---  ┆ ---        │
│ i64  ┆ date       │
╞══════╪════════════╡
│ 2015 ┆ 2015-01-01 │
│ 2016 ┆ 2016-01-01 │
│ 2017 ┆ 2017-01-01 │
└──────┴────────────┘
@mcrumiller mcrumiller added the enhancement New feature or an improvement of an existing feature label May 16, 2023
@deanm0000
Copy link
Collaborator

I don't know how to monkey patch this into the dt namespace but otherwise, here ya go

def replace(self, year=None, month=None, day=None):
    """Mimic the datetime replace method"""
    if year is None and month is None and day is None:
        raise ValueError("don't you want to replace something, specify a year, month, and/or day")
    if year is None:
        outyear=self.dt.year()
    else:
        outyear=year
    if month is None:
        outmonth=self.dt.month()
    else:
        outmonth=month
    if day is None:
        outday=self.dt.day()
    else:
        outday=day
    return pl.date(outyear, outmonth, outday)
pl.Expr.replace=replace

then you can do

df.select(
    col("my_date").replace(year=col("year"))
)

@mcrumiller
Copy link
Contributor Author

Thanks @deanm0000 . Yeah, I currently have my own monkeypatched methods but I felt it would be a good addition to the API.

FYI, when you post code in github, you can fence it with a language, e.g. ```python and you'll get syntax highlighting.

@mcrumiller
Copy link
Contributor Author

@MarcoGorelli does someone want to accept this feature request? I can work on it but I don't want to spend the time if it's not desired/will be rejected.

@mcrumiller
Copy link
Contributor Author

(FYI this is the original one :D)

@MarcoGorelli MarcoGorelli added the accepted Ready for implementation label Aug 24, 2024
@MarcoGorelli
Copy link
Collaborator

sure, marking as 'accepted', feel free to ping me for review

@MarcoGorelli
Copy link
Collaborator

MarcoGorelli commented Nov 3, 2024

i don't think anyone's actively working on it, are you interetsed in taking it?

EDIT: not sure why github is displaying this comment above @Andre-Medina 's below, even though my comment came after, my guess is that it's a DST-related but at GitHub 🤣 (today the clocks changed in the US, but not in the UK where I live)

@Andre-Medina
Copy link

was this issue still being worked on?

@Andre-Medina
Copy link

i don't think anyone's actively working on it, are you interetsed in taking it?

I wish I had the rust know how. I looked into this issue a few months back but my 0 rust experience held me back, especially with how technical the polars repo seems to be. Would love to learn one day :)

EDIT: not sure why github is displaying this comment above @Andre-Medina 's below, even though my comment came after, my guess is that it's a DST-related but at GitHub 🤣 (today the clocks changed in the US, but not in the UK where I live)

Haha wow that's very odd. For me it says your comment is slightly older. Love DST. thought everything would be UNIX timestamped tho?

@mcrumiller
Copy link
Contributor Author

I have an almost-working implementation, just need to find some time one of these days to finalize it.

@mcrumiller mcrumiller linked a pull request Nov 9, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-temporal Area: date/time functionality accepted Ready for implementation enhancement New feature or an improvement of an existing feature
Projects
Status: Ready
Development

Successfully merging a pull request may close this issue.

4 participants