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

Calculating time #214

Open
exsell-jc opened this issue Oct 10, 2022 · 5 comments
Open

Calculating time #214

exsell-jc opened this issue Oct 10, 2022 · 5 comments
Labels
feature New feature or request

Comments

@exsell-jc
Copy link

In R with lubridate, it would look like this:

one_year_before = some_date - years(1)
one_year_before = some_date - months(12)

But in tidypolars functions list, there doesn't seem to be a years or months function: https://tidypolars.readthedocs.io/en/latest/reference.html

@markfairbanks markfairbanks added the feature New feature or request label Oct 10, 2022
@markfairbanks
Copy link
Owner

Hmm I'll have to look into this more. At first glance there isn't a "simple translation". There might have to be a tidypolars helper for this.

@markfairbanks
Copy link
Owner

For now I'd recommend using col().dt.offset_by() straight from polars.

FYI this method is built into the col() expression, so you don't need to import polars for this to work.

import tidypolars as tp
from tidypolars import col

df = tp.Tibble(date = ['2021-01-01', '2021-10-01']).mutate(date = col('date').str.strptime(tp.Date))

(
    df
    .mutate(minus_two_months = col('date').dt.offset_by("-2mo"),
            add_year = col('date').dt.offset_by("1y"))
)
┌────────────┬──────────────────┬────────────┐
│ date       ┆ minus_two_months ┆ add_year   │
│ ---        ┆ ---              ┆ ---        │
│ date       ┆ date             ┆ date       │
╞════════════╪══════════════════╪════════════╡
│ 2021-01-01 ┆ 2020-11-01       ┆ 2022-01-01 │
├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2021-10-01 ┆ 2021-08-01       ┆ 2022-10-01 │
└────────────┴──────────────────┴────────────┘

@markfairbanks
Copy link
Owner

@exsell-jc
Copy link
Author

exsell-jc commented Dec 1, 2022

Workaround (not exactly a solution) -- use polars inbetween, not as elegant, but works

I'm having problem with filtering/date comparison. I just added the filter() pipe to your code.

monthss = 6
(
    df
    .mutate(minus_two_months = col('date').dt.offset_by("-2mo"),
            add_year = col('date').dt.offset_by("1y"))
    .filter(col('date') > max(col('date')).dt.offset_by(f'-{monthss}mo'))
)

Here is R's equivalent:

monthss = 6
df |>
  mutate(minus_two_months = date - months(2),
         add_year = date - years(1)) |>
  filter(date > max(date) - months(monthss))

@PathosEthosLogos
Copy link

(Link is dead)
Reporting another issue related -- calculation of date - date = x days (as duration, so it would be ddays(1))
e.g. 2000-01-01 - 1999-12-31 = 1 day
Compensating for leap years and other anomalies would be nice also.

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

No branches or pull requests

3 participants