ibis icon indicating copy to clipboard operation
ibis copied to clipboard

feat(api): `day_of_week.iso_index()` method

Open kaijennissen opened this issue 1 year ago โ€ข 10 comments

Description of changes

Added a new isoday_of_week method for timestamps.

Issues closed

Resolves #8989

kaijennissen avatar Apr 18 '24 14:04 kaijennissen

ACTION NEEDED

Ibis follows the Conventional Commits specification for release automation.

The PR title and description are used as the merge commit message.

Please update your PR title and description to match the specification.

github-actions[bot] avatar Apr 18 '24 14:04 github-actions[bot]

We have a DayOfWeek class defined in ibis/expr/types/temporal.py.

It's a bit of an oddball API in that it functions like a namespace instead of top level method like everything else.

Naturally, your first development experience with Ibis is extending a slightly weird bit of the codebase ๐Ÿ˜‚.

Perhaps we can add two new methods there?

class DayOfWeek:
    ...

    def iso_index(self): ...
    def iso_full_name(self): ...

for consistency with the surrounding methods?

cpcloud avatar Apr 18 '24 14:04 cpcloud

Here's a usage example:

In [8]: t = ibis.read_parquet("/data/avg_duration_over_time.parquet")

In [9]: t.head()
Out[9]:
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ date       โ”ƒ avg_duration_m โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ date       โ”‚ float64        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 2024-04-16 โ”‚      57.833333 โ”‚
โ”‚ 2024-04-15 โ”‚      42.833333 โ”‚
โ”‚ 2024-04-14 โ”‚      31.500000 โ”‚
โ”‚ 2024-04-13 โ”‚      53.600000 โ”‚
โ”‚ 2024-04-12 โ”‚      47.400000 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

In [10]: t.select(
    ...:     "date",
    ...:     weekday=t.date.day_of_week.index(),
    ...:     weekday_name=t.date.day_of_week.full_name(),
    ...: )
Out[10]:
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ date       โ”ƒ weekday โ”ƒ weekday_name โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ date       โ”‚ int16   โ”‚ string       โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 2024-04-16 โ”‚       1 โ”‚ Tuesday      โ”‚
โ”‚ 2024-04-15 โ”‚       0 โ”‚ Monday       โ”‚
โ”‚ 2024-04-14 โ”‚       6 โ”‚ Sunday       โ”‚
โ”‚ 2024-04-13 โ”‚       5 โ”‚ Saturday     โ”‚
โ”‚ 2024-04-12 โ”‚       4 โ”‚ Friday       โ”‚
โ”‚ 2024-04-10 โ”‚       2 โ”‚ Wednesday    โ”‚
โ”‚ 2024-04-09 โ”‚       1 โ”‚ Tuesday      โ”‚
โ”‚ 2024-04-07 โ”‚       6 โ”‚ Sunday       โ”‚
โ”‚ 2024-04-06 โ”‚       5 โ”‚ Saturday     โ”‚
โ”‚ 2024-04-05 โ”‚       4 โ”‚ Friday       โ”‚
โ”‚ โ€ฆ          โ”‚       โ€ฆ โ”‚ โ€ฆ            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

cpcloud avatar Apr 18 '24 14:04 cpcloud

Alternatively, I can take a crack at this one, if you want to handle iso_year()

cpcloud avatar Apr 18 '24 14:04 cpcloud

We have a DayOfWeek class defined in ibis/expr/types/temporal.py.

I missed that.

Alternatively, I can take a crack at this one, if you want to handle iso_year()

I'll give it a try. And iso_yearwith a different PR.

kaijennissen avatar Apr 18 '24 15:04 kaijennissen

Is it possible that there are currently no tests for .day_of_week.index and .day_of_week.full_name? Was looking to place a test nearby but couldn't find them.

kaijennissen avatar Apr 19 '24 11:04 kaijennissen

@kaijennissen You should be able to find the tests that run across backends in ibis/backends/tests/test_temporal.py:

โฏ rg test_day_of_week
ibis/backends/bigquery/tests/unit/test_compiler.py
72:def test_day_of_week(case, dtype, snapshot):

ibis/backends/impala/tests/test_client.py
202:def test_day_of_week(con):

ibis/backends/tests/test_temporal.py
1463:def test_day_of_week_scalar(con, date, expected_index, expected_day):
1484:def test_day_of_week_column(backend, alltypes, df):
1529:def test_day_of_week_column_group_by(

cpcloud avatar Apr 19 '24 12:04 cpcloud

@cpcloud The sqlite tests pass locally. The exasol as well as the impala containers do not work thus I cannot test this locally. I have to investigate this a little deeper. I have no idea why the docs-test fails.

kaijennissen avatar Apr 20 '24 20:04 kaijennissen

@kaijennissen Are you still interested in this feature? If so, would you mind trying it out for your use case and reporting back?

cpcloud avatar Jul 16 '24 15:07 cpcloud

@cpcloud Sorry for the late answer. It works for my use case, thanks.

kaijennissen avatar Aug 29 '24 12:08 kaijennissen